I have just a couple issues here with my code. It works but I'm not advanced enough to do what I want to yet. Nor do I know how to word it for a google search. I have a Blackjack game that I'm doing and want to know how to edit certain lines of the file depending on user input. Simpler, I want a user to be able to open the game and start with their balance from the last time they were playing(a save and load feature). My issues are the balance and the username are on the same line in the text file (purposefully) and I want to assign the variables to those in the text file. I know I'm on the right track, I just dont know where to go from here. Thanks for the help in advance. If I broke a rule of posting, I'm sorry.
input username
if username is present in file
edit balance on leave
if username isnt present in file
create new user
Here is my code for the load function:
void load(userAcc user1) { ifstream in; in.open("Balances.txt"); if (in.is_open()) { string word; for (int x = 0; in >> word; x++); { user1.name = word; user1.balance = word; } cout << user1.name << endl; cout << user1.balance << endl; in.close(); } else cout << "Cannot open a file"; } void save(userAcc user1) { user1.balance = "1000"; cout << "Enter a username: "; cin >> user1.name; ofstream out; out.open("Balances.txt", ios_base::app); if (out.is_open()) { out << user1.name << " " << user1.balance << endl; out.close(); } else cout << "Cannot open a file"; }
for (int x = 0; in >> word; x++);remove the trailing;. It ends theforwithout entering the loop. The for spins around doing nothing but incrementingxand the following code block will be run exactly once.