I need your help, please. Lets say, I want a code like this. There is subsection after which you decide if you want to continue reading next subsection or skip these subsections and continue into the main text.
I read it can be done with using cin.get() but everything is weird.
First of all, the cin.get() doesnt absolutely care which key I press...
Secondly I want to press one ENTER to get away, not twice...
If I write (cin.get() <= 'q') it needs to read two 'q' characters to work properly. Iam confused.
All I want to do is - when asked - press ENTER once to continue, press Q (and then ENTER) to quit..
Can you tell me the problem in my code, please?
Your code is calling cin.get() multiple times, but only once is needed.
If you needed to test for several different values, then save the result in a variable, and test that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
char ch;
ch = cin.get();
if (ch == 'a')
{
// action for a
}
elseif (ch == 'b')
{
// action for b
}
elseif (ch == 'c')
{
// action for c
}
else
{
// default action
}
#include <iostream>
usingnamespace std;
int main()
{
// All I want to do is - when asked
// - press ENTER once to continue,
// press Q (and then ENTER) to quit..
cout << "Hello. Continue? ENTER=yes, 'q'=no" << endl;
if (cin.get() == 'q')
return 0;
cout << "And so you continue..." << endl;
if (cin.get() == 'q')
return 0;
cout << "3.TEXT..." << endl;
return 0;
}
Also, though in your code, the use of goto is straightforward and easy to understand, its use is often frowned upon as it can lead to unstructured chaotic code which is hard to read, understand or maintain.