1. I need to read from the file 2. output whats on the file and the total of the
ID: 3657199 • Letter: 1
Question
1. I need to read from the file
2. output whats on the file and the total of the file
3. write to the file and total the new data(number)
file name: file
Tony Gaddis
05/24/12 100
05/30/12 300
07/01/12 -300
--------------------
accountInfo.open("file", ios::in | ios::out | ios ::app)
accountInfo >> firstName;
cout << firstName << " ";
accountInfo >> lastName;
cout << lastName << endl;
while (accountInfo >> date)
{
accountInfo >> amount;
cout << date << " ";
cout << amount << endl;
}
total = amount;
cout << "the total is" << total << endl;
accountInfo.close();
//adding new entry
accountInfo.open (cID.c_str(), ios::in | ios::out | ios ::app);
cout << "Enter the amount you will like to deposit. ";
cin >> amount;
cout << "Enter the date you made the deposit. [DD/MM/YR] ";
cin >> date;
accountInfo << endl << date << " " << amount;
cout << "Would you like to add a new entry? [Y/N]";
cin >> choice;
accountInfo.close();
my main problem is I don't know how to get the number from the file and add them. i know how to write to the file but how would i also total the new entry (number) that was added.
Explanation / Answer
//set total to 0 first //and then add amount to total every time amount is adjusted/read from file or input #include #include #include using namespace std; int main(){ //declare variables string firstName,lastName; string date; double amount,total=0; char choice; fstream accountInfo; accountInfo.open("file", ios::in | ios::out | ios ::app); accountInfo >> firstName; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.