Char into Int issue, (to me unexplainable behavior)

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
natox1986
Posts: 53
Joined: December 14th, 2012, 1:11 pm

Char into Int issue, (to me unexplainable behavior)

Post by natox1986 » February 12th, 2013, 9:34 pm

Hey guys what's up,

While most of you are already programming actual games with C++ and DirectX on Chili's framework, I decided to take another route.

I'm currently messing around a lot with console applications and I'm trying to comprehend all the code I write. This means I don't mindlessly copy code from other programs because it works, but I'm trying to write every single piece of code from my own knowledge.
This also means I will stumble onto a lot of 'problems'.
Last night I decided to write a small simple "Guess My Number" game.
Although the code was fairly simple and easy to come up with, I did find something I cannot seem to fix with my current knowledge, nor with search results on Google.
This is why I'm turning to you guys, the Chili Community.

The principle is simple... there is an integer called iGuess, this integer will hold the input value of the player.
The input will be compared to a random generated number within the numeric boundaries.
This comparison happens inside a while loop, while the number isn't equal to the random generated number.
This actually all works fine, but.... the problem lies in the user input.

Whenever the player enters a non-numerical value, the problem will slip into the loop and keep repeating the loop over and over and over until the program crashed. The loop does ask for a new input every time it loops through, but it seems to get ignored because of the non-numerical input.

The following things I have tried but did not work:
- Changed std::cin syntax to scanf("%i",iGuess);
- Filtering out everything non-numeric by restricting input value to numbers from 0 to 10 with an if statement
- Also tried the cin.clear() and cin.ignore() syntax

Source Code:

Code: Select all

#include <iostream>
#include <ctime>
using namespace std;

// PREDEFINED VALUES
#define MIN_NUM 1
#define MAX_NUM 10


int main(){
	// Random Time Seed
	srand(time(NULL));

// GAMELOOP VARIABLE INITIALIZATION
char cAgain = 'y';

while( cAgain == 'y' ){

	// Variables local to main
	int iNumber = 0;
	int iTurns = 1;
	int iGuess = 0;

	// Introduction
	cout << "\n\tWelcome to guess my number!" << endl;
	cout << "\n\tI'm going to pick a number anywhere between " << MIN_NUM << " and " << MAX_NUM << "\n\tand you have to guess what number I picked.\n\tSounds easy? Well let's go then!\n" << endl;

	// Guessing Game PRE-LOOP initialization
	cout << "\tGuess my number: ";
	cin >> iGuess;
	
	// Random Number Input Into iNumber
	iNumber = (rand() % MAX_NUM) + 1;

	// Wrong Answer Loop
	while( iGuess != iNumber ){
			// Higher Or Lower
			if( iGuess < iNumber ){
				cout << "\tThe number you are looking for is higher." << endl;
			} else if ( iGuess > iNumber ){
				cout << "\tThe number you are looking for is lower." << endl;
			}
			// END Higher or Lower
		cout << "\n\tWrong answer, you should try again: ";
		cin >> iGuess;
		// Input Validation
		++iTurns;
	}

	// Wrapping Up End
	if( iGuess == iNumber){
		cout << "\n\tWell done! You guessed it in " << iTurns << " times!" << endl;
		cout << "\n\tPress 'y' to play again or another random character to quit. ";
		cin >> cAgain;
		system("CLS");
	}


// END GAMELOOP
}

	// Aftermath
	EXIT_SUCCESS();
	return 0;
}
Question:
Is there anyone here with some valuable information that might help me to continue my journey?
Image

User avatar
GreatJake
Posts: 169
Joined: January 6th, 2013, 5:13 pm
Location: USA

Re: Char into Int issue, (to me unexplainable behavior)

Post by GreatJake » February 12th, 2013, 11:29 pm

I had that problem before to took me a while to find a strait answer though. There are a few glitches when i tried it in your program but i hope it leads you to a better solution.
http://www.physicsforums.com/showthread.php?t=206539

EDIT: just dont add a !(cin >> iGuess) and you should be good :)

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: Char into Int issue, (to me unexplainable behavior)

Post by indus » February 13th, 2013, 2:35 am

Here Youll find your answer.
Validation and error checking is a must.

Post Reply