Sign in to follow this  
Dragonsoulj

C++ Input Help

Recommended Posts

Here is my game if you want to see what I am talking about: Guess The Number This is for the Windows Console with C plus plus. I have a problem with the input. The game asks for a number, and if a character (letter or symbol) is typed and the player presses ENTER, the game will repeat the last bit of code put on the screen over and over and the player has to close the program to stop the repeating. The problem is that the input type is int, and it doesn't accept characters. The program is supposed to take an integer the player inputs and compare it with another integer, but if the inputted value was a character, the input end of the comparison is messed up. Is there any way to have the input as a char or str and compare it with an integer the same way you can compare an int with an int? (i.e. int x; x <=100) Dragonsoulj

Share this post


Link to post
Share on other sites
The return value of the >> operator actually enables you to check for invalid input. For example:


int value;

if (cin >> value)
{
// Input is a valid integer
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by anothrguitarist
The Cpp faq lite holds the answer to your problem.

And you can use boost to cast a string to an int.


is there a similar website for other languages? (i mean similar to cpp-faq-lite)


If I said no, what would you deduce about C++? ;) (I honestly can't think of anything that's quite analogous, though.)

Share this post


Link to post
Share on other sites
Well, thanks for the help everyone gave. It is completely finished, and I am happy with the result. This is my first game, and with knowing only some C++ programming, I got through it. I was using it as practice for what I was learning.

Download the final version here:Click Here

Thanks again,
Dragonsoulj

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this