0

I need help for looping back on the start of the program [C++].

#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    int rand_number = rand() % 101;
    int number;
    int counter = 1;


    cout << "NUMBER GUESSING" << endl;
    cout << "Try to guess number from 1 to 99: " << endl;

    do
    {
        cout << "Input number: ";
        cin >> number;
        if (number < rand_number)
        {
            cout << "Number is too small." << endl;
        }
        else
        {
            if (number > rand_number)
            {
                cout << " Number is too big." << endl;
            }
        }
        number++;

    } while (number != rand_number);
    cout << "Great! You guessed it in " << number << "th try." << endl;    

    cout << "Do you want to play again [Y/N]: ";
    cin >> Y;
    cin >> N;
 // dont know how to proceed 

    return 0;
}

I need help for looping back on the start when it asks me if I want to play again and answer Yes "Y", if I answer No "N" it says Goodbye. Any help would be appreciated, Thanks.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

4 Answers4

0

Similar to how you are using a do while, try adding an outer while loop that checks if the N key was pressed

xaisoft
  • 3,343
  • 8
  • 44
  • 72
0

You could create a boolean playAgain which would start as true. If the player says no, set it to false. You can then put your do while in another do while(playAgain). This would loop the game until the player says he does not want to play again.

Cedric Martens
  • 1,139
  • 10
  • 23
0

A very easy way to do this is to use nested while loops. You can use what you already have as the inner loop, then have another outside that that checks if the user has put in a Y or not. It can look something like this:

do {
    do {
        //Get numbers and check them
        //...
    } while(number != rand_number);
    std::cout << "Some message" << std::endl;
    std::cin >> option;
} while(option != 'N');

This goes through your loop, then allows the user to choose to continue. If they choose to go again, it will take them back up to the top of the outer while loop, and keep going until they say to stop.
EDIT:
Here would be the complete code:

#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    char option = 'a';
    do
    {
        int rand_number = rand() % 101;
        int number;
        int counter = 1;

        std::cout << "NUMBER GUESSING" << std::endl;
        std::cout << "Try to guess number from 1 to 99: " << std::endl;

        do
        {
            std::cout << "Input number: ";
            std::cin >> number;
            if (number < rand_number)
            {
                std::cout << "Number is too small." << std::endl;
            }
            else if (number > rand_number)
            {
                std::cout << " Number is too big." << std::endl;

            }
            counter++;

        } while (number != rand_number);

        std::cout << "Great! You guessed it in " << counter << "th try." << std::endl;    
        std::cout << "Do you want to play again [Y/N]: ";
        std::cin >> option;
    } while(option !='N');

    std::cout << "Goodbye!" << std::endl;

    return 0;
}
Hawkeye5450
  • 672
  • 6
  • 18
  • Your loop forces the player to play again on any input, except `'Y'`. Which is the opposite from the user's intentions. – Algirdas Preidžius Mar 14 '18 at 01:04
  • I tried it but it's not working, I'm a beginner and I'm struggling a lot. Can you make it so I can see, please? I really appreciate the effort. I just need to guess random number, when that ends it asks me if I want to try again , if no it just inputs Goodbye. –  Mar 14 '18 at 01:14
0

It is not the most orthodox method but it works :) Use goto.

int main()
{
mylabel:

...

if( <condition> )
{
    goto mylabel;
}

...

} 

If you want to have a more structured program write your main in anther function, say int func() and loop in main based on the return of the function.

int func()
{
    ...
    if( <condition> )
    {
        return 1;
    }
    ...
    return 0;
}

int main()
{
    while(func())
    {};
    return 0;
}
Victor Padureanu
  • 604
  • 1
  • 7
  • 12
  • Thanks for taking time, but I'm beginner and have no clue about everything. –  Mar 14 '18 at 01:20