Skip to main content
Tweeted twitter.com/StackCodeReview/status/970425960016293888
deleted 87 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

How I can make this programm better? Criticize this code C++ number-guessing game (computer tries to guess user's chosen number)

In this programmprogram we input a number. Our PC tries to guess this number.

After every try PC asks us:"Are your number more or less than?".

We input 'l' if our number is less, and input 'h' if our number is greater?

A range of possible values ​​is created.


 

I think this code is bad.

What What do you think about this code?

I need more critic !!!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max) {
    return min + rand() % (max - min);
}

int main()
{
    setlocale(LC_ALL, "rus"); 
    int our_num;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Input a positive number: " << endl;
    cin >> our_num;
    int t = 0;  //number of attempts
    int max = rand() + our_num;  //maximum possible value
    int min = 0;  //minimum possible value
    int d = Random(min, max);
    do {
        char lh;
        cout << d << " Is this your number?(my number is greater: 'h';   less: 'l')" << endl;
        cin >> lh;
        ++t;
        if (lh == 'h') {
            min = d;
            d = Random(min, max);
        }
        else if (lh == 'l') {
            max = d;
            d = Random(min, max);
        }
    } while (d != our_num);
    cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
    return 0;
}

How I can make this programm better? Criticize this code

In this programm we input a number. Our PC tries to guess this number.

After every try PC asks us:"Are your number more or less than?".

We input 'l' if our number is less, and input 'h' if our number is greater?

A range of possible values ​​is created.


 

I think this code is bad.

What do you think about this code?

I need more critic !!!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max) {
    return min + rand() % (max - min);
}

int main()
{
    setlocale(LC_ALL, "rus"); 
    int our_num;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Input a positive number: " << endl;
    cin >> our_num;
    int t = 0;  //number of attempts
    int max = rand() + our_num;  //maximum possible value
    int min = 0;  //minimum possible value
    int d = Random(min, max);
    do {
        char lh;
        cout << d << " Is this your number?(my number is greater: 'h';   less: 'l')" << endl;
        cin >> lh;
        ++t;
        if (lh == 'h') {
            min = d;
            d = Random(min, max);
        }
        else if (lh == 'l') {
            max = d;
            d = Random(min, max);
        }
    } while (d != our_num);
    cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
    return 0;
}

C++ number-guessing game (computer tries to guess user's chosen number)

In this program we input a number. Our PC tries to guess this number.

After every try PC asks us:"Are your number more or less than?".

We input 'l' if our number is less, and input 'h' if our number is greater?

A range of possible values ​​is created.

I think this code is bad. What do you think?

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max) {
    return min + rand() % (max - min);
}

int main()
{
    setlocale(LC_ALL, "rus"); 
    int our_num;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Input a positive number: " << endl;
    cin >> our_num;
    int t = 0;  //number of attempts
    int max = rand() + our_num;  //maximum possible value
    int min = 0;  //minimum possible value
    int d = Random(min, max);
    do {
        char lh;
        cout << d << " Is this your number?(my number is greater: 'h';   less: 'l')" << endl;
        cin >> lh;
        ++t;
        if (lh == 'h') {
            min = d;
            d = Random(min, max);
        }
        else if (lh == 'l') {
            max = d;
            d = Random(min, max);
        }
    } while (d != our_num);
    cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
    return 0;
}
Source Link
Bogdasar
  • 235
  • 2
  • 7

How I can make this programm better? Criticize this code

In this programm we input a number. Our PC tries to guess this number.

After every try PC asks us:"Are your number more or less than?".

We input 'l' if our number is less, and input 'h' if our number is greater?

A range of possible values ​​is created.


I think this code is bad.

What do you think about this code?

I need more critic !!!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max) {
    return min + rand() % (max - min);
}

int main()
{
    setlocale(LC_ALL, "rus"); 
    int our_num;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Input a positive number: " << endl;
    cin >> our_num;
    int t = 0;  //number of attempts
    int max = rand() + our_num;  //maximum possible value
    int min = 0;  //minimum possible value
    int d = Random(min, max);
    do {
        char lh;
        cout << d << " Is this your number?(my number is greater: 'h';   less: 'l')" << endl;
        cin >> lh;
        ++t;
        if (lh == 'h') {
            min = d;
            d = Random(min, max);
        }
        else if (lh == 'l') {
            max = d;
            d = Random(min, max);
        }
    } while (d != our_num);
    cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
    return 0;
}