1

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"; } 
4
  • In for (int x = 0; in >> word; x++); remove the trailing ;. It ends the for without entering the loop. The for spins around doing nothing but incrementing x and the following code block will be run exactly once. Commented Jul 15, 2021 at 23:48
  • Thank you for that Commented Jul 15, 2021 at 23:51
  • @user4581301 do you mind if I messaged you for some help? I completely understand if you do. Commented Jul 15, 2021 at 23:57
  • Best if I just answer what I think the question is so that it will help more than just you. Private messaging defeats Stack Overflow's goal of creating an online repository of questions and answers. Commented Jul 16, 2021 at 0:22

1 Answer 1

1

In

for (int x = 0; in >> word; x++); 

remove the trailing ;. It ends the statement before the body of the for loop, separating the two. The for spins around doing nothing but reading the file until it ends and incrementing the unused variable x and the following code block will be run exactly once, storing whatever is in word (and since the loop will exit when the read into word fails, what's in word will depend on the C++ Standard version the the compiler's been set to use) into user1.

Once the ; is removed, the for loop will read into word until no more words can be read from the file. Every word read is copied into the same userAcc writing over the previous word. When the file hits the end in >> word will fail and the loop will exit. The last word in the file will then be printed out, all other words having been overwritten.

Naïve fixing of this would look something like

void load(userAcc user1) { ifstream in; in.open("Balances.txt"); if (in.is_open()) { while (in >> user1.name // read name in from file >> user1.balance) // read balance in from file { // loop will exit when it cannot read a name and a balance from the file // for now we're just printing out what's read from the file. cout << user1.name << endl << user1.balance << endl; } // in.close(); not needed. File will automatically close when in goes out of scope. } else cout << "Cannot open a file"; } 

But we probably want to do more than print out all of the users in the file, so let's put them into a convenient resizable container like std::vector.

vector<userAcc> load() // takes no parameters, returns list of accounts { vector<userAcc> accounts; ifstream in; in.open("Balances.txt"); if (in.is_open()) { userAcc user1; // account we can read into while (in >> user1.name >> user1.balance) { accounts.push_back(user1); // store account } } else cout << "Cannot open a file"; return accounts; // hand accounts read back to caller. } 

Use of the function would be something like

vector<userAcc> accounts = load(); 

The save function looks pretty much good-to-go as written.

Sign up to request clarification or add additional context in comments.

1 Comment

Well written and tailored to the level of the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.