Hello, I'm having a little trouble with this checkbook program and taking the string from the text file and assigning the string to an int and performing a mathematical operation. My code is here, it compiles and runs fine, it just displays an incorrect value for the remaining balance. I'm just hoping someone here can put me on the right track.
Thank you.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
string file;
void line();
double balance;
double d, m;
int main()
{
cout << "Enter a file name (including extension): " << endl;
cin >> skipws >> file;
ifstream input(file);
if (!input.good())
{
cerr << "Unable to open file" << endl;
exit(1);
}
line();
while (!input.eof())
{
char deposit[15];
input.getline(deposit, 15, ':');
cout << left << setw(15) << deposit;
d = atof(deposit);
char date[15];
input.getline(date, 15, ':');
cout << setw(15) << date;
char place[15];
input.getline(place, 15, ':');
cout << setw(15) << place;
char money[15];
input.getline(money, 15);
cout << right << setw(15) << "$ " << money << right << endl;
m = atof(money);
balance = d - m;
}
line();
cout << right << setw(55) << "Balance : " << " $ " << balance << right << endl;
return 0;
}
void line()
{
for (int j = 0; j < 65; j++)
cout << '-';
cout << endl;
}
Oh I almost forgot the text I'm using for this program.
deposit:July 7:-:300
416:July 8:Albertsons:15.85
417:7/9:Checker Auto:19.95
418:7/10:Super Target:47.50
419:Dec 5:Home Depot:47.89
Thanks again.
balance = d - m; Here you are deducting some amount from... deposit number?
Also only last calculation of balance would be saved.
I believe you intended to do balance -= m, but you still lack initial balance amount.