-1
#include <iostream>
#include <stdlib.h>

using namespace std;

void Guess(int Answer,int guess)
{
    if(guess!=Answer)
    {
        if(guess<Answer && guess!=Answer)
        {
            cout<<"The number is higher, try again"<<endl;
            cin>>guess;
        }
        else
            if(guess>Answer && guess!=Answer)
                {
                    cout<<"The number is lower, try again"<<endl;
                    cin>>guess;
                }
    }

}

int main()
{
    int Answer,guess;
    Answer=rand()%100;
    cout<<Answer<<endl;
    cout<<"Guess the number from 1 to 100"<<endl;
    cin>>guess;
    while(Answer!=guess)
       {
            Guess(Answer,guess);
            if(Answer==guess)
                break;
       }
       if(guess==Answer)
        cout<<endl<<"Congratulations, you guessed the number, it was "<<Answer<<" !";
}

So, this is the code I made after I learned some basic functions, you can probably tell that it is supposed to generate a random number, and then you have to guess what the number is, but I have a few problems:

  1. it generates the same random number
  2. If you don't guess it right the first time, the code keeps going forever even if at some point u guess the number.
  3. If you guess a higher number, let's say 50 and the answer if 40, even if u then guess 30 it still says the number is lower when it's supposed to say higher.

What did I do wrong?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Byakuran
  • 15
  • 2

1 Answers1

1

Changed void Guess(int Answer,int guess) to void Guess(int& Answer,int& guess) and added srand ( time(NULL) ); at the start of int main to prevent the same number to be generated

Byakuran
  • 15
  • 2