3

I have made a basic "guess my number game" in c++, and I have added a variable for how many guesses it took the person to guess the number. However when the game ends, a random value for the guesses is shown. Something like 1980046322. I can't figure out why it does this, considering that I haven't put anything extraordinary into my code.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int randomNumber;
int guess;
int guesses;

srand((unsigned)time(0));
randomNumber = (rand()%10)+1;

cout << "I am thinking of a number between 1 to 10. Can you guess it?" << endl;

while(guess != randomNumber){
    cout << "That is not correct, try again.";
    guesses++;
    cin >> guess;
}

if(guess == randomNumber){
    cout << "Good Job. Guesses: " << guesses << endl;
    guesses++;
}

return 0;
}
H3XXX
  • 595
  • 1
  • 9
  • 22

6 Answers6

9

Per Paragraph 8.5/11 of the C++11 Standard:

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. —end note ]

Paragraph 8.5/6 explains what default-initialized means for different variable types:

To default-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

otherwise, no initialization is performed.

Since T is neither a class type nor an array type in your case (all your default-initialized variables have type int), no initialization is performed and the initial value is indeterminate.

Moreover, your program has Undefined Behavior, since it tries to reads the value of an uninitialized variable. Doing so necessitates an lvalue-to-rvalue conversion (although the Standard does not specify this explicitly, requiring so is likely to be intended, see this Q&A on SO), and Paragraph 4/1 of the C++11 Standard mandates:

A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue.53 If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the prvalue is the cv-unqualified version of T. Otherwise, the type of the prvalue is T.54

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
4

You have not initialized some variables that you then use before assigning any value to them. They start with an unspecified value, which looks random. You can fix it like this

int guess = 0;
int guesses = 0;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

The code doesn't initialize guess or guesses, so their initial values are unpredictable.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

You don't initialise guesses to 0. It will therefore contain whatever was in the memory address that it gets to start with

drone.ah
  • 1,135
  • 14
  • 28
2

You do not initialize guesses and therefore you have no idea what the value will be. C/C++ doesn't make any guarantee about what he value of an uninitialized variable is.

It is good practice to always initialize all values

int guesses = 0;

rerun
  • 25,014
  • 6
  • 48
  • 78
  • He ought to initialize `guess` in some fashion, or his `while` loop may fail before he even reads a single value. Perhaps with a `cin >> guess;` before the loop. – Nik Bougalis Feb 22 '13 at 18:10
  • yes he should also do a do{} while. Just wanted to point out the specific issue with that one variable. – rerun Feb 22 '13 at 18:18
1

You didn't initialize your guesses variable to zero.

If you compile your code with g++, for example, with the following flags the problem becomes obvious:

$ g++ -O2 -Wall foo.cpp 
foo.cpp: In function ‘int main()’:
foo.cpp:25:38: warning: ‘guesses’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Note that the optimization flag was needed in addition to the warning flag to trigger the warning, at least with g++ 4.7.2.

Moral of the story: enable compiler warnings.

Void - Othman
  • 3,441
  • 18
  • 18