C++初学者关于输入/输出文本文件的问题。?

标签 c++ text input ascii

如果我有一个输入文本,其中唯一写的是“A”,并且我想要一系列代码来生成下一个 ASCII 集 (B),我该怎么做?

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

#include iostream>
#include fstream>
#include iomanip>
#include string>

using namespace std;

int main()
{

ifstream inFile;

ofstream outFile;

    string firstName;
    string lastName;
    string character;

    int age;
    double rectangle, length, width, area, parameter, circle, radius, areaCircle, circumference, beginningBalance, interestRate, endBalance;

    inFile.open("inData.txt");
    outFile.open("outData.txt");

    outFile << fixed << showpoint;
    outFile << setprecision(2);

    cout << "Data is processing..." << endl;

    inFile >> length >> width;
    area = length * width;
    parameter = (length * 2) + (width *2);
    outFile << "Rectangle:" << endl;
    outFile << "Length = " << length << " " << "width = " << width << " " << "area = " << area << " " << "parameter = " << parameter << endl;

    inFile >> radius;
    outFile << " " << endl;
    outFile << "Cricle:" <<endl;
    areaCircle = 3.14159 * (radius * radius);
    circumference = 2 * (3.14159 * radius);
    outFile << "Radius = " << radius << " " << "area = " << areaCircle << " " << "circumference = " << circumference;

    outFile << endl;
    outFile << endl;

    inFile >> firstName >> lastName >> age;
    outFile << "Name: " << firstName << " " << lastName << "," << " " << "age: " << age;

    outFile << endl;

    inFile >> beginningBalance >> interestRate;
    outFile << "Beginning balance = " << beginningBalance << "," << " " << "interest rate = " << interestRate << endl;
    endBalance = ((18500 * .0350) / 12.0 ) + 18500;
    outFile << "Balance at the end of the month = $" << endBalance;

    outFile << endl;
    inFile >> character;
    outFile << "The character that comes after" << character << "in the ASCII set is" << character +1;



        inFile.close();
    outFile.close();

    return 0;

}

最佳答案

与其将character 声明为string,不如将其声明为charstring 指的是std::string,一个用于存储多个字符的高级对象。 char 是一种低级 native 类型,它只存储 1 个字符。

char character;
infile >> character;
outfile << "next after " << character << " is " << char(character+1) << endl;

尽管输入了 Z 或字母。

关于C++初学者关于输入/输出文本文件的问题。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307780/

相关文章:

c++ - 阻止代码处理的宏

mysql - Sphinx 搜索引擎的首字母缩略词

c++ - 如何在 Windows 上使用 C++ 访问低级硬件 I/O 函数?

python - 从用户输入中获取矩阵并将其定义为列表列表时遇到问题

c++ - QImage@@QEAA@$$QEAV0@@Z 无法位于 dll 中

c++ - 在 C++ 中使用 new 分配大于 2GB 的单个对象(在 Windows 上)

python - 根据随机数打开唯一的文本文件

html - 为什么 &lt;input&gt; 的行为不像普通的 <div>?

c++ - 从 constexpr 字符串初始化字符数组

ios - 在 iOS 中进行复制拟合的最佳方法?