Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,414 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,417 people online right now. Registration is fast and FREE... Join Now!




Number Guessing Game Help

 
Reply to this topicStart new topic

Number Guessing Game Help

FtK Shadow
14 Oct, 2008 - 09:27 AM
Post #1

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 20

Alright I have been working on this game for like a week or 2 now, but I am having some trouble towards the end. I need it for c++ work, but I have gotten so far I am sure someone can give me a little help with some bits.

Heres the code I have gotten so far:

CODE
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
char play;
int guess;
bool done;
int noOfGuesses = 0;
int random;
int c;
double bank, winnings, total;
int check;

void printPayout()
{
                cout << "1 guess, win 2.00" << endl;
                cout << "2 guesses, win 1.75" << endl;
                cout << "3 guesses, win 1.5" << endl;
                cout << "4 guesses, win 1.25" << endl;
                cout << "5 guesses, win 1.00" << endl;
                cout << "6 guesses, .75" << endl;
                cout << "7 guesses, win .5" << endl;
                cout << "8 guesses, win .25" << endl << endl;
}

int checkGuess(int guess)
{
    if (guess > random)
        {
            check = 1;
        }
    else if (guess < random)
        {
            check = -1;
        }
    else
        {
            check = 0;
        }

    return (check);
}

int genRandom()
{
    int r;
    srand(time(NULL));
    r = rand() % 100 + 1;

return r;
}


int game()
{
    cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
    cout << "Do you want to play (y / n)" << endl;
            bank = 100.00;
    cin >> play;
        if (play=='y')
        {
        winnings = 2.00;
        cout << "Great!, your payout will be as follows:" << endl << endl;
        printPayout();

        random = genRandom();
        while((bank!=0)&&(play=='y'))
{
noOfGuesses=0;
random = genRandom();
winnings = 2.00;

        while ((noOfGuesses!=8))
        {
cout << "Now guess a number between 1 and 100" << endl;
        cin >> guess;
        noOfGuesses++;
        c = checkGuess (guess);
        if (c == 0)
        {
    bank = bank + winnings;
cout << "Correct, you win " << winnings << ", your bank is now "<< bank<< endl<<"Do You Wanna Play Again??(Y/N)";
cin>>play;
break;
        }
        else if (c == -1)
        {
        cout << "Sorry too low try higher" << endl;
                winnings = winnings - .25;
        }
        else if (c == 1){
        cout << "Sorry too high try lower" << endl;
        winnings = winnings - .25;
        }
}
}
    }
    cout<<"Thank You For Playing, you are left with a total of " <<bank<<" $\n Wishing you will be back to play once more.";
return 0;
}


int main()
{

    game();

    return 0;
}


I am missing this part I just realized and dont know how I can take it out and make it a function.

QUOTE
4. You must have a function called calcWinnings that takes as an argument, the number of guesses it took to determine the number. You must use a switch statement to determine the winnings. This function returns the number of winnings.



I also realized it doesn't take a dollar away when you lose.

another part I am also missing is

QUOTE
3. You must ask the user if they want to continue after each correct guess. This means they cannot quit in the middle of a game


Anyone know of a good way to add these. I have been trying and keep doing something wrong. If someone could help me out here I would be so thankful smile.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Number Guessing Game Help
14 Oct, 2008 - 01:28 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,208



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well you can start by actually creating a function called calcWinnings. As the instructions say, it takes one parameter (the number of guesses) so that is going to be an integer parameter and it will return the winnings (which will be a dollar amount) so you use a double.

The result...

cpp

double calcWinnings(int numGuesses) {
// Switch statement for numGuesses
// Return a dollar value based on the guesses
}


Once you have this function setup, you can call it from the if statement where you detect they win. Pass the function the number of guesses it took them to get it right. Then it will return the winnings by which you can give to the user's bank.

The second one is saying create a game loop that basically keeps asking "do you want to play again (y/n)?" and in that loop you are going to have the actual guessing game loop. So when that loop ends it goes back to the beginning and prompts them for another game.

So put that in and we can help you further if you get stuck.

smile.gif
User is online!Profile CardPM
+Quote Post

FtK Shadow
RE: Number Guessing Game Help
14 Oct, 2008 - 03:17 PM
Post #3

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 20

Alright I got all of that working except for having the game loop when they say yes.


CODE
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
char play;
int guess;
bool done;
int noOfGuesses = 0;
int random;
int c;
double bank, winnings, total;
int check;
int main();

double calcWinnings(int numGuesses) {
    double value;
     switch (numGuesses)
    {
       case 1:  value = 2.00;
                break;
       case 2:  value = 1.75;  // fall through
                break;
       case 3:  value = 1.50; // fall through
                break;
       case 4:  value = 1.25; // fall through
                break;
       case 5:  value = 1.0; // fall through
                break;
       case 6:  value = 0.75;
                break;
       case 7:  value = 0.50;
                break;
       case 8:  value = 0.25;
                break;
   }
return value;
}


