How to make a loop exit? I tried all the options. In debugging, for some reason, after the withdrawal of "Try again?" makes it endless.

int main() { int ch = getch(); char buffer[10] = {0}; do{ buffer[10] = {0}; int number = 0; cin >> number; int res = CheckNumber(number); if (res != 0) { Worker(number); }else{ cout << "Is not number!"; if(ch == 'q'){ break; } } }while(true); return 0; } 
  • buffer[10] = {0} ? How did you compile this? - VladD
  • I don’t know) I tried to reset the array there (it’s not strong in the pros because I’m used to working with strings) - user211406
  • 2
    And why are you trying to refer to the element of the array beyond? Index 10 you do not exist. - VladD
  • What difference does it make, I have an endless loop. If a number is not entered, and I want to, after the output of Is not number, the function starts again. And she just takes and infinitely displays this message and does not reach cin on a new one. - user211406
  • 2
    You do not know what is undefined behavior, so you do not see the difference. But this does not mean that it is not. - VladD

3 answers 3

Depart and check the received ch values, it never goes into if, and that's what

example:

 int q; cout << "Введите число:"; cin >> q; func(q) void func(int q) { /*что то проверяет и делает*/ return; else { std::cout << "Введите число:"; std::cin >> q; func(q); } 
  • Yes, I noticed, I fixed it all the same, the cycle is not clear. You can throw an adequate example. That the program did a recursion and again it was required to enter number. (And she constantly shows Is not a number) - user211406
  • int q; cout << "Enter the number:"; cin >> q; func (q) void func (int q) {/ * checks and does something * / return; else {std :: cout << "Enter the number:"; std :: cin >> q; func (q);} - Xambey
  • Formatted sample code from your comment. Something is wrong with him) - Nick Volynkin

To begin with, it’s not good to use getch and cin at the same time. It’s like entering two doors at the same time ...

Let's say you entered something in ch . What's next? Then you try to count the number from cin and process. Suppose something is entered ... you are processing. If res number, go to the next cycle, no - the conclusion that it is not a number. Check - WHAT? ch you entered before loop . It is unlikely that q entered - so the cycle goes to the next iteration ... And so to infinity - ch does not change!

Only here you have at this assignment buf[10] and the memory turns out to be damaged, keep this in mind. You write outside the array ...

    so it is possible

    `

      int main() { int ch = getch(); char buffer[10] = {0}; switch (o) { case 'q': exit(0)' default: { buffer[10] = {0}; int number = 0; cin >> number; int res = CheckNumber(number); if (res != 0) { Worker(number); }else{ cout << "Is not number!"; } } } } int main() { int ch = getch(); char buffer[10] = {0}; while ((ch = getch()) != 'q') buffer[10] = {0}; int number = 0; cin >> number; int res = CheckNumber(number); if (res != 0) { Worker(number); }else{ cout << "Is not number!"; } } return 0; } 

    `

    • one
      And what do you get if you need to enter the next number? Eaten figure? - Harry
    • get a new character entry - Senior Pomidor
    • By no means. You entered a number. Worker issued something there, and then what? You can’t enter a number right away because the first click will be “eaten” ... - Harry