void play if statement not working | login/register system

Hi there, so when you run this code it shows me all the possible options for the if statement at the void play(), why is it doing that ?

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>

using namespace std;

void play();
void about();
void howToPlay();
void createAccount();

int main()
{
    int choice;
        system("Color 1A");
    system("title The Currency Game");
    cout << "Hello world!" << endl;
    cout << "Main Menu"<<endl;
     cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
      cout << "1. Play"<<endl;
       cout << "2. Create An Account"<<endl;
     cout << "3. How To Play "<<endl;
      cout << "4. About The Game(And Game Developer)"<<endl;
      cout << "5. Quit the program"<<endl;
      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;
}
void createAccount(){

string username;
string password;
long int money;
 ofstream createAcc("createAcc.txt", ios::app);
system("CLS");
cout <<" Create an account : " <<endl;
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
cout << "Enter A Username: "<<endl;
cin>> username;
cin.get();
cout << "Enter A Password: "<<endl;
cin >> password;
cin.get();
createAcc<< username<<" "<< password<<" "<< money<<endl;
createAcc.close();
system("CLS");
main();
}


void play(){
system("CLS");
string us1;
string pass1;
long int money;
string password;
string username;
 ifstream createAcc("createAcc.txt");
 cout << "Welcome To The Game !" <<endl;
 cout << "-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
 cout << "First, log in : "<<endl;
 cout << "Enter Your Username: "<<endl;
 cin >> us1 ;
 cin.get();
cout << "Enter Your Password: "<<endl;
cin >> pass1;
cin.get();
while(createAcc >> username >> password >> money){
if(us1==username && pass1 ==password){
cout << "Welcome, "<< us1<<endl;
}else if(!(us1==username && pass1 ==password)){
 cout << "Unknown Account !" <<endl;
}
}
}
void howToPlay(){


}
void about(){
system("CLS");

}
Hello lazylife,

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.

Be sure to read the comments I put in the code.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <fstream>

using namespace 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;
    long int 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;
    long int 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';
        }
        else if (!(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.

Andy

Hello lazylife,

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.

Andy
Hello lazylife,

Sorry I keep forgetting this part.

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.

Andy
Thank you for "cleaning" my code, but even so, how am I supposed to fix this ?
Perhaps something like this:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void play();
void about();
void howToPlay();
void createAccount();

int main()
{
	//system("Color 1A");
	//system("title The Currency Game");

	cout << "Hello world!\n";

	do {
		int choice {};

		cout << "Main Menu\n";
		cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
		cout << "1. Play\n";
		cout << "2. Create An Account\n";
		cout << "3. How To Play\n";
		cout << "4. About The Game(And Game Developer)\n";
		cout << "5. Quit the program\n";
		cout << "\nEnter option: ";
		cin >> choice;

		switch (choice) {
			case 1:
				play();
				break;

			case 2:
				createAccount();
				break;

			case 3:
				howToPlay();
				break;

			case 4:
				about();
				break;

			case 5:
				return 0;

			default:
				cout << "Unknown menu choice\n";
				break;
		}
	} while (true);

	return 0;
}

void createAccount()
{
	ofstream createAcc("createAcc.txt", ios::app);

	if (createAcc) {
		string username;
		string password;
		long int money {};

		//system("CLS");

		cout << " Create an account:\n";
		cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";

		cout << "Enter A Username: ";
		cin >> username;

		cout << "Enter A Password: " << endl;
		cin >> password;

		// MONEY WILL BE 0 HERE!!!
		createAcc << username << " " << password << " " << money << '\n';

		//system("CLS");
	} else
		cout << "Cannot open file\n";
}


void play() {
	//system("CLS");

	ifstream createAcc("createAcc.txt");

	if (createAcc) {
		string us1;
		string pass1;
		long int money {};
		string password;
		string username;

		cout << "Welcome To The Game !\n";
		cout << "-=-=-=-=-=-=-=-=-=-=-=-=-\n";
		cout << "First, log in : \n";
		cout << "Enter Your Username: ";
		cin >> us1;

		cout << "Enter Your Password: ";
		cin >> pass1;

		while (createAcc >> username >> password >> money)
			if (us1 == username && pass1 == password) {
				cout << "Welcome, " << us1 << '\n';
				// PLAY CODE GOES HERE
				return;
			}

		cout << "Unknown Account !\n";
	} else
		cout << "Cannot open file\n";
}

void howToPlay() {
}

void about() {
	//system("CLS");
}

Hello lazylife,

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.

Andy
Topic archived. No new replies allowed.