Help Please in making Code replay the game.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gomi
    New Member
    • Feb 2008
    • 1

    Help Please in making Code replay the game.

    Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of 'Yes' and 'No' and if Yes then the game starts up again and if No then the game tells the user bye and quits.

    "Do you want to play again? (y/n)"

    Please Help if possible thanks. ^^


    -----------------------------------------

    [code=cpp]
    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "\t\tWelcom e to the Number Guessing Game!\n\n";

    cout << "Think of a number between 1 and 1000 and I'll try to guess it.\n";

    cout << "After guess, respond with 1 for higher, 2 for lower, or 3 for correct";


    int tries = 0;
    int low = 0;
    int high = 1001;
    char response = 1;


    do
    {
    int guess = (low + high) / 2;
    cout << "\nMy guess is " << guess << endl;
    tries += 1;


    cout << "Is your number (1)higher, (2) lower or am I correct?(3): ";
    cin >> response;

    switch (response)
    {
    case '1':
    low = guess;
    break;
    case '2':
    high = guess;
    break;
    case '3':
    cout << "Yeah! I guessed your number in only " << tries << " tries.";
    break;
    default:
    cout << "Uh, error. You made an illegal choice. " << response << endl;
    }
    } while (response != '3');


    system ("pause");

    return 0;
    }[/code]
    Last edited by sicarie; Feb 24 '08, 03:34 PM. Reason: Code tags.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need a loop in main() that tests for the choice to continue or not.

    If the choice is to continue, then call the code you have already written. Your main() is really the game. So change the name to Game().

    If the choice is to quit, then break out of the loop.

    Comment

    Working...