"Would you like to play again?" issue

This is a discussion on "Would you like to play again?" issue within the C++ Programming forums, part of the General Programming Boards category; Hello I am having difficulty, in all my programs, adding the line "would you like to_____again y/n?" I would like ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    "Would you like to play again?" issue

    Hello

    I am having difficulty, in all my programs, adding the line "would you like to_____again y/n?"

    I would like it to repeat the entire program an infinite amount of times as long as y is pressed and to cout "Thanks for _________. Have a nice day." when n is pressed

    (I have gotten it to work once and the program is nice from a users view but the code turned into this weird paradox of for loops inside themselves, its pretty ugly)

    Hopefully that makes sense.

    Hhere is one of the less complicated examples I am working on:

    Code:
    #include <iostream>
    #include <time.h>
    
    #define MAX_RANGE 100
    
    using namespace std;
    
    int main ()
    {
        int counter;
        counter=0;
        int value, input;
        
        srand (time(NULL));
        value=rand()%MAX_RANGE+1;
        
        cout<<"Guess a number between 1 and 100\n\n";
        do {
            cin>> input;
            counter++;
            
            if (value>input) {
                  cout<<"\nI was thinking of a higher number than "<< input <<".  Try again.\n\n";
                  }
            else if (value<input) {
                 cout<<"\nI was thinking of a lower number than "<< input <<".  Try again.\n\n";
                 }
            else {
                 cout<<"\nThat's right! I was thinking of "<<input<<".  Good game.\n\n";
                 cin.ignore();
                 }
            }
        while (value!=input);
        cin.get();
    }
    Any help would be appreciated. Thank You.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Generally, this problem is solved by having multiple loops, one for "do continuing" and one for the actual guessing game (in this case). There really is no other solution.

    Using functions, we can of course hide one or both of the loops from the main function, but the two loops are still necessary SOMEWHERE in the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by beyondhuman View Post
    ... but the code turned into this weird paradox of for loops inside themselves, its pretty ugly)...
    Why is that a paradox? It's the natural way to express the algorithm. As Mats suggested, maybe you would find it more elegant to put the game loop in a separate "playGame()" function, and have only the "play again" loop (which will call playGame())in your main function.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    thank you that makes a lot of sense

    and R.Stiltskin it doesn't seem that strange when viewed that way.

    I have tried to impliment that technique but now I have an actual error in my program.

    It will run once and if you type 'n' it will cout "Thanks for playing." and everything is fine. However, if you type 'y' it will do nothing, just sit there (if you then type 'n' it will give the line "I was thinking of a lower number than *some random number not always with in the max range* Try again.").

    here is the new code:

    Code:
    #include <iostream>
    #include <time.h>
    
    #define MAX_RANGE 100
    
    using namespace std;
    int game();
    
    int main ()
    {
        char answer;
        
        cout<<"Guess a number between 1 and 100\n\n";
        do {
            
            game();
            cout<<"Would you like to play again? y/n\n\n";
            cin>> answer;
                 }
        while (answer=='y');
        cout<<"\nThanks for playing.";
        cin.get();
    }
                 
    int game()
    { 
        int counter;
        counter=0;
        
        int value, input;
        
        value=rand()%MAX_RANGE+1;
        srand (time(NULL));
        
        do {
            cin>> input;
            counter++;
            
            if (value>input) {
                  cout<<"\nI was thinking of a higher number than "<< input <<".  Try again.\n\n";
                  }
            else if (value<input) {
                 cout<<"\nI was thinking of a lower number than "<< input <<".  Try again.\n\n";
                 }
            else {
                 cout<<"\nThat's right! I was thinking of "<<input<<".  Good game.\n\n";
                 cin.ignore();
                 }
            }
        while (value!=input);
    }
    I know I can get this I just need a bit of help with this one instance.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        cout<<"Guess a number between 1 and 100\n\n";
        do {
    Study this bit of code, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Thank You!

    I can't believe I missed that!

    If anybody happens to encounter the same issues this is the working code:

    Code:
    #include <iostream>
    #include <time.h>
    
    #define MAX_RANGE 100
    
    using namespace std;
    int game();
    
    int main ()
    {
        char answer;
        
        do {
            game();
            cout<<"Would you like to play again? y/n\n\n";
            cin>> answer;
            cin.ignore();
            cout<<"\n";
                 }
        while (answer=='y');
        cout<<"Thanks for playing.";
        cin.get();
    }
                 
    int game()
    { 
        int counter;
        counter=0;
        
        int value, input;
        
        value=rand()%MAX_RANGE+1;
        srand (time(NULL));
        
        do {
            cout<<"Guess a number between 1 and 100\n\n";
            cin>> input;
            counter++;
            
            if (value>input) {
                  cout<<"\nI was thinking of a higher number than "<< input <<".  Try again.\n\n";
                  }
            else if (value<input) {
                 cout<<"\nI was thinking of a lower number than "<< input <<".  Try again.\n\n";
                 }
            else {
                 cout<<"\nThat's right! I was thinking of "<<input<<".  Good game.\n\n";
                 cin.ignore();
                 }
            }
        while (value!=input);
    }
    Thanks again for the quick and very helpful suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  2. I need to play a pre recorded vox file using C
    By m.sudhakar in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 06:59 PM
  3. pls post here tricks that you know of in C/C++, thanks
    By mickey in forum C++ Programming
    Replies: 55
    Last Post: 06-12-2003, 05:28 PM
  4. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 05:28 PM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21