1

I'm writing a basic random number guessing game, and I'm trying to perfect it a bit when it comes to entering illegal characters, and as long as numbers outside of the range 1-100 are entered the program tells the user and the user gets to redo it, same goes with letters. However, if you enter 23x5 you end up getting double error messages, you get both the letter and a too high/too low depending on the random number. How do I sort it out so that this entry would go under the letter error message as well?

Here's my code: Header.h

#ifndef HEADER_H
#define HEADER_H

int nGuessedNumber;
int nNumberOfGuesses = 1;
int nRandomNumber;

int UserInput();


#endif

Source.cpp

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

extern int nGuessedNumber;

int UserInput()
{
    while(!(cin >> nGuessedNumber))
        {
            cin.clear();
            while(cin.get() != '\n'){}

            cout << "I asked for a number between 1 and 100.\n";
        }
    return nGuessedNumber;
}

main.cpp

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Header.h"
using namespace std;

int main()
{
    srand(time(0));
    nRandomNumber = rand() % 100 + 1;// sets random number between 1 and 100

    cout << "Guess a number from 1 too 100: " << endl;
    UserInput();

    while (nGuessedNumber != nRandomNumber)
    {
        if ((nGuessedNumber < 1) || (nGuessedNumber > 100))
        {
            cout << "Oi! Between 1 and 100!\n";
            UserInput();
        }
        else
        {
            if (nGuessedNumber < nRandomNumber)
            {
                for (nGuessedNumber; nGuessedNumber < nRandomNumber; nNumberOfGuesses++)
                {
                    cout << "Too low, try again!" <<endl;
                    UserInput();
                }
            }
            else if (nGuessedNumber > nRandomNumber)
            {
                for (nGuessedNumber; nGuessedNumber > nRandomNumber; nNumberOfGuesses++)
                {
                    cout << "Too high, try again!"<< endl;
                    UserInput();
                }
            }
        }
    }
    if (nGuessedNumber == nRandomNumber)
    {
        cout << "Congratulations! " << nGuessedNumber << " is correct!" << endl;
        cout << "You guessed " << nNumberOfGuesses << " times." << endl;
    }
    system("PAUSE");
    return 0;
}
paddy
  • 52,396
  • 6
  • 51
  • 93
Jon
  • 55
  • 1
  • 7
  • Just wondering - is there any reason to use C++ for this solution? – Display Name May 19 '13 at 23:04
  • BTW `Header.h` must contain declarations, and some `.cpp` file must contain definitions, not the other way around ([link](http://stackoverflow.com/a/1410632/509868)) – anatolyg May 19 '13 at 23:51

1 Answers1

2

Read whole lines from std::cin and parse them individually using std::istringstream:

int UserInput()
{
    std::string line;
    while (getline(std::cin, line)) {
        std::istringstream is(line);
        if (is >> nGuessedNumber) {
            ...
            return nGuessedNumber;
        } else {
            ...
        }
    }
}
anatolyg
  • 23,079
  • 7
  • 51
  • 113
nosid
  • 45,370
  • 13
  • 104
  • 134