I have been programming a simple number guessing game and I am wondering if there is any way of making my code more efficient and cleaner. I have spent some time on it implementing error checking to make it as safe as I know how. I feel as if the way's that I have done this may be 'long winded' so if you know of a shorter way to do so then this would be much appreciated.
I do have comments, and I hope they make the program readable!
I am using Visual Studio 2010 - not sure if this changes much.
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
// Include the standard namespace for easy use of cout/cin
using namespace std;
// Function Declarations.
string convertIntToString(int input);
bool isValidInput_Int(string input);
void cls();
void pause();
void printError(int ErrorNumber, bool ClearWindow);
void game(int difficulty);
// Main Function
int _tmain(int argc, _TCHAR* argv[])
{
// Variables
string input;
int inputVal;
bool gameIsRunning = true;
// We want rand() to be as random as possible.
srand( time(NULL) );
// Run until the user wishes to quit.
while(gameIsRunning)
{
cout << "Welcome to the number guessing game V1.0\n\nPlease select an option:\n";
cout << "1- Easy\n2- Medium\n3- Hard\n4- Expert\n5- Exit\n>>";
cin >> input;
// Check if the data is valid according to our wishes.
if(isValidInput_Int(input))
{
// Act correctly according to the input.
// Store the integer we want.
inputVal = atoi(input.c_str());
// Make sure the input is a correct selection
if(inputVal < 5 && inputVal > 0)
{
// Run the game.
game(inputVal);
}
else if(inputVal == 5)
{
// Quit the application.
exit(0);
}
else
{
printError(2, true);
}
}
else
{
printError(1, true);
}
}
// Leave the Application.
return 0;
}
///--------------------------------------
/// Resource:
/// http://www.cplusplus.com/forum/beginner/7777/
/// Converts an integer to a string.
///--------------------------------------
string convertIntToString(int input)
{
stringstream ss; //create a stringstream
ss << input; //add number to the stream
return ss.str(); //return a string with the contents of the stream
}
///--------------------------------------
/// Checks if the input is of size 1 and
/// The values inside this are of type
/// Integer and nothing else.
///--------------------------------------
bool isValidInput_Int(string input)
{
bool retVal = 0;
try
{
if(atoi(input.c_str()))
retVal = 1;
}
catch(exception)
{
retVal = 0;
}
return retVal;
}
///--------------------------------------
/// Clears the console window of text.
///--------------------------------------
void cls()
{
system("cls");
return;
}
///--------------------------------------
/// Pauses the console window.
///--------------------------------------
void pause()
{
system("pause");
return;
}
///--------------------------------------
/// Shows an error based on the input to
/// The function. If your error is not
/// Listed then put '0' in as a default
/// value. This returns a some-what
/// Generic message to the use.
///--------------------------------------
void printError(int ErrorNumber, bool ClearWindow)
{
// Clear the console window
if(ClearWindow)
cls();
switch(ErrorNumber)
{
case 1:
cout << "---Error: Invalid Input---\n\n";
break;
case 2:
cout << "---Error: Please enter a correct option---\n\n";
break;
default:
cout << "---Error: Something went wrong---\n\n";
break;
}
return;
}
///--------------------------------------
/// This is where the 'game' code is kept
///--------------------------------------
void game(int difficulty)
{
// Variables
string input;
string message;
int inputVal;
// Generate our random number!
int numberToGuess = rand() % ((difficulty * 2) * 10) + 1;
cout << "Welcome!\n";
cout << "You have 10 lives.\nThe number is between 1 and " << (difficulty * 2) * 10 << "\n";
cout << "Start guessing!\n\n";
// Loop until they are dead or they guess the number.
for(int lives = 10;lives > 0; lives--)
{
cout << ">>";
cin >> input;
// Check if the data is valid according to our wishes.
if(isValidInput_Int(input))
{
// Store the integer we want.
inputVal = atoi(input.c_str());
// Check if the guess was correct or not
// If it isnt, give them some 'guidance'.
if(inputVal == numberToGuess)
{
message = "Congratulations!\nYou Win!\n";
lives -= 10;
}
if(lives != 1)
{
if(inputVal > numberToGuess)
{
message = "Incorrect, try guessing lower!\n";
}
if(inputVal < numberToGuess)
{
message = "Incorrect, try guessing higher!\n";
}
}
else
{
message = "Incorrect, the number was: [" + convertIntToString(numberToGuess) + "]\n";
}
cout << message;
}
else
{
// Let them know that there input was invalid.
printError(1, false);
// We could 'punish' however, we will be nice.
lives++;
}
}
// Pause.
pause();
// Clear the screen before showing the menu again.
cls();
return;
}