-1

Alright so here's my question... I pretty much finished my little game I made, but I want to test to see if it works with a "debug cheat" to see if the program is actually showing you the right number.

I have srand(time(0)) set so that when you play the game, the program obviously makes the number selection different each time. Though, it apparently keeps generating a different number apparently, because the system time is changing.

As you can see in the code, I have a variable named "ANSWER" aka Range(low,high). The user input variable called "guessN" is checked to see if it matches "ANSWER". If it does, then the program says the user won the game, which I believe works.

The PROBLEM is that when I want the program so show what the answer is first, which happens to be 47, when I enter in 47, it's actually WRONG because the time generator is still going and it turned to be 37 already.

What I need help with: is there a way to currently pause the generation until the user enters in his answer?..

THANK YOU IN ADVANCE FOR HELPING ME! :)

SCREEN SHOT:

https://i.stack.imgur.com/CWAhw.png

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib> // For srand and rand

using namespace std;

/*Guessing Game: Program generates random #'s,
user has 3 chances to guess one number at a time,
otherwise the Computer wins.
*/


static unsigned int rangevalue;

void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
    rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}


unsigned int getRange() 
{
    return rangevalue;
}

//DON'T GET LOST!
string DescRange; // Descriptive Range
string SelectionInput;
int low = 1;
int high;
//DON'T GET LOST!

void NumberGen()
{



    do
    {
        cout << "Guess a number in between...\n " << endl;
        cout << "1.) 1-5\t\t[EASY]" << endl;
        cout << "2.) 1-10\t[MEDIUM]" << endl;
        cout << "3.) 1-50\t[HARD]" << endl;
        cout << "4.) 1-100\t[IMPOSSIBLE]" << endl;
        cout << "\n5.) Choose a custom range? *" << endl;
        cout << "6.) EXIT \n" << endl;
        cout << "=> ";
        getline(cin, SelectionInput);

    } while (
           SelectionInput != "1" && SelectionInput != "2" && SelectionInput != "3" && SelectionInput != "4" && SelectionInput != "5" && SelectionInput != "6"
        && SelectionInput != "EASY" && SelectionInput != "Easy" && SelectionInput != "easy"
        && SelectionInput != "MEDIUM" && SelectionInput != "Medium" && SelectionInput != "medium"
        && SelectionInput != "HARD" && SelectionInput != "Hard" && SelectionInput != "hard"
        && SelectionInput != "IMPOSSIBLE" && SelectionInput != "Impossible" && SelectionInput != "impossible"
        && SelectionInput != "CUSTOM" && SelectionInput != "Custom" && SelectionInput != "custom" 
        && SelectionInput != "EXIT" && SelectionInput != "Exit" && SelectionInput != "exit"
            );



    if (SelectionInput == "1" || SelectionInput == "EASY" || SelectionInput == "Easy" || SelectionInput == "easy")
    {
        high = 5;
        DescRange = "1-5";

        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "2" || SelectionInput == "MEDIUM" || SelectionInput == "Medium" || SelectionInput == "medium")
    {
        high = 10;
        DescRange = "1-10";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "3" || SelectionInput == "HARD" || SelectionInput == "Hard" || SelectionInput == "hard")
    {
        high = 50;
        DescRange = "1-50";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "4" || SelectionInput == "IMPOSSIBLE" || SelectionInput == "Impossible" || SelectionInput == "impossible")
    {
        high = 100;
        DescRange = "1-100";
        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

    }

    else if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
    {

        cout<<"\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;

        cout << "Input a digit range for you to guess a number in between. " << endl;

        cout << "Low: ";
        cin >> low;

        cout << "High: ";
        cin >> high;

    }

    else if (SelectionInput == "6" || SelectionInput == "EXIT" || SelectionInput == "Exit" || SelectionInput == "exit")
    {

        high = 10;

        cout << "\nYou've chosen to terminate the program!" << "\t[Input was \"" << SelectionInput << "\"]\n\n" << endl;
        for (int iii = 0; iii < 5; iii++)
        {
            cout << "The program will now collapse..." << endl;

        }

        cout << "\n" << endl;

    }



    NewRange;

    int ANSWER = getRange();


    cout << "For debugging/test purposes, the range is: \"" << ANSWER << "\". \n" << endl; // DEBUG for knowing what the 'ANSWER' is.


}



