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);

Recommended Answers

All 2 Replies

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().

problem solved, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.