return

Hi,
It is a part of my code(it is a game)
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
bool start(void)
{
	
	string option;
	cout<<"\nChoose one of these options:\n\n\n";
	cout<<"[1] start game \n\n";
	cout<<"[2] exit\n";
	cin>>option;

	system("cls");
	if(option!="1"&&option!="2")
	{
		cout<<"\nInvalid choice!Please try again.\n\n";
		cin.clear();
		cin.ignore(1000,'\n');
		start();

	}
		if(option=="1")
			{
				return true;//return to main function

			}
		if(option=="2")
		{
			cout<<"Thank you...";
			cin.ignore(1000,'\n');
			return false; //return to main function
		}
			


}


it has a problem.
if user enter a wrong choice(like "vnkjfnvfj") and then enter 1 program finished.
but if he or she inter 1(without entering wrong choice) program run correctly.
why????
Last edited on
if(option!="1"&&option!="2")

I think you wanted the or operator, ||
Thank you;
but it's not my problem
Then explain your issue better. I can tell you using the wrong logical operator is going to cause you problems.
I would recommend not using recursion in this function, instead investigate the use of a loop like a while or do/while.

use a loop instead of recursion. add print statements and you can follow your code go down the rabbit hole of recursion
@residentbiscuit
input:
"jfndkjv"
output:
Invalid choice!Please try again.
Choose one of these options:";
"[1] start game";
"[2] exit";
input:
"1"
output:
prees any key to continue...


_________________________________________________---


input:
"1"
output:
Enter your choice://I want it for "1" every time
____________________________________________


Did you get it?
Last edited on
How can I use "while"?
I have 3 cases:
for"1"

for"2"

for anything else
because you are using recursion, the first start() executed with the input as "wefwefwefwef" results in option not equal to '1' and options not equal to '2', then instead of executing the "outer start()" you are executing the inner "start ()", giving the input of "1" returns true back to the outer start() and because your outer input for start() was "wefwefwefwef", it does not return true, but nothing as there is nothing being returned for the first if.


Either way stay away from recursion
Last edited on
Thank you.
please explain more about using while in this program.
I don't understand how change the program...
http://www.cplusplus.com/doc/tutorial/control/

Sometimes you have to learn on your own.
while (condition){
//loop untill condition is met
}
Last edited on
@ResidentBiscuit

I know using control structures...!!
The while loop is a control structure, if you knew how to use them then this would a non-issue to you.
apparently not, because you could put those if conditions inside the while loop and break out of it, if the "if" condition is met, meeting your needs
@residentbiscuit
ooh really?
I didn't know while loop is a control structure...!!!
ha ha ha....


@metulburr
Thank you so much...
Last edited on
ResidentBiscuit wrote:
I think you wanted the or operator, ||

No. && is correct.
See for example http://www.cplusplus.com/forum/beginner/89412/#msg480260

Below, I took out the "cls" simply because on my machine, the response is very slow with it there.
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
bool start(void)
{
    string option;

    do {
        cout<<"\nChoose one of these options:\n\n\n";
        cout<<"[1] start game \n\n";
        cout<<"[2] exit\n";
        cin>>option;

        if (option!="1" && option!="2")
        {
            cout<<"\nInvalid choice!Please try again.\n\n";
        }

        cin.ignore(1000,'\n');

    } while (option!="1" && option!="2");

    if(option=="1")
    {
        return true;
    }

    // Option must be "2" when we get here
    cout<<"Thank you...";
    return false;
}

Thank you Chervil.
You always help me...
Topic archived. No new replies allowed.