Chris De Bow
Chris De Bow

Reputation: 775

problems with using the "cin" object

in my final project, a poker and black jack simulator, my main function doesn't appear to want "cin" to work. The entire project is syntax error free, so that's not a problem, but in order to test to see if it runs or not, I need "cin" to work. The following is my main function, where I seem to be having the trouble:

#include <iostream>

using namespace std;

#include "card.h"

#include "poker.h"

#include "blackJack.h"





void handlePoker(int&);

void handleBlackJack(int&);

//TO DO:

//FIX CIN PROBLEM

//*pulls hair out*





//main function:

//asks the user what game they want to play

//then calls a function for the appropriate

//game chosen

int main()

{   //two choices:

    //one for quitting the program

    //the other for whichever game they want

    char yesOrNo;

char choice;

int totalMoney;

cout<< "please enter a starting amount to bet with"<<endl;

cin>> totalMoney;

do{

    //ask the user which game they want

    cout<<"would you like to play poker or black jack?"<<endl;

    cout<<"input '1' for poker and '0' for blackjack"<<endl;

    cin>>choice;



    if(choice == '1')

    {

        handlePoker(totalMoney);

    }

    else if(choice == '0')

    {

        handleBlackJack(totalMoney);

    }

    else

    {

        cout<<"I'm sorry, the input you entered was invalid"<<endl;

        cout<<"please try again"<<endl;

    }

    cout<<"would you like to try again?"<<endl;

    cout<<"('y' for yes, or 'n' for no)"<<endl<<endl;

    cin>>yesOrNo;

}while(yesOrNo == 'y' || yesOrNo == 'Y');

}

Everytime I use "cin" in the do while loop, it won't let me input anything. The program never stops for user input. as it is not, he's the program's output:

"please enter a starting amount to bet with"
*user can enter starting amount here, this "cin" works no problem*
"input '1' for poker and '0' for black jack"
"I'm sorry, the input you entered was invalid"
"please try again"
"would you like to try again?"
"('y' for yes, or 'n' for no)

then the program ends, because none of the choices have any value in them. I'll include more code if asked, but I believe this is where the problem with it lies. thank you to all who may help me!

edit: The program does enter the do while loop, and all messages are printed as they should bee, but the program won't let me input any data at all, it doesn't stop to let me input data, it just acts as if they do not exist.

Upvotes: 4

Views: 1122

Answers (2)

Mooing Duck
Mooing Duck

Reputation: 66912

Usually when I see cin ignoring everything and putting me in an infinite loop, that's caused when I enter in invalid input, and never clear the stream state. Namely, entering "A" for a number will do that. The idiomatic way to do this would be something like

while (! (cin >> totalMoney)) {
    cout<<"I'm sorry, the input you entered was invalid"<<endl;
    cout<<"please try again"<<endl;
    cin.clear(); //important
} 
// totalMoney holds a valid value now

Upvotes: 4

Pramod
Pramod

Reputation: 5208

Try using cin.ignore() instead of just cin for the process to wait for user input

cin>>choice;
cin.ignore();

Check out this link on MSDN for more info

Upvotes: 0

Related Questions