void Guessing()
{


    NumberGen();

    int ANSWER = getRange(); // Making things SIMPLE
    int guessN;

    if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
    {

        cout << "Guess the number I picked between " << low << "-" << high << ": ";


        cin >> guessN;

        if (guessN == ANSWER)
            cout << "Oh no, you won! The answer \"" << guessN << "\", is... correct! \n\n" << endl;

        else
            cout << "\nSorry you lose, try again! Answer was " << ANSWER << ". \n\n" << endl;



    }

    else
    {
        cout << "Guess the number I picked between " << DescRange << ": ";

        cin >> guessN;

        if (guessN == ANSWER)
            cout << "Oh no, you won! The answer \"" << guessN << "\", is... correct! \n\n" << endl;

        else
            cout << "\nSorry you lose, try again! Answer was "<<ANSWER<<". \n\n" << endl;


    }



}


int main()
{

    srand(time(0));

    Guessing();







//system("PAUSE");      //  * Temporarily Disabled *

    return 0;

}
Jordan
  • 69
  • 1
  • 11
  • `NumberGen` has an `int Answer`, and `Guessing` has a completely different `int ANSWER` variable, and they're initialized separately. Why would you expect the numbers to be the same. – Mooing Duck Aug 07 '14 at 23:41
  • I was thinking that actually! Though, how would I go about fixing this, because apparently srand(time(0)) has to be declared before int ANSWER – Jordan Aug 07 '14 at 23:44
  • 1
    srand() just seeds the random generator -- you don't need to call it at all as long as you are happy with getting the same sequence of "random"number each time -- so move the srand() to main, and only call your "Range" function when you actually want a new range. – Soren Aug 07 '14 at 23:53
  • But then if people play the game again, won't they get the same predictable answer? It wouldn't be different. – Jordan Aug 08 '14 at 00:02
  • @Jordan -- yes they will --that is why I say "as long as you are happy with getting the same sequence". If not, you just need to seed once in the lifetime of the program, and not on every call before doing rand() -- if you seed more than once you will actually risk of getting identical numbers for each rand() call if you do it sufficently often, and the generator is reset at every call. – Soren Aug 08 '14 at 00:06

1 Answers1

-1

You should do the srand(time(0)) only once in your program -- move it to the main() program before you other code is called.

srand(time(0)) is generally considered a bad idea where security is a concern, but that is a different topic and too long to discuss here.

Edit

The simplest fix to your code is to create two functions -- one to generate a new value, and one to retrieve the value;

static unsigned int rangevalue;
void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
    rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}
unsigned int getRange() {
    return rangevalue;
}

And then use those two functions in your code.

If you want to make your code more C++ like, the consider using a class concept for wrapping your logic ... something like;

class RangeValues {
    unsigned int rvalue;
public:
    RangeValues() {
       static bool initialized = false;
       if (!initialized) {
          initialized = true;
          srand(time(0));
       }
       rvlaue = rand();
    }
    unsigned getRange(int nLow, int nHigh) { // Range for generated #'s.
        return (rvalue % (nHigh - nLow + 1)) + nLow;
    }
}

Which will generate a new value for every time you create a new instance of RangeValues, which you can then pass around by reference.

Soren
  • 14,402
  • 4
  • 41
  • 67
  • Thank you about that, but that didn't fix my actual problem :( – Jordan Aug 07 '14 at 23:47
  • The `rand()` cal;l will return a new number every time cal;led (e.g. a new random number) -- so if you want to reuse (or pause it as you say), you will need to store it in a local variable, and only call `rand()` when you want a new number – Soren Aug 07 '14 at 23:49
  • Im not sure what you are confused about -- you are calling Range multiple times for every question and every answer -- you need to cal it only once – Soren Aug 08 '14 at 00:08
  • Ohhhh! I see now. I'm just using the call to show the number to the user, not to reset it. How can I fix this exactly just to SHOW the range? – Jordan Aug 08 '14 at 00:10
  • I updated the answer with suggestion on how you implement your "show" function.... – Soren Aug 08 '14 at 00:17
  • So now I'm doing int Answer=rangevalue() ? It's giving me an answer of zero when I run it now. – Jordan Aug 08 '14 at 00:29
  • That would be because you have not called `NewRange` --- which you should call every time you want a new number -- so about the place where your original code had the srand() call.. and then replace all other `Range` calls with `getRange` – Soren Aug 08 '14 at 00:37
  • god... dammit.. it's not working fml. i'm going to update the original post with the code I currently have. Let me know if I did it right – Jordan Aug 08 '14 at 00:48
  • You are not calling `newRange` anywhere... In `NumberGen`, just before calling `getRange` make a call to `NewRange` -- -and in `main` before calling `Guessing` make a call to `srand(time(0))` – Soren Aug 08 '14 at 00:54
  • I called NewRange by saying cout< – Jordan Aug 08 '14 at 00:58
  • change the `cout< – Soren Aug 08 '14 at 01:00
  • Mother f*** IT WORKED!!! Thank you so much! I didn't downvote you by the way, I'm new to this site and I can't upvote/downvote yet. :( Thank you for all your help! – Jordan Aug 08 '14 at 01:03