Repeating code

Hey, i made a code, and i would like to know how to repeat it without exiting, if i say M stating that i am a male, the script should answer properly, and then repeat, and ask me again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include "iostream"
using namespace std;

int main()
{
	char x;
	cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
	cin >> x;
	switch (x)
	{
		case 'F':
			cout << "I know your gender now: Male" << endl;
			break;
		case 'M':
			cout << "I know your gender now: Female" << endl;
			break;
		default:
			cout << "Reply with M for male and F for female. Your  answer is case sensitive" << endl;
	}
	system("PAUSE");
}
You could use a while or do while loop to repeat while the answer is not 'M' or 'F'.

There's a section on while and do... while loops here.
http://www.cplusplus.com/doc/tutorial/control/

You could also add in support for a lower case character and just prompt for re-entry if they don't enter lower or uppercase M or F.

I think you have the output text reversed in your cases?
Yes, i did have my answers reversed, and i made a loop and now works, but i have another problem: when i add anything instead of M or F, the code submits on my behalf the number times as the number of characters my answer has:
View photo

http://i.imgur.com/3d9LYcl.png


there were submitted for "." on my behalf after i replied with a four letter word. Why did it do that?

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
#include "stdafx.h"
#include "iostream"
using namespace std;

int main()
{
	int rep = 0;
	while (rep == 0)
	{
		char x = 0;
		cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
		cin >> x;
		switch (x)
		{
		case 'F':
			cout << "I know your gender now: Female" << endl;
			break;
		case 'M':
			cout << "I know your gender now: Male" << endl;
			break;
		case 'Q':
			rep = 1;
			break;
		default:
			cout << "Reply with M for male and F for female. Your  answer is case sensitive." << endl;
		}
	}
}
Last edited on
Can you post your current code?
I edited the post above
You're repeating the loop based on rep ==0. rep is still equal to 0 after a valid entry is made, so the loop runs again.
Last edited on
What should i do? If i set rep = 1 in the default case, the program will exit.
You want the loop to exit if they enter F, M or Q (to quit?) - so you would set rep to 1 in those cases so they get prompted to re-enter.

or you could do this without creating another variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	char x;
	
	do 
	{
		cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
		cin >> x;
		switch (x)
		{
    		case 'F':
    			    cout << "I know your gender now: Female" << endl;
    			    break;
    		case 'M':
    			    cout << "I know your gender now: Male" << endl;
    			    break;
    		case 'Q':   
    		            break;
    		default:
    			    cout << "Reply with M for male and F for female. Your  answer is case sensitive." << endl;
		}
	}while (x!='F' && x!='M' && x!='Q');
}
Last edited on
I want it to quit only when i input Q, so i used

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
#include "stdafx.h"
#include "iostream"
using namespace std;

int main()
{
	char x;

	do
	{
		cout << "Are you a male, or female? Reply with M for male and with F for female" << endl;
		cin >> x;
		switch (x)
		{
		case 'F':
			cout << "I know your gender now: Female" << endl;
			break;
		case 'M':
			cout << "I know your gender now: Male" << endl;
			break;
		case 'Q':
			break;
		default:
			cout << "Reply with M for male and F for female. Your  answer is case sensitive." << endl;
		}
	} while (x != 'Q');
}


and the same happens if i input something that is not F, M or Q, the loop repeats the same number of times as the number of characters in my reply. If i reply "test", it repeats 4 times, because there are 4 letters
x is one character so it extracts only one character from the buffer in the case of "test" it extracts the t and places it in x. Now "est" is left in the buffer. There are error flags letter you know that something went wrong you can check with if(!cin) if there are any errors you then may wish to clear the flag with cin.clear(); and then ignore anything that may be left in the buffer (read but don't extract) with cin.ignore(1024, '\n'); where 1024 is an arbitrary number if you wish to ignore everything that could possibly be in the buffer then you would use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); (You would put this stuff in the default case or after getting the input)
Last edited on
I might add a message to the user that they can stop the repeated questioning if they input Q.
Topic archived. No new replies allowed.