Help about while-do?!

Pls check this 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
#include<string>
#include<iostream>

using namespace std;

char menu();

void main()
{
char choice='0';

while (choice=='0')
{
choice=menu();
}
while (choice=='y')
{
choice=menu();
}while (choice=='t')
{
	cout <<"If I press 'y' now it goes out, why?!"<<endl;
choice=menu();
}
}

char menu()
{
char x;
cout << "Hello World!" << endl;
cout << endl;

cout << "Do again? (y/n)" << endl;
cout << "- ";
cin >> x;
cout << endl;
return(x);
}


If you press 'y' hundreds of times the program works fine..and if you press 't' hundres og times the program works fine..BUT after you've pressed 't' once and you press 'y' after that, then the program break down! why?.. I find it hard to understand.. please help.. and what's the solution, if I want to switch between pressing 'y' and 't' hundreds of times?

best regards
Malik
It is because of the while loop structure...if you press 'y' a bunch it will work, then when you press 't' it exits the 'y' loop and enters the 't' loop, then if you press something other then 't', if exits the t loop and leaves main().

Also, indent your code a little so it is easier to see...
1
2
3
4
5
6
//Good
char menu()
{
    cout<<"Hello World!"<<endl;
    ...
}
Because you forgot to remember that the code runs sequentially.

Let me walk you through your code to see why it did not turn out the way you expect it to be.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//This is your main
//All the values are assumed to be set within the menu() function
char choice='0';

while (choice=='0')
{
    choice=menu();  //Any values entered aside from '0' will exit this loop.
}
while (choice=='y')
{
    choice=menu(); //The program loops here when you enter 'y' lots of times.
}
while (choice=='t')
{
	cout <<"If I press 'y' now it goes out, why?!"<<endl;
    //Because the loop checks, if choice is 't'. Since you enter 'y', 'y' == 't' is false. So the loop
    //exits.
    choice=menu();
}
Okay I see.

The problem is, that I have i menu with 6 options.. and after I chose an option I would like to get back to the menu.

Can any of you guys help me out or give me a hint? - WITHOUT using goto-statements ;)

Best regards
Malik, DK
Last edited on
Try a switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        bool play = true;
	int number = 0;
	while(play)
	{
		cout << " Lets see how smart you are. Enter a number between 1 and 6, 0(zero) to exit.\n";
		cin >> number;
		switch(number)
		{
		case 1:
			cout << "1? ok...\n";
			break;
		case 2:
			cout << "hmm 2? not bad.\n";
			break;
		//case 3: case 4: case 5: case 6:
		case 0:
			cout << "bye!\n";
			play = false;
			break;
		default:
			cout << "FOOL! i said a number between 1 and 6, not: " << number << " geez...\n";
			break;
		}//end of switch
	}//end of while 


more info on Control Structures:
http://www.cplusplus.com/doc/tutorial/control.html

cheers

Jeff
Last edited on
Thanks alot :) I got it..
Topic archived. No new replies allowed.