void printPayout()
{
                cout << "1 guess, win 2.00" << endl;
                cout << "2 guesses, win 1.75" << endl;
                cout << "3 guesses, win 1.5" << endl;
                cout << "4 guesses, win 1.25" << endl;
                cout << "5 guesses, win 1.00" << endl;
                cout << "6 guesses, .75" << endl;
                cout << "7 guesses, win .5" << endl;
                cout << "8 guesses, win .25" << endl << endl;
}

int checkGuess(int guess)
{
    if (guess > random)
        {
            check = 1;
        }
    else if (guess < random)
        {
            check = -1;
        }
    else
        {
            check = 0;
        }

    return (check);
}

int genRandom()
{
    int r;
    srand(time(NULL));
    r = rand() % 100 + 1;

return r;
}


int game()
{
    cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
    cout << "Do you want to play (y / n)" << endl;
            bank = 100.00;
    cin >> play;
        if (play=='y')
        {
        winnings = 2.00;
        cout << "Great!, your payout will be as follows:" << endl << endl;
        printPayout();

        random = genRandom();
        while((noOfGuesses!=8)&&(bank!=0)&&(play=='y'))
{
noOfGuesses=0;
random = genRandom();
winnings = 2.00;

        while ((noOfGuesses!=8))
        {
cout << "Now guess a number between 1 and 100" << endl;
        cin >> guess;
        noOfGuesses++;
        c = checkGuess (guess);
        if (c == 0)
        {
        winnings = calcWinnings(noOfGuesses);
        bank = winnings + bank;
cout << "Correct, you win " << winnings << ", your bank is now "<< bank<< endl<<"Do You Wanna Play Again??(Y/N)";
cin>>play;
break;
        }
        else if (c == -1)
        {
        cout << "Sorry too low try higher" << endl;
        }
        else if (c == 1){
        cout << "Sorry too high try lower" << endl;
        }
}
                cout << " Sorry you lose"<< endl<<"Do You Wanna Play Again??(Y/N)";
cin>>play;
}

    }

return 0;
}


int main()
{

    game();

    return 0;
}


This post has been edited by FtK Shadow: 14 Oct, 2008 - 03:17 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Number Guessing Game Help
14 Oct, 2008 - 04:30 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,208



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Ok great.... good work on that. One thing to make sure you do is in your calcWinnings to have a default case in the switch which will return 0. We want it so that if the guesses ever do ever equal anything but 1 - 8 that they get no winnings.

Now the next part is going to take place in your game function. What you want to do is start the program and have it ask the user if they want to play. But instead of just testing if it is "y" using an if statement you will begin a loop instead.

cpp

// Ask the user if they want to play
cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
cout << "Do you want to play (y / n)" << endl;
cin >> play;

// Now instead of an if statement, go into a loop that will keep going as long // as they keep saying yes to play...

while (play == 'y') {
// All game code is going to be in this loop.

// At the end we prompt them again (after winnings have been handled etc)
cout << "Do you want to play (y / n)" << endl;
cin >> play;
}


The first time you ask is going to get you into the loop where the game will the commence and after winnings have been paid and game ends, you prompt at the end of the game loop. If they choose yes, it goes right back to the top of the loop again and plays over.

Edit: Now you could do this back in main as well and make the loop setup around game(); but it might be good to stay in game() since you have most of your logic there to run the game.

Hope that makes sense to you. smile.gif

This post has been edited by Martyr2: 14 Oct, 2008 - 04:32 PM
User is online!Profile CardPM
+Quote Post

David W
RE: Number Guessing Game Help
15 Oct, 2008 - 12:36 AM
Post #5

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
... however ... it looks like you are still just a little lost ... eh?

This may help to get you started biggrin.gif

You have done a lot of work to get as far as you have... smile.gif

Shalom.

David

CODE
/*
    The ol'quessing game again, 'theme and variation' ...
    with 'crash proofing' and 'error input' handling.
*/

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cctype> // for toupper ...

using namespace std;

// GLOBALS ...

// Can use a GLOBAL variable here to KEEP value preserved between function calls
double bank = 2.00; // To ease testing ... and to keep junior from BIG losses.

const int maxNum = 100; // These Global constants allow the rules of the game to
const int minNum = 1;   // be easily adjusted, (if desired) ... right here.

double calcWinnings(int numGuesses)
{
    double value;
    switch (numGuesses)
    {
       case 1:  value = 2.00;
                break;
       case 2:  value = 1.75;  
                break;
       case 3:  value = 1.50;
                break;
       case 4:  value = 1.25;
                break;
       case 5:  value = 1.00;
                break;
       case 6:  value = 0.75;
                break;
       case 7:  value = 0.50;
                break;
       case 8:  value = 0.25;
                break;
    }
    return value;
}

