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"
please help me!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

i have heard that i need to "fulsh the input"
please help me!

read this thread

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.

comments like this

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

This article has been dead for over six months. Start a new discussion instead.