#include "stdafx.h" #include <iostream> #include "string" #include <cstdlib> #include <ctime> using namespace std; int main() { cout << "Make the number from 1 to 6" << endl; srand(static_cast<unsigned int>(time(0))); int number = rand() % 6+1; string answer; do { cout << "Is your number " << number << "?" << endl; cin >> answer; if (answer == "yes") { cout << "Your number is " << number << endl; } } while (answer!="yes"); return 0; } 

Tell me please. There is a code above. In theory, each cycle he should ask the user whether he made the correct number (the user keeps the number in his head). The number is from 1 to 6 and it should be different for each cycle. When the program guesses, displays the instruction from the if. But I constantly get the number 6 instead of different numbers. Where did I go wrong?

Closed due to the fact that off-topic participants are Harry , 0xdb , aleksandr barakin , andreymal , nörbörnën Jan 15 at 15:11 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Harry, 0xdb, aleksandr barakin, andreymal, nörbörnën
If the question can be reformulated according to the rules set out in the certificate , edit it .

2 answers 2

The number is from 1 to 6 and it should be different for each cycle.

Well, if it has to be different for each iteration of the loop, put a random number in the loop.

    Hey. Try before you use f-i rand call f-i randomize for initialization.

      ... randomize(); int number = rand() % 6+1; string answer; ... 
    • @ Oleg Only once in the program, and sometimes it happens that there are people who, before each call to rand() guess to do it ... And also - it is usually called as srand(time(0)) . - Harry
    • What is randomize() ??? There is no such function in the standard C ++ library. - AnT