Hello I am having issues with this program and am not too sure what I am doing wrong. The programs ask the user for their name, and if the name does not yet exist as a file, create it. Then it prompts the user to enter their first and last name and their balance. When the user runs the program again, their name and info should be saved in an output file and should greet the user and display their balance.
std::ofstream does not have a >> operator. Think of the direction of the arrows as being the direction that the information is flowing. outputFile >> balance; implies that the information is flowing from the outputFile to the balance. This is incorrect.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
string name;
string fullname;
int balance;
cout << "Enter your first name." << endl;
cin >> name;
name = name + ".txt";
ifstream myFile(name);
if (myFile.good())
{
cout << "Welcome back " << fullname << endl;
cout << "Here is a log of all your BALANCE acitivity." << endl;
cout << "Your balance is " << balance << endl;
return 1;
}
ofstream outputfile(name);
cout << "Enter your FIRST and LAST name." << endl;
std::cin.ignore(); // ignore the remnant newline from the previous cin >> call.
getline(std::cin, fullname);
cout << "Hello " << fullname << endl;
cout << "Enter your initial balance." << endl;
cin >> balance;
outputfile << balance;
return 0;
}
Edit: You still have a small bug, though.
D:\code\cplusplus232844>main
Enter your first name.
hello
Enter your FIRST and LAST name.
hello there
Hello hello there
Enter your initial balance.
350
D:\code\cplusplus232844>main
Enter your first name.
hello
Welcome back
Here is a log of all your BALANCE acitivity.
Your balance is 32761
Try to figure out why. Hint: What is the value of balance?