2010-02-21 1 views
1

"A"로만 작성된 입력 텍스트가 있고 다음 ASCII 세트 (B)를 생성 할 수있는 일련의 코드가 필요한 경우 어떻게해야합니까?C++ 입출력 텍스트 파일에 대한 초급 질문.?

#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

characterstring으로 지정하는 대신 char으로 선언하십시오. string은 복수 문자를 저장하는 상위 수준 개체 인 std::string을 나타냅니다. char은 정확히 1 문자를 저장하는 하위 레벨 기본 유형입니다.

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

입력 또는 문자 외에도.

+0

감사합니다,하지만 지금은 내 outdata는 "아스키 세트에서 A 뒤에 오는 문자는 66입니다" – Sagistic

+0

죄송합니다, 고정. 문제는 '1'은'int' 타입이고'int'는'char'보다 더 큰 타입이고 다른 타입의 숫자를 더할 때 그 결과는 더 큰 타입이므로' B'를 'int'로 간주합니다. 'outfile << ++ character;'는 올바른 것을 인쇄하지만'character'를 수정합니다. – Potatoswatter

+0

감사합니다 !!!!!!! 우 ~! 나는 오랫동안 우연히 목이 마른 상태 였고 교과서는 정말로 끝나지 않았다. 근실 한 감사. – Sagistic

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

이것은 ASCII를 가정하면 다음 문자 ('B'여기를) 제공합니다. 이것이 숙제 인 것처럼 보이므로 나머지 작업은 혼자서해야합니다.

+0

나를 다시 도울 수 있습니까? – Sagistic

+0

@user 278330 : 무엇이 도움이 필요합니까? – Javier

+0

결국 코드가 텍스트 파일 (문자 A 포함)을 읽은 다음 문자를 표시합니다 (텍스트 파일에 A 대신 C를 넣으면 코드가 작성됩니다). D) – Sagistic