``````     do
{
cout << "and how many of the 4 opponents would you like to play aganst?"<<endl;
cin >> numberofopponents;//were the user inputs the number of opponents

if(numberofopponents==1)//the if statment that plays the game if the user choses 1 opponents
{
cout<<"1 opponent......really?????   ...   noob"<<endl;
cout << "round one!"<<endl;
cout << setw(15) << "Place" << setw(15) << "Name" << setw(15) << "Dollars" <<endl;
cout << setw(15) << "1st" << setw(15) << name1 << setw(15) << "100" <<endl;
cout << setw(15) << "1st" << setw(15) << "Jenneifer" << setw(15) << "250" <<endl;
cout<<setw(15)<<"1st"<<setw(15)<<"The House "<<setw(15)<<" so much that god owes us"<<endl;
}
else if(numberofopponents==3)//the if statment that plays the game if the user choses 3 opponents
{
cout<<"3 opponents? daring move old chum daring move indeed"<<endl;

cout <<"round one!"<<endl;
cout << setw(15) << "Place" << setw(15) << "Name" << setw(15) << "Dollars" <<endl;
cout << setw(15) << "1st" << setw(15) << name1 << setw(15) << "100" <<endl;
cout << setw(15) << "1st" << setw(15) << "Joshua" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Simon" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Chang" << setw(15) << "250" <<endl;
cout<<setw(15)<<"1st"<<setw(15)<<"The House "<<setw(15)<<" censored for your protection"<<endl;
}
else if(numberofopponents==2)//the if statment that plays the game if the user choses 2 opponents
{
cout<<"2 opponents it is!"<<endl;
cout <<"round one!"<<endl;
cout << setw(15) << "Place" << setw(15) << "Name" << setw(15) << "Dollars" <<endl;
cout << setw(15) << "1st" << setw(15) << name1 << setw(15) << "100" <<endl;
cout << setw(15) << "1st" << setw(15) << "Timmy" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Greg Ramsay" << setw(15) << "250" <<endl;
cout<<setw(15)<<"1st"<<setw(15)<<"The House "<<setw(15)<<" so much god owes us"<<endl;
}
else if(numberofopponents==4)//the if statment that plays the game if the user choses 4 opponents
{
cout <<"4 opponents you say? how quaint somehow"<<endl;
cout <<"round one!"<<endl;
cout << setw(15) << "Place" << setw(15) << "Name" << setw(15) << "Dollars" <<endl;
cout << setw(15) << "1st" << setw(15) << name1 << setw(15) << "100" <<endl;
cout << setw(15) << "1st" << setw(15) << "Comp1" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Comp2" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Comp3" << setw(15) << "250" <<endl;
cout << setw(15) << "1st" << setw(15) << "Comp4" << setw(15) << "250" <<endl;
cout<<setw(15)<<"1st"<<setw(15)<<"The House "<<setw(15)<<" don't ask"<<endl;
}
else
cout <<" does not freakin compute"<<endl;

} while (numberofopponents<=0 || numberofopponents > 4);
}
``````

i have made a program of blackjack and this piece has been bothersome to me for a while.
every time i try to input a letter into the numberofopponents it gives me the else statement in an infitie loop.
i have heard that i need to "fulsh the input"

Can you actually post your code again, but this time in a way we can read it?

Read this first ...

i have heard that i need to "fulsh the input"

Your coded would look a little better if you used a switch statement instead of all those if statements.

``````cin >> numberofopponents;
switch( numberofopponents )
{
case 1:
// blabla
break;
case 2:
// blabla
break;
default:
cout <<" does not freakin compute"<<endl;
break;

}``````

Simple solution - change your data type for the number of players to be a char - then the input will take it and your else clause will execute, then the loop goes round again.

``//the if statment that plays the game if the user choses 4 opponents``

really serve more to clutter the code than add any useful information. One can see it's an if statement, and your variable name and the test are quite clear as to what's being evaluated.

That's why you don't check a result of an user input in `cin >> numberofopponents` . If you try to input a letter instead of a number then cin stream goes to a failed state and does not change operator >> target. So you will get loop forever because the next input is not performed (failed stream) and so on.

Alway test input result, for example:

``````...
if (!(cin >> numberofopponents)) {
cin.clear(); // reactivate failed cin
cin.ignore(1000,'\n'); // flush keyboard
break; // out of the loop
}
...``````

So it's not a C++ problem: it's your program logic problem ;)...

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.