I changed your code so it is easier to read. Unless your intention is that you want no one to read your code. I you are writing the code to benefit the compiler there is to much white space.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void play();
void about();
void howToPlay();
void createAccount();
int main()
{
int choice;
system("Color 1A");
system("title The Currency Game"); // <--- Not sure what this is for or does. It may not even work.
cout <<
"Hello world!\n""Main Menu\n""-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n""1. Play\n""2. Create An Account\n""3. How To Play\n""4. About The Game(And Game Developer)\n""5. Quit the program\n";
cin >> choice;
switch (choice)
{
case 1:
play();
break;
case 2:
createAccount();
break;
case 3:
howToPlay();
break;
case 4:
about();
break;
case 5:
return 0;
}
return 0; // <--- Not required, but makes a good break point.
}
void createAccount()
{
string username;
string password;
longint money{};
ofstream createAcc("createAcc.txt", ios::app);
system("CLS");
cout << " Create an account : ";
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
cout << "Enter A Username: ";
cin >> username;
//cin.get(); // <--- This is is not needed.
cout << "Enter A Password: ";
cin >> password;
//cin.get(); // <--- This is is not needed.
createAcc << username << " " << password << " " << money << endl;
createAcc.close();
system("CLS");
//main(); // <--- Return to main. DO NOT call "main".
}
void play()
{
system("CLS");
string us1;
string pass1;
longint money;
string password;
string username;
ifstream createAcc("createAcc.txt");
// <--- But how do you know that the file is open and usable?
cout <<
"Welcome To The Game !\n""-=-=-=-=-=-=-=-=-=-=-=-=-\n""First, log in : \n""Enter Your Username: ";
cin >> us1;
//cin.get(); // <--- This is is not needed.
cout << "Enter Your Password: ";
cin >> pass1;
//cin.get(); // <--- This is is not needed.
while (createAcc >> username >> password >> money)
{
if (us1 == username && pass1 == password)
{
cout << "Welcome, " << us1 << '\n';
}
elseif (!(us1 == username && pass1 == password))
{
cout << "Unknown Account !\n";
}
}
}
void howToPlay()
{
}
void about()
{
system("CLS");
}
What if statement? Do you mean the switch?
When I run the program and choose 1 it appears to be working. What I am not sure about is where "us1" and "pass1" are getting their values from. If that is from reading a file I think you have missed.
What would be better is to read the file and create a vector or a map so that it can be used more than once. Then if you add to this you can write it to the input file for next time.
Since you open a file and do not check it the program continues even if it can not read anything from the file.
The while loop on line 112 is questionable. It took several times B4 I understood that "createAcc" is a file stream. "inFileAcc" would be much better at describing the name of the file stream and much harder to misinterpret.
It took awhile to set up something to test with, but I finally got it.
After testing the program a few times I realized that the if/else works just fine. It is the while loop that is the problem.
You wrote: ifstream createAcc("createAcc.txt");, but you did not check if the file stream is open and usable. The while loop is telling you that there is a problem because it evaluates to false and you by pass the while loop never reaching the if/else because the while loop can not read the file.
The other thing ablout the if/else is that after it prints the message the function ends returning to main where I set it up to clear the screen B4 the main menu is printed again.
Other problems I found in "main" is if you enter something that is not a number. "cin" will fail and cause an endless loop. The other part is if you enter a number outside of the menu choices there is no way of knowing that a wrong choice was maid. The switch need the "default" case to cover this.
The function "play" is a bad name. There is nothing in the function to play anything. Since all you do there is login the function would be better named as "LogIn" or something similar the describes what the function does. Save "play" for the function that actually plays something.
While working up a response I see that seeplus has shortened up your code.
Now the question is which code do you want to use?
For now I have the main menu working like this:
Hello world
Main Menu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1. Log In
2. Create An Account
3. How To Play
4. About The Game(And Game Developer)
5. Quit the program
Enter Choice: a <---
Invalid choice! Must be a number.
Hello world
Main Menu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1. Log In
2. Create An Account
3. How To Play
4. About The Game(And Game Developer)
5. Quit the program
Enter Choice: 0 <---
Invalid choice!
Hello world
Main Menu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1. Log In
2. Create An Account
3. How To Play
4. About The Game(And Game Developer)
5. Quit the program
Enter Choice: 9 <---
Invalid choice!
Then after the login it would look like this:
Hello world <--- First time through.
Main Menu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1. Log In
2. Create An Account
3. How To Play
4. About The Game(And Game Developer)
5. Quit the program
Enter Choice:
Hello JohnD <--- After the log in.
Main Menu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1. Log In
2. Create An Account
3. How To Play
4. About The Game(And Game Developer)
5. Quit the program
Enter Choice:
Little things, but they make a difference. And easy to add.