Need to write a game "Guess the number." Enter the number and number of attempts. Entered a number (it does not display it), and the one who guesses gets the number of attempts. And there in the course writes how many attempts left.

Tell me how to do it, give a starting point, the code is not needed - just a thought.

Did something like this:

#include <iostream.h> void main() { int a, b, p; std::cout << "Vvedite chislo" << std::endl; std::cin >> a; std::cout << "Vvedite kolichestvo popitok" << std::endl; std::cin>>p; int s = 0, e = 1; for(int i = 0; i < p; i++) { std::cin>>b; if(b > a) std::cout << "Bolshe" << std::endl; //Подсказка 1 else if(b < a) cout<<"Menshee"<<endl; // Подсказка 2 else if(b == a) { std::cout << "Vi ugadali chislo" << std::endl; break; // Завершает цикл } s += e; } std::cout << "Popitka nomer - " << s << std::endl; } 

    3 answers 3

    Something like that:

     int number=(число, которое надо угадать); int N=(количество попыток); while(N) { cout << "Попыток осталось: " << N << endl; int value; cin >> value; if(value==number) { cout << "Число угадано!\n"; break; } N--; } 

    This is pseudocode, since in fact the Russian text is not displayed correctly.

    • We must try and so too) - Goldy

    By the frontal cycle by the number of attempts: at each iteration, output (the number of attempts minus the iteration number) and request a number, if the number is what you need, you process and drop out of the cycle. Look like that's it :)

    • but as a thread is easier without arrays. This is an idea of ​​a training task, which was given without explanation ... - Goldy
    • I did not say anything about arrays - Kozlov Sergei
    • After reading the "iterations" I thought about arrays, sorry. - Goldy

    Ask for more, less or equal to the given number given.

    Next, apply a binary search. The starting range is MIN_INT: MAX_INT.

    The first attempt to guess is 0.

    • I just started learning C ++, I’ll have to read about MIN_INT: MAX_INT - Goldy
    • Yes, and at the same time about the binary search (or know this?). By the way, I apologize, I was wrong. Not MIN_INT, MAX_INT, but INT_MAX and INT_MIN . They are defined in <limits.h> - avp
    • It is new for me about it, I’m happy to read it) I found an easier solution, frankly made to “leave me alone” because of the deadlines) - Goldy
    • Tell me how to make the number not shown when typing? ) - Goldy
    • In * nix, the easiest way (for tests, etc.) is system ("stty -echo"); Only then do not forget to turn on the display of the input system ("stty echo"); In Windows, I do not remember (look for yourself). - avp