1

So, I have to write a program from a random guessing game. The program needs to ask the player to guess a number between 1-100. At least one function must be used. It needs to tell the player if they are too low/high, ask them to try again, or if they guess it, ask them to play again.

I have a few errors that I can not figure out.

44: error: ‘int winlose’ redeclared as different kind of symbol 9: error: previous declaration of ‘int winlose(int)’ 44: error: ‘g’ was not declared in this scope

Code

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

int winlose(int);

int main()
{
    int g, n, x;

    while (x!=0)
    {
        do
        {
            srand(time(NULL));
            n = 1 + rand()%100;

            cout<<"Welcome to the guessing game. I have a number between 1-100. 
               Can you guess it?"<<endl;
            cout<<"Please enter your guess"<<endl;
            cin>>g;

        winlose(g); 
        } while (n!=0);

        cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
        cin>>x;
    }
    return 0;
}

int winlose(g)
{
    if (g<n)
    {
        cout<<"Your guess is too low. Try again."<<endl;
        cin>>g;
    }
    else if (g>n)
    {
        cout<<"Your guess is too high. Try again."<<endl;
        cin>>g;
    }
    else (g=n)
    {
        cout<<"Congrats! You win. The number was "<<n<<endl;
        n=0;
    }
    return g;
    return n;
}
Talita
  • 805
  • 3
  • 11
  • 31
  • `int winlose(g)` this is not a valid function declaration syntax – Borgleader Jul 12 '17 at 20:09
  • 2
    `int winlose(g)` -> `int winlose(int g)` – HolyBlackCat Jul 12 '17 at 20:10
  • 1
    [Off Topic] Only call `srand` **once** unless you really know what you are doing. – NathanOliver Jul 12 '17 at 20:10
  • 1
    You are picking a new number after every guess. This makes the hint that the number is too high or too low, as well as the request that they try again, meaningless. – François Andrieux Jul 12 '17 at 20:11
  • 2
    `x` is uninitialized on first use. `n` is not accessible in `winlose`. `return n;` will never be reached, but then the return value of `winlose` is never used. You should really take a minute to understand the concept of a scope. See this [cppreference page](http://en.cppreference.com/w/cpp/language/scope) and this [Stack Overflow question](https://stackoverflow.com/questions/11137516/scope-vs-lifetime-of-variable). – François Andrieux Jul 12 '17 at 20:12
  • `else (g=n)` this doesn't do what you think – Charles Jul 12 '17 at 20:35
  • make sure you give agata (and SO) full credit when you hand their code in – pm100 Jul 12 '17 at 21:24
  • for real extra credit you should write a program that plays this game. ie tries to guess the answer as fast as possible – pm100 Jul 12 '17 at 21:25
  • Thanks Agata, however in the class we have not learnd "bool" or to "return false/return true" yet, so I will not be able to use those. – Cry Bloodwing Jul 12 '17 at 23:23

1 Answers1

2

You made a few mistakes in addition to the function declaration. Function declarations must contain the type of each parameter, so the correct way is:

int winlose(int g);

You cannot use a condition in an else statement: (else (g=n)). An else statement is a catch-all, if none of the previous conditions (the ones in the if and else if()'s) were met. If you only want this to trigger on a specific condition, use another else if(). You are not required to have an else at the end of every if statement; it is perfectly correct to end with an else if(){...}.

You also need to compare with '==' rather than '='. = is the assignment operator and g=n will set the value of g to n. If you want to check if g is equal to n, you will have to use g==n

You should call srand() in the outer loop, otherwise after every guess, value changes.

The rest is corrected and sometimes slightly changed for correct performance:

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

bool winlose(int number, int givenNumber);

int main(){
    int g, n, x;
    bool guessed;
    do {
        srand(time(NULL));
        n = 1 + rand()%100;
        cout<<"Welcome to the guessing game. I have a number between 1-100. Can you guess it?"<<endl;
        do {
            cout<<"Please enter your guess"<<endl;
            cin>>g;
            guessed = winlose(g, n); 
        } while (!guessed);

        cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
        cin>>x;
    } while (x!=0);
    return 0;
}

bool winlose(int g, int n) {
    if (g<n) {
        cout<<"Your guess is too low. Try again."<<endl;
        return false;
    }
    else if (g>n) {
        cout<<"Your guess is too high. Try again."<<endl;
        return false;
    }
    else {
        cout<<"Congrats! You win. The number was "<<n<<endl;
        return true;
    }
}
user3362196
  • 102
  • 7
Talita
  • 805
  • 3
  • 11
  • 31