Вопрос C ++ для начинающих по текстовым файлам ввода / вывода. - PullRequest
0 голосов
/ 22 февраля 2010

Если у меня есть входной текст с единственной вещью, написанной «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;

}

Ответы [ 2 ]

1 голос
/ 22 февраля 2010

Вместо объявления character как string, объявите его как char. string относится к std::string, высокоуровневому объекту для хранения нескольких символов. char является нативным нативным типом, который хранит ровно 1 символ.

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

ввод Z или независимо от букв.

1 голос
/ 22 февраля 2010
char c = 'A';
char next_one = c+1;

Это дает вам следующий символ ('B' здесь), предполагая ASCII. Поскольку это, кажется, домашнее задание, остальную часть работы вы должны выполнять самостоятельно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...