H3XXX
H3XXX

Reputation: 647

C++ Why does this variable return a random value?

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

Upvotes: 4

Views: 1272

Answers (6)

Andy Prowl
Andy Prowl

Reputation: 126432

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

Upvotes: 9

Void - Othman
Void - Othman

Reputation: 3481

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.

Upvotes: 1

rerun
rerun

Reputation: 25495

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;

Upvotes: 2

Shriram Shrikumar
Shriram Shrikumar

Reputation: 1155

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

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227390

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;

Upvotes: 4

Pete Becker
Pete Becker

Reputation: 76245

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

Upvotes: 3

Related Questions