Number guessing game C++

 Complete code for number guessing game written in C++

Code:


#include<cmath>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    unsigned seed;
    srand(seed);
    int number , chk ;
    number = 1 + rand() % 100;
    char tryag = 'y';

    cout<< endl<< endl<<endl<< "              ===================================================="<<endl;
    cout<<"              ========= This Game is made by Zain Aftab =========="<<endl;
    cout<<"              =============== Number Guessing Game ==============="<<endl;
    cout<<"              ===================================================="<<endl<<endl<<endl;

    system("pause");
    system("cls");

    while(tryag == 'y')
    {
        number = 1 + rand() % 100;
        cout << "Guess The Number (1 to 100) : ";
        cin >> chk ;
        if (chk >= 1 && chk <=100)
        {
            while(chk >= 1 && chk <=100)
            {
                if (chk > number)
                {
                    cout << "Too Large.";
                    cout << endl << "Guess Again : ";
                    cin >> chk;
                }

                if (chk < number)
                {
                    cout << "Too Small.";
                    cout << endl << "Guess Again : ";
                    cin >> chk;
                }

                if (chk > number)
                {
                    cout << "You Won.";
                    cout << endl << "Do You Want To Try Again (y/n) : ";
                    cin >> tryag ;
                    chk = 0 ;
                    if (tryag == 'n')
                    {
                        cout << "The Program Now Exits.";
                        system("pause");
                        system("cls");
                        cout<<endl<<endl<<endl<<"                ===================================================="<<endl;
                        cout<<"                ================ Thanks For Playing ================"<<endl;
                        cout<<"                ===================================================="<<endl;
                        system("pause");
                        exit(0);
                    }
                }
            }
        }
        else
        {
            cout << "You Have Entered An Invalid Number .";
        }
    }
    system("pause");
    return 0;
}

Comments

Popular posts from this blog

Assembly Program Code For Brick Breaker Game

Polynomial C++ Class