【问题标题】:C++ Beginner's question on input/output text files.?C++ 初学者关于输入/输出文本文件的问题。?
【发布时间】:2010-02-21 22:51:36
【问题描述】:

如果我有一个只有“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;

}

【问题讨论】:

  • 请不要使用“char”作为变量名。这是一个关键字。
  • 要格式化代码,选择它并使用带有 101010 模式的按钮。如果您只想格式化 this 这样的句子中的一个块,请使用单反引号。

标签: c++ text input ascii


【解决方案1】:

不要将character 声明为string,而是将其声明为charstring 指的是std::string,一个用于存储多个字符的高级对象。 char 是低级原生类型,仅存储 1 个字符。

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

尽管输入了 Z 或其他字母。

【讨论】:

  • 谢谢,但现在我的输出数据显示为“ASCII 集中 A 之后的字符是 66”
  • 抱歉,已修复。问题是1int 类型,而int 是比char“更大”的类型,当您添加不同类型的数字时,结果是更大的类型,因此它打印@ 987654332@ 作为intoutfile &lt;&lt; ++ character; 也会打印正确的内容,但会修改 character
  • 谢谢!!!!!!!呜呜呜!!我被难住了很长时间,我的教科书并没有真正超越它。最诚挚的感谢。
【解决方案2】:
char c = 'A';
char next_one = c+1;

这给你下一个字符(这里是'B'),假设是ASCII。由于这似乎是家庭作业,您需要自己完成其余的工作。

【讨论】:

  • @user 278330:你需要什么帮助?
  • 我试图让它在最后,代码读取文本文件(其中包含字母 A),然后将显示下一个字符(所以如果我放 C 而不是A 在文本文件中,会显示 D)
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多