0

I am getting an error saying that guess isn't defined in main. whats going on? the prog is designed to be a guessing game

The while is only stopping because of the guess defined in main, but that makes the computer ask fore an input that isn't tested.

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int nextGuess();

int main() 
{
    srand(time(0));
    int number = rand() % 100 + 1;

    cout <<" --GUESSING GAME-- \n You are to enter numbers, trying to guess rthe computer's number"<< endl;

int guess = nextGuess();
    do
    {    
    int guess = nextGuess();

    if (guess > number)
       cout <<"Your guess is to high."<< endl;

    if (guess < number)
       cout <<"Your guess is too low."<< endl;

    if (guess == number)
       cout <<"Good job, that's the number!"<< endl;
    }
    while (guess != number);

    system("pause");

}

int nextGuess()
{
    int guess = 0;
    cout <<"Please enter a number:";
    cin >> guess;

    return guess;
}

** Id paste the c prompt but it wont copy.

matttm
  • 124
  • 1
  • 2
  • 15

5 Answers5

1

You do call nextGuess twice: Once before the loop, and once inside the loop. Remove the first call, the one before the loop, but keep the declaration of the variable guess outside the loop and don't declare it again inside.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

In your while loop you are redefining the variable guess. Maybe change it to something like this:

int guess;
do
{    
    guess = nextGuess();

The way you have it written now, the variable guess used in the loop is not the same variable used in the while condition.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
1

You are calling nextGuess twice and redeclaring variable guess inside the do-while block. Change to something like :

int guess;
do
{    
guess = nextGuess();

if (guess > number)
   cout <<"Your guess is to high."<< endl;

if (guess < number)
   cout <<"Your guess is too low."<< endl;

if (guess == number)
   cout <<"Good job, that's the number!"<< endl;
}
while (guess != number);
jester
  • 3,491
  • 19
  • 30
0

Use while{} instead of do{}while

int guess = nextGuess();
while (guess != number)
{    
guess = nextGuess();

if (guess > number)
   cout <<"Your guess is to high."<< endl;

if (guess < number)
   cout <<"Your guess is too low."<< endl;

if (guess == number)
   cout <<"Good job, that's the number!"<< endl;
}
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
0

It's because you're calling nextGuess twice. It's enough to declare the variable outside the loop and then call the function inside it.

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int nextGuess();

int main()
{
    srand(time(0));
    int number = rand() % 100 + 1;

    cout <<" --GUESSING GAME-- \n You are to enter numbers, trying to guess rthe computer's number"<< endl;

    int guess;

    do{
    guess = nextGuess();

    if (guess > number)
       cout <<"Your guess is to high."<< endl;

    if (guess < number)
       cout <<"Your guess is too low."<< endl;

    if (guess == number)
       cout <<"Good job, that's the number!"<< endl;
    }
    while (guess != number);

    return 0;
}
    int nextGuess()
{
    int guess = 0;
    cout <<"Please enter a number:";
    cin >> guess;

    return guess;
}
user2699298
  • 1,446
  • 3
  • 17
  • 33