0

The do...while loop works fine when i key in only single character, however if i key in multiple characters, the default statement is executed infinitely. I think the problem lies in cin >> int?, i need some help in modifying the code, thx

bool rightanswer = true;
		do
		{
			int choice;
			cout << "Gender: \n"  
				 <<	"1 - Male \n"
				 <<	"2 - Female" << endl;
			cin >> choice;
			fflush(stdin);
			switch (choice)
			{
			case 1:
				gender = "Male";
				break;
			case 2:
				gender = "Female";
				break;
			default:
				cout << "Wrong selection, please try again" << endl;
				rightanswer = false;
			}
		}while (rightanswer == false);

Edited by Qmage: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Qmage
0

1> Once the default case is invoked, the flag rightanswer is set to false and it is never reset to true so that the loop ends.

2> U are trying to assign a string using = operator. Use strcpy().

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.