HOW cin.get()

Hi!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
      cout << "Hello. Continue? ENTER=yes, 'q'=no" << endl;
      cin.get();
      if(cin.get() !='q'){goto Next1;}
      if(cin.get() =='q'){goto Escape;}

Next1:
      cout<<"And so you continue..."<<endl;
      cin.get();
      if(cin.get() !='q'){goto Next2;}
      if(cin.get() =='q'){goto Escape;}

Next2:
      cout<<"3.TEXT..."<<endl;

Escape:
    return 0;
}


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?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace std;
int main()
{
	int  condition = 1;
	char option;

	cout << "Hello. Continue? ENTER=yes, 'q'=no" << endl;
	option = getch();

       // keep looping until q is pressed
	while(condition)
	{
		switch(option)
		{
			case 'q':
			{
                               //when q is pressed exit program
				cout << "exiting " << endl;
				condition = 0;				
			}
			break;

			default:
			{
                                // default action when any key is pressed.
				cout<<"And so you continue..."<<endl;
				cout << "Hello. Continue? ENTER=yes, 'q'=no" << endl;
				option = getch();
			}
			break;
		}			
	}
     
	int exit;
	cin >> exit;
	return 0;
}
Last edited on
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
    }
    else if (ch == 'b')
    {
        // action for b
    }
    else if (ch == 'c')
    {
        // action for c
    }
    else 
    {
        // default action 
    }


However, your requirement is simpler,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace 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.

Instead of this:
1
2
    if(cin.get() !='q'){goto Next1;}
    if(cin.get() =='q'){goto Escape;}

it is better to use else
1
2
3
4
    if (cin.get() !='q') 
        goto Next1;
    else
        goto Escape;
Last edited on
thank you all for your time and help! One day I become mentor too ;-D

but until then I gather experience and from now on I know this little usefull code:

if (cin.get() !='q')
goto Next1;
else
goto Escape;

thanks again!
Topic archived. No new replies allowed.