It is necessary to ask the user if he is sure of the number of threads that he entered

while (true) { int newThreadCount; string choice; cout << "Количесво потоков: "; cin >> newThreadCount; cout << endl; if (newThreadCount > boost::thread::hardware_concurrency()*2) { cout << "Указанное количество потоков сильно превышает количество ядер процессора, прироста производительности не будет. Продолжить? (y\n)"; while (true) { getline(cin, choice); if (choice == "n") break; else if (choice == "y") break; } } } *Код, выполняемый после подтверждения* 

How to organize a confirmation (y \ n)? If you entered y , then the code should continue, if n - should repeatedly ask for the number of threads, if it entered not y \ n, then the code should require you to enter y or n .

  • internal loop organize as a separate function. In this case, everything is greatly simplified. - KoVadim
  • @KoVadim really. He himself did not think of it ._. - Vitali
  • one
    If you select no, you can make a return or exit the program in another way. The outer loop is short-closed - you need to change, or even remove it, or set the exit condition from the loop using newThreadCount. - nick_n_a February

2 answers 2

Well, if you need to enter in lower case and it is y/n (i.e. yes is not a valid input), then, for example,

 bool yes() { string s; for(;;) { cout << "(y/n)? "; if (!getline(cin,s,'\n')) return false; if (s == "y") return true; if (s == "n") return false; cout << "Wrong input! "; } } 

    An example from the book by Björn Straustrup, but with the number of attempts he tries

    ...

     int tries = 1; while (tries < 4) { cout << "Do you want to proceed (y or n) ?\n"; char answer = 0; cin >> answer; //считать ответ switch (answer) { case 'y': return true; case 'n': return false; default: cout<<"Sorry, I don't understand that.\n" ; tries = tries + 1; } }