HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcppMinor

How can I make a simple number guessing game more efficient?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
cansimplenumbermakemoreefficientgamehowguessing

Problem

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
#include
#include
#include

// 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 >";

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 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;
}

///--------------------------------------

Solution

Here some of my language-focused remarks, not touching the actual program design.

usings

// Include the standard namespace for easy use of cout/cin
using namespace std;


Very nice to list what you need by adding the comment. But instead of pulling all names you could make it more explicit

using std::cin;
using std::cout


In big files you may want to "sectionize" the using by from which #include they came from. (Edit, thanks to comment below) Best do not introduce using before other #includes though, you can group the using afterwards. E.g.:

#include 
#include 
#include 
#include 
// for shorten code
using std::string;
using std::map;
using std::vector;
// from iostreeam
using std::cin;
using std::cout;


I do not do this for all names, only for the most frequent ones when it really saves space -- and most people do know anyway. Most std::-items I write with their namespace when I use them.

string I often apply a using to (lots of strings in function arguments), but map and vector I typically do not (better being explicit at the few location they are used). When I have to use lots of std::vector::const_iterator somewhere I shorten the code with a local typedef or using.

void drawData(std::vector &data) {
    typedef std::vector::const_iterator tit;
    const tit end = data.end();
    for(tit it=data.begin(); it!=end; ++it) {
        *it.drawYourself();
    }
}


And with C++11 you do not even need that, you have auto and ranged-for to beautify your.

variable initialization

// Variables
string input;
int inputVal;
bool gameIsRunning = true;


It is ok not to initialize string, because it is a class/object.

But you should initialize int variables.

latest possible declaration

// Variables
string input;

...
while(...)
   ...
   cin >> input;


Your rule-of-thumb should be to declare a variable as late as possible which would mean

...
while(...)
   ...
   string input;
   cin >> input;


The exception are "tight loops" when the frequent initialization and destruction would cost to much. But actually, string is quite cheap and this is not a "tight loop", so I would recommend it.

Code Snippets

// Include the standard namespace for easy use of cout/cin
using namespace std;
using std::cin;
using std::cout
#include <iostream>
#include <string>
#include <map>
#include <vector>
// for shorten code
using std::string;
using std::map;
using std::vector;
// from iostreeam
using std::cin;
using std::cout;
void drawData(std::vector<thingy> &data) {
    typedef std::vector<thingy>::const_iterator tit;
    const tit end = data.end();
    for(tit it=data.begin(); it!=end; ++it) {
        *it.drawYourself();
    }
}
// Variables
string input;
int inputVal;
bool gameIsRunning = true;

Context

StackExchange Code Review Q#12460, answer score: 8

Revisions (0)

No revisions yet.