2010-02-21 78 views
1

如果我有一個輸入文本,其中唯一寫的是「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

請不要使用「char」作爲變量名稱。這是一個關鍵字。 – Dave

+1

要格式化代碼,請選擇它並使用帶有101010模式的按鈕。如果您只想格式化一個像「this」這樣的語句塊,請使用單引號。 –

+0

請重新格式化您的代碼。 – cpx

回答

1

而不是聲明character作爲string,聲明它爲charstringstd::string,一個用於存儲多個字符的高級對象。 char是一個低級本地類型,它只能存儲1個字符。

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

輸入Z或字母儘管如此。

+0

謝謝,但現在我的outdata讀作「在ASCII集A後面的字符是66」 – Sagistic

+0

對不起,修正了。問題是'1'的類型是'int',而'int'是比'char'更大的類型,當你添加不同類型的數字時,結果是更大的類型,因此它會打印' B'作爲'int'。 'outfile << ++ character;'也會打印正確的東西,但會修改'character'。 – Potatoswatter

+0

謝謝!!!!!!! WOOHOOOO!我被耽擱了很長時間,我的教科書並沒有真正解決它。真誠的感謝。 – Sagistic

1
char c = 'A'; 
char next_one = c+1; 

這給你的下一個字符( 'B' 在這裏),假設ASCII。由於這似乎是功課,你需要自己完成剩下的工作。

+0

你能再幫我一次嗎? – Sagistic

+0

@user 278330:你需要什麼幫助? – Javier

+0

我試圖讓它在最後,代碼讀取文本文件(其中包含字母A),然後將顯示下一個字符(所以如果我把C而不是A放在文本文件中,它將顯示D) – Sagistic