There is such a code

I just can not understand what the problem is. Tried to make a cycle of dialogue with the user, when selecting item 1 - nothing, with 2 - an eternal cycle. Vinyl internal functions, but they are tested, 100% operational. What could be the problem?

void text::user_iteration() { int x; for( ;; ) { std::cout << "--Menu--\n" << "Push the number to choose the option.\n" << "1. Add a string or few strings.\n" << "2. Check current text.\n" << "3. Exit.\n"; if( std::cin >> x ) { if( x == 1 ) { std::cout << "Enter your text please: \n"; text::add_string(); continue; } if( x == 2 ) { std::cout << "The current text is: \n"; text::output_text(); continue; } if( x == 3 ) { break; } } } } 

Closed due to the fact that off-topic participants Vlad from Moscow , aleksandr barakin , αλεχολυτ , user194374, Denis 14 Dec '16 at 6:49 .

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

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - aleksandr barakin, αλεχολυτ, Community Spirit, Denis
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    The only thing that works is true is 3. - Freddy
  • five
    And why don't you use the debugger? - Vladimir Martyanov
  • Good question) - Freddy
  • Either I'm stupid, or I don't get anything worthwhile from the debugger - Freddy
  • The answer to the question about or - definitely YES. Learn to use the debugger, without it you should not and try to program. - Vladimir Martyanov

2 answers 2

Why don't you use case ?

 #include <iostream> int main() { int x; bool looping = true; char text[80]; for( ;; ) { if (!looping) break; std::cout << "--Menu--\n" << "Push the number to choose the option.\n" << "1. Add a string or few strings.\n" << "2. Check current text.\n" << "3. Exit.\n"; std::cin >> x; switch(x) { case 1: { std::cout << "Enter your text please: \n"; std::cin >> text; break; } case 2: { std::cout << "The current text is: \n"; std::cout << text << std::endl; break; } case 3: { looping = false; break; } } } } 
  • I just found a problem. The call works successfully if you do not use this long std :: cout << with entries in the menu. I'm generally at a dead end. I can't even imagine why. - Freddy
  • and case used, just laid out my last option. 4 hours already I fight with this and I can not understand what the problem is. - Freddy
  • the funny thing is that without std :: cout everything works - Freddy
  • Indent by 4 characters - and everything will look like a code (int main line), or select the text and click on the "code" icon - nick_n_a
  • why for ?? when it was possible to do while ?? - Alex.B

Since this code works correctly:

 int main(int argc, const char * argv[]) { int x; for( ;; ) { std::cout << "--Menu--\n" << "Push the number to choose the option.\n" << "1. Add a string or few strings.\n" << "2. Check current text.\n" << "3. Exit.\n"; if( std::cin >> x ) { if( x == 1 ) { std::cout << "Enter your text please: \n"; cout << "text::add_string()" << endl; continue; } if( x == 2 ) { std::cout << "The current text is: \n"; cout << "text::output_text();" << endl; continue; } if( x == 3 ) { break; } } } } 

it remains to conclude that the problem is in your functions of adding a line and output. Without their text, talking about something is completely meaningless.

As it is now customary to say, the vangy remains in the input buffer after reading the numbers.

Most likely, the cin buffer is not reset in add_string after you enter the value of x ; \n lies in the buffer, and you get not what you want. And when you query the menu x , the string is read, not the number, and goes into an infinite loop - you do not execute input errors!

  • You are right, that was exactly the problem! Thank! - Freddy