Need Help with File I/O

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.

Any help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace 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;
exit(1);
}


ofstream outputfile(name);


cout << "Enter your FIRST and LAST name." << endl;
getline(outputfile,fullname);
cout << "Hello " << fullname << endl;
cout << "Enter your initial balance." << endl;
outputfile >> balance;






return 0;
}
getline is for reading, not writing.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>
#include <fstream>
using namespace 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?
Last edited on
Thank you for the help, but I am still a bit confused.

cout << "Welcome back " << fullname << endl;

How would I get the fullname to display in this line?

Edit: Nevermind, just got it to work!
Last edited on
Topic archived. No new replies allowed.