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;
}
Is there anyone here with some valuable information that might help me to continue my journey?