void printPayout()
{
    cout << "If you guess the number in:\n\n"
         << "\t1 guess,   win 2.00\n"
         << "\t2 guesses, win 1.75\n"
         << "\t3 guesses, win 1.50\n"
         << "\t4 guesses, win 1.25\n"
         << "\t5 guesses, win 1.00\n"
         << "\t6 guesses, win 0.75\n"
         << "\t7 guesses, win 0.50\n"
         << "\t8 guesses, win 0.25\n\n"
         << "\telse ....  pay 1.00\n\n";
}

int checkGuess(int guess, int random)
{
    int check;
    if (guess > random)
    {
        check = 1;
    }
    else if (guess < random)
    {
        check = -1;
    }
    else
    {
        check = 0;
    }
    
    return (check);
}

int genRandom(int lowest, int highest)
{
    if (lowest > highest) // swap
    {
        int temp = lowest;
        lowest = highest;
        highest = temp;    
    }
    return  rand() % (highest -lowest +1) + 1;
}

// This function call 'Updates' the GLOBAL variable 'bank'
// if we exit on case '2' or '3' ... ( Bank is untouched if we exit case '1' )
bool game()
{
    // Call this only once ... like here at the beginning, (so it IS done).
    srand( time(NULL) );
    
    cout << "\nWelcome to the guess-o-matic.\n"
         << "It only costs a dollar to play.\n"
         << "You could double your bet.\n\n"
         << "Do you want really want to play (y/n) ? ";
        
    char play;
    cin >> play;
    cin.sync(); // 'flush' cin in-stream of any extra characters ...
    
    if ( toupper(play)=='N') return false; // exit case (1) this program right now ....
    if (bank < 1.00)
    {
        cout << "\nYou need at least ONE dollar in your bank to play.\n"
             << "Press 'Enter' to exit ... ";
        cin.get();    // to wait for 'Enter' key to be pressed ...
        return false; // exit case (1a) this program right now ....
        
    }
    
    // else ... WAS 'Y' ... so ... continue on ...
    
    // if we reach here ...
    double winnings = 2.00; // initial ... max possible win each 'game'
    cout << "\nGreat! Your payout will be as follows ...\n\n";
    printPayout();

    int noOfGuesses=0; // initial guess to zero before we start guessing ...
    int guess, low=minNum,  high=maxNum; // set initial low and high values
    int random = genRandom( low, high ); // get computer 'hidden guess'

    // use do...while since we do this at least once ...
    do
    {
         // We will force an exit at the bottom if this becomes '8'
        noOfGuesses++;
        cout << "Now guess a number in range " << low << ".." << high << ": ";
        cin >> guess;

        // But ... first ...
        // check cin flags for NOT good input (i.e. NOT an integer was entered)
        if ( !cin.good() || guess < low || guess > high )
        {
            cin.clear(); // first clear error condition flags so won't crash out
            cin.sync(); // now 'flush' ...
            cout << "\nERROR! Only integers "<<low<<".."<<high<<" please.\n\n";
            // If we want to NOT 'count' this 'typo error' ... we can now ...
            noOfGuesses--;
            continue; // i.e. go back to top of 'do...while' ... and do again.
        }
      
        int c = checkGuess (guess, random);
        if (c == 0) // a win so update and exit ....
        {
            winnings = calcWinnings(noOfGuesses);
            bank =  bank + winnings; // update Global bank
            cout << "Correct, you win $" << winnings
                 << ", your bank is now $" << bank << endl<< endl
                 <<"Do you want to play again (y/n) ? ";
            cin>>play;
            cin.sync();
            if ( toupper(play)== 'N') return false; // exit case (2) ...
            // if reach here...
            return true; // or ...  exit case (2) ...
        }
        // if we reach here ...we haven't got it yet ...  
        // so do one of these (ONLY ... if NOT last quess)      
        else if (noOfGuesses< 8)
        {
            if(c==-1)
            {
                cout << "Sorry too low ...  Try higher." << endl;
                if (guess > low) low = guess; // 'raise' the 'low' in this case
            }
            else // c==1; Reaching here, we know c!=0 and c!=-1 from above.
            {
                cout << "Sorry too high ...  Try lower." << endl;
                if( guess < high) high = guess; // 'lower' the 'high' in this case
            }
        }
    }while( noOfGuesses<8 && bank>=1.00 ); // Need at least ONE dollar to play
    
    // if reach here ... a loss ... so print ...
    cout << "Sorry ... You lose."<< endl;
    // and update losses ...
    bank = bank - 1.00;
    cout << "Your bank now has $" << bank << endl<< endl
         // and exit (after setting play again condition) ...
         <<"Do you want to play again (y/n) ? ";
    cin>>play;
    cin.sync();
    if ( toupper(play)== 'N' ) return false; // exit case (3) ...
    return true; // exit case (3) ...
}



int main()
{
    // We will always enter the game at least once ....
    // and the game always returns either 'true' or 'false'
    // So ...
    do{}while( game() );
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month