1

Whenever a number is given as an input, it always say too low even if I put negative value while the less and max random number is 1-100. I don't really understand why it is not working.

#include<iostream>

Is there a problem in here?

#include<cstdlib>
#include<time.h>

above

using namespace std;
void again();
void play_game();
void again()
{
    int schoice;
    while(true)
    {
        cout<<"\n\nYou want to play again?\n\n";
        cout<<"1) Play Again"<<endl;
        cout<<"0) Exit\n\n";
        cout<<"Input answer:";
        cin>>schoice;
        if(schoice == 1)
        {
            play_game();
        }
        else if(schoice == 0)
        {
            cout<<"\nOh ok see ya!\n\n";
            break;
        }
    }
}
void play_game()
{

Or the problem is in here?

    srand(time(0));
    int RanNum;
    RanNum = rand() % 100 + 1;

This part I don't really understand (above)

    cout<<"\n\n*The game is being played*\n\n"<<endl;
    cout<<"Guess the number between 1 to 100\n"<<endl;
    while(true)
    {
        cout<<"Input number:";
        int maxNum = 100;
        int leastNum = 1;
        int guess;
        cin>>guess;
        if(guess < leastNum || guess > maxNum)
        {
            cout<<"It must be higher or equal than"<<" "<<leastNum<<endl<<"and it must not exceed"<<" "<<maxNum<<endl;
        }
        else if(guess < RanNum)
        {
            cout<<"\nYour number is LOW\n"<<endl;
        }
        else if(guess > RanNum)
        {
            cout<<"\nYour number is HIGH\n"<<endl;
        }
        else
        {
            cout<<"\nYour correct!";
            again();
            break;
        }
    }
}
int main()
{
    int choice;
    cout<<"Want to play the game?\n\n"<<endl;
    cout<<"Type 1 if want to play"<<endl<<"Type 0 if not\n\n"<<endl;
    cout<<"Input here:";
    cin>>choice;
    switch(choice)
    {
    case 1 :
        play_game();
        break;
    case 0 :
        cout<<"\nOh ok see ya!\n\n";
        break;
    default :
        cout<<"\n\nThe choices are\n\n1 or 0 \n\nplease try again\n\n";
        break;
    }
    return 0;
}
Stef
  • 9,779
  • 2
  • 14
  • 27
LickMe
  • 21
  • 2
  • 1
    You seem to think the LOW vs HIGH is refering to comparing the answer to your guess. It's not. It's comparing your guess to the answer (order is important). I.e. when it says LOW it means your *guess* is lower than the answer and you should be guessing a *higher* number. Likewise, if it says HIGH it means your *guess* is higher than the answer, and you should be guessing a *lower* number. In other words, read the text. If it says your guess is LOW and you guess and even *lower* number, guess what: you're still too low. – WhozCraig Oct 29 '20 at 04:52
  • @WhozCraig A good point, but it doesn't account for the fact that the OP is experiencing a problem putting a number less than their min. Putting a negative number in, for example, should trigger the other message, which it sounds like it's not. –  Oct 29 '20 at 05:28
  • If your program is written and compiles fine but doesn't what's expected then don't be scared. That's what happens to most developers every day. You either have implemented the wrong algorithm or you have wrong implemented the right algorithm or (the most imaginable accident) you have wrong implemented the wrong algorithm. However, in any case, you have to debug your program to find out what is it in your case. FYI: [SO: What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/7478597) – Scheff's Cat Oct 29 '20 at 05:52
  • Ahh i get it now thanks people – LickMe Oct 29 '20 at 18:12

1 Answers1

0

I copied the code and everything worked just fine on my end. Maybe try recompiling it and making sure you are saving and compiling the updated version of the code.

Dy1lan
  • 1
  • 2
    Best to not answer a question like this. Stick to concrete actions you know will work. When you have more reputation, 50 I think, you can leave comments, but for now your options are limited. A few good questions answered or asked and you'll have the rep you need. – user4581301 Oct 29 '20 at 05:02
  • Thank you for clarifying that. I am just starting to get used to using StackOverflow to actually answer questions. I will keep that in mind for the future. – Dy1lan Oct 29 '20 at 05:10
  • I'm new here too, idk what is the meta here in stackoverflow (lol) – LickMe Oct 29 '20 at 18:14