~riderius/cpp-code

cpp-code/guess_the_number.cpp -rw-r--r-- 760 bytes
380d65aariderius digits_analysis.cpp: argv[1] can replace n 11 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <ctime>
#include <iostream>

int main(int argc, char* argv[])
{
    using namespace std;

    int highest_num;

    if (argc > 1)
        highest_num = atoi(argv[1]);
    else {
        highest_num = 100;
    }

    srand(time(NULL));
    int secret_num = 1 + rand() % highest_num, your_num = -1, attempts = 0;
    while (your_num != secret_num) {
        cout << "Enter the number: ";
        cin >> your_num;
        if (secret_num < your_num)
            cout << "Our number is less" << endl;
        else
            cout << "Our number is greater" << endl;
        attempts++;
    }
    cout << "You guessed. Number of attempts: " << attempts << ". "
         << "The hidden number is " << secret_num << endl;

    return 0;
}