So before I get started, I will state this is part of my homework. I have to ask the user to input information that will get inserted into a text file in which it will be presented back to the user. The issue I'm having is that one of the variable outputs must be an addition of itself.
Example, lets say the problem is, creating a small money management account.
We need to insert the amount of money we had before purchase, after purchase, and total, as well as other forms of information. I'll post the snippet I've come up with so far.
I've inserted the code. My question is this, how can I have it add the second column (Cost) from every input we give it? Its not looping right now but ideally I want every time I insert a cost, at the end it will add all the
COST
from all the
accountCost
inserted.
Any help is appreciated, even if you just give me a link elsewhere, it is appreciated.
PS. My professor wants the use of CSTRINGS not C++ Strings, hence why im using char aStringVar[SIZE] = ""; over a typical std::string aStringVar[SIZE] = "";
What I have tried:
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE = 256;
int main(void){
ifstream readFile;
ofstream writeFile;
char accountName[SIZE] = "";
char cost[SIZE] = "";
char Total[SIZE] = "";
char Tax[SIZE] = "";
int accountCost = 0;
int accountTotal = 0;
int accountTax = 0;
writeFile.open("bank.txt", ios::app | ios::ate);
writeFile << accountName << "," << Cost << "," << Total << "," << Tax << "\n";
writeFile.close();
readFile.open("bank.txt");
readFile.getline(accountName, SIZE, ',');
readFile.getline(Cost, SIZE, ',');
readFile.getline(Total, SIZE, ',');
readFile.getline(Tax, SIZE);
accountCost = atoi(Cost);
accountTotal = atoi(Total);
accountTax = atoi(Tax);
cout << "Account Cost: " << accountCost << endl;
cout << "Account Total: " << accountTotal << endl;
cout << "Account Tax: " << accountTax << endl;
}