Quick actions

cmd+k|ctrl+k

Navigation

Languages

random number generator (terminal)

Snippet info

Language

Cpp

Visibility

public

Author

aventus

Created

2019-05-13T20:33:21Z

Updated

2019-05-13T20:33:21Z

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
        int a;
        int y=0;
        int guesses=0;
        int games=0;
        double percentage;
        int RandomNumber;
        char response;
        int answer1;

        cout << fixed << setprecision(2);

        srand ( 99 );

        cout << "Would you like to play? (enter y or n)" << endl;
        cin >> response;
        cin.ignore();

        while ( response == 'y' )
        {
                a = rand() %7 + 13;
                cout << "Enter your guess" << endl;
                cin >> answer1;
                cin.ignore();

                while ( answer1 != a )
                {
                        if ( answer1 > a )
                        {
                                cout << "Your guess is greater than the random number." << endl;
                                cout << "Enter your guess" << endl;
                                cin >> answer1;
                                cin.ignore();
                                guesses +=1;
                        }
                        if ( answer1 < a )
                        {
                                cout << "Your guess is less than the random number." << endl;
                                cout << "Enter your guess" << endl;
                                cin >> answer1;
                                cin.ignore();
                                guesses +=1;
                        }
                }

                if ( answer1 == a )
                {
                        cout << "Congratulations. You guessed correctly." << endl;
                        games +=1;
                        cout << "Would you like to play again? (enter y or n)" << endl;
                        cin >> response;
                        cin.ignore();

                }
        }


        cout << "Number of Games played is " << games << endl;
        cout << "Number of guesses is " << games + guesses << endl;
        percentage = (double) games / (games + guesses);
        cout << "Your total winning percentage is " << percentage << endl;
        cout << "Thanks for playing. Come back again." << endl;



        return 0;
}


                                
INFO