when i try to build the solution for this program i get 4 warnings. they are:

c:\documents and settings\mike\my documents\visual studio 2005\projects\practice\practice\practice.cpp(22) : warning C4244: '=' : conversion from 'time_t' to 'int', possible loss of data
c:\documents and settings\mike\my documents\visual studio 2005\projects\practice\practice\practice.cpp(13) : warning C4101: 'ncount' : unreferenced local variable
c:\documents and settings\mike\my documents\visual studio 2005\projects\practice\practice\practice.cpp(19) : warning C4700: uninitialized local variable 'noOfguesses' used
c:\documents and settings\mike\my documents\visual studio 2005\projects\practice\practice\practice.cpp(20) : warning C4700: uninitialized local variable 'noofgamesplayed' used

and then it doesn't work. it gives me a message that says the variable 'noofguesses' is being used without being defined. could someone please show me what i need to do to fix this. here is my program. i will try to do it the right way this time

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

using namespace std;

int main ()
{
    int num;
    int guess;
    bool done;
    int noOfGuesses = 0;
    int ncount;
    int sum=0;
    int noOfguesses;
    int avgNoOfGuesses;
    int noofgamesplayed;

     sum+=noOfguesses;
     avgNoOfGuesses=sum/noofgamesplayed;

    num = (rand() + time(0)) % 1000;
    done = false;
    while ((noOfGuesses < 10) && (!done))

    {
        cout << "Enter an integer greater"
            << " than or equal to 0 and "
            << "less than 1000: ";
        cin >> guess;
        cout << endl;
        noOfGuesses++;
        if (guess == num)
        {
            cout << "you guessed the correct "
                << "number." << endl;
            done = true;
        }
        else
            if (guess < num)
                cout << "Your guess is lower "
                << "than the number. \n"
                << "Guess again!" << endl;
            else
                cout << "Your guess is higher "
                << "than the number.\n"
                << "guess again!" << endl;
            cout <<"Total gueses equal " << noOfGuesses << endl;



    }
    return 0;
}

i hope i did it right this time. if i didn't i am sorry

Edited 3 Years Ago by mike_2000_17: Fixed formatting

error "unreferenced local variable" Means that you initialized the variable without using it

error "uninitialized local variable 'noOfguesses' used" means that you forgot to initialize it before using it. just write int noOfguessos=0; somewhere before you use it.

i now have 1 error and 1 warning. the error message says :
error C2086: 'int noOfGuesses' : redefinition
what does this mean? this is really confusing me. thank you for the help you are giving me. if anyone could check my work and tell me where i am going wrong i would really appreciate it

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

using namespace std;

int main ()
{
	int num;
	int guess;
	bool done;
	int noOfGuesses=0;
	int ncount;
	int sum=0;
	int noOfGuesses;
	int noofgamesplayed;
	int avgNoOfGuesses;

	 sum+=noOfGuesses;
	 avgNoOfGuesses=sum/noofgamesplayed;

	num = (rand() + time(0)) % 1000;
	done = false;
	while ((noOfGuesses < 10) && (!done))
	
	{
		cout << "Enter an integer greater"
			<< " than or equal to 0 and "
			<< "less than 1000: ";
		cin >> guess;
		cout << endl;
		noOfGuesses++;
		if (guess == num)
		{
			cout << "you guessed the correct "
				<< "number." << endl;
			done = true;
		}
		else
			if (guess < num)
				cout << "Your guess is lower "
				<< "than the number. \n"
				<< "Guess again!" << endl;
			else
				cout << "Your guess is higher "
				<< "than the number.\n"
				<< "guess again!" << endl;
			cout <<"Total gueses equal " << noOfGuesses << endl;


		
	}
	return 0;
	}

Sorry if my first reply confused you, I didn't really look at the code you posted, just the errors.
You initialized "noOfguesses" twice, thats what the redefinition error means.


A good way to correct your errors is by double clicking them in the error box (if your using microsoft visual studio)
That will take you to the line where the error occurs, and might help you clear out any problems.

thank you i didn't know you could double click on it and it would point it out. i am sorry to bother you again but could you tell me what these warnings mean and why they might come up or how i can fix them. again thank you for all your help

warning C4244: '=' : conversion from 'time_t' to 'int', possible loss of data
warning C4101: 'ncount' : unreferenced local variable
warning C4700: uninitialized local variable 'noofgamesplayed' used

i tried to run the program and i received an error message. do you know what this means

run time check failur #3 the variable 'noofgamesplayed' is being used without being defined. i thought i defined it in the program under int noofgamesplayed;
did i do it wrong? please let me know

Unless you've changed it by now your code has this snippet:

int noOfGuesses = 0;
int ncount;
int sum=0;
int noOfguesses; 

int noofgamesplayed;
int avgNoOfGuesses;

sum+=noOfGuesses;
avgNoOfGuesses=sum/noofgamesplayed;

This:
int noOfguesses;
is a duplicate of this:
int noOfGuesses = 0;
and needs to go if you haven't already gotten rid of it.

Here:
int noofgamesplayed;
you declare the variable, but don't give it any meaningful value so it's got a value of some unknown amount and amounts to junk.

Here:
sum+=noOfGuesses;
both sum and noOfGuesses have both been given a value before they are used (assuming you ditch the duplicate of noOfGuesses as instructed). But by the time you use noofgamesplayed here:
avgNoOfGuesses=sum/noofgamesplayed;
it still hasn't been given a meaningful value so it's still got a value that's junk at run time, eventhough it's legal code and will compile as is, so your compiler is pointing out a runtime error, not a compile/build time error. Lucky you.

This article has been dead for over six months. Start a new discussion instead.