When looping a switch, my "pause" runs twice

Hey guys, I am back because of the incredible help I see throughout this forum. I am currently having a problem when trying to loop a switch statement and then pausing after a selection is made..

Here is my code:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <algorithm>


using namespace std;

void myMenu();

int main()
{
    char myChoice;;
    myMenu();
    
    cout << "\nPlease enter a selection: ";
    cin >> myChoice;
    
    do
    {
        
    switch (toupper(myChoice))
    {
        case 'A': cout << "Placeholder for A \n";
                  cout << endl << "(Press Enter key to continue)" << endl;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  break;
            
        case 'B': cout << "Placeholder for B \n";
                  cout << endl << "(Press Enter key to continue)" << endl;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  break;
            
        case 'C': cout << "Placeholder for C \n";
                  cout << endl << "(Press Enter key to continue)" << endl;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  break;
            
        case 'D': cout << "Placeholder for D \n";
                  cout << endl << "(Press Enter key to continue)" << endl;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  break;
            
        case 'E': cout << "Placeholder for E \n";
                  cout << endl << "(Press Enter key to continue)" << endl;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');
                  break;
            
        case 'F': cout << "\nExiting program" << endl;
                  exit(EXIT_SUCCESS);
            
        default: cout << "\nInvalid selection" << endl;
                 cout << endl << "(Press Enter key to continue)" << endl;
                 cin.ignore(numeric_limits<streamsize>::max(), '\n');
                 break;
            
    }
    
    } while(myChoice != 'F');
    
    
    return EXIT_SUCCESS;
}

void myMenu()
{
    cout << "Text Edit \n" <<endl;
    
    cout << "A - New Text \n";
    cout << "B - Append To Text \n";
    cout << "C - Display Text \n";
    cout << "D - Display Text Sorted \n";
    cout << "E - Display Text Statistics \n";
    cout << "F - Exit \n";
}


It is not finished but before I plug in all the information, I am working on the most puzzling parts.

When I run it and I enter H as my choice it naturally defaults and runs
1
2
3
4
 default: cout << "\nInvalid selection" << endl;
                 cout << endl << "(Press Enter key to continue)" << endl;
                 cin.ignore(numeric_limits<streamsize>::max(), '\n');
                 break;


Now my problem with this is the output.
Text Edit 

A - New Text 
B - Append To Text 
C - Display Text 
D - Display Text Sorted 
E - Display Text Statistics 
F - Exit 

Please enter a selection: h

Invalid selection

(Press Enter key to continue)

Invalid selection

(Press Enter key to continue)


As you can see I get
Invalid selection

(Press Enter key to continue)
more than once when I would love for the actual output to be
Invalid selection

(Press Enter key to continue)
only once.

Now a HUGE problem I am having that I cannot figure out is I am trying to get it to re-run the entire main function after I press enter.

I have got it to re-run if I place main(); right before break; in the default code. But the problem with this is that it does not actually pause and give me a chance to press enter before it runs again.
So when I try that, the output is this:
Text Edit 

A - New Text 
B - Append To Text 
C - Display Text 
D - Display Text Sorted 
E - Display Text Statistics 
F - Exit 

Please enter a selection: h

Invalid selection

(Press Enter key to continue)
Text Edit 

A - New Text 
B - Append To Text 
C - Display Text 
D - Display Text Sorted 
E - Display Text Statistics 
F - Exit 

Please enter a selection: 


I guess after this super long explanation, I would love to know how to get it to allow me to press enter before re-running the main function after it defaults and how to get it to run
Invalid selection

(Press Enter key to continue)
only once after I enter something.

Thanks in advance guys!

EDIT: I forgot to mention. If it defaults and tells me
Invalid selection
and I try to enter a valid selection, it just re-runs
Invalid selection

(Press Enter key to continue)
over again, same with any selection. I enter
A
and try to enter
B
it still runs
A
.
Last edited on
I think you want this in the do..while loop (and remove the do..while loop which is around your switch statement).
1
2
3
4
5
6
do
{
    cout << "\nPlease enter a selection: ";
    cin >> myChoice;
}while(myChoice != 'A' && myChoice != 'B' && myChoice != 'C'); //etc 
 


This should also resolve the problem you commented on in your edit. :)

Edited repeatedly: I just wasn't happy with the post each time for whatever reason. <_<
Last edited on
Your 'cin >>' (Line 17) should be inside of your 'do loop' and your compiler should not even allow you to call main().
I think you want this in the do..while loop (and remove the do..while loop which is around your switch statement).


I changed it to this:
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
43
44
45
46
47
 do
    {
        cout << "\nPlease enter a selection: ";
        cin >> myChoice;
        
        switch (toupper(myChoice))
        {
            case 'A': cout << "Please enter new text \n";
                cout << "You have entered " << endl;
                cout << endl << "(Press Enter key to continue)" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;
                
            case 'B': cout << "Placeholder for B \n";
                cout << endl << "(Press Enter key to continue)" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;
                
            case 'C': cout << "Placeholder for C \n";
                cout << endl << "(Press Enter key to continue)" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;
                
            case 'D': cout << "Placeholder for D \n";
                cout << endl << "(Press Enter key to continue)" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;
                
            case 'E': cout << "Placeholder for E \n";
                cout << endl << "(Press Enter key to continue)" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                break;
                
            case 'F': cout << "\nExiting program" << endl;
                exit(EXIT_SUCCESS);
                
            default: cout << "\nInvalid selection" << endl;
                cout << endl << "(Press Enter key to continue)\n" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');\
                main();
                break;
                
        }

        
    } while(myChoice != 'A' && myChoice != 'B' && myChoice != 'C' && myChoice != 'D' && myChoice != 'E');
    


I left the switch inside the loop because it doesn't run right when I take it out. Odd, I know.

your compiler should not even allow you to call main().


I am using Xcode and it lets me call it... Weird if it isn't supposed to be called.

BONUS QUESTION

I would love to be able to press H and get my
Invalid selection
output then actually be given a chance to pause the program and to press enter to re-run the menu. It always says
(Press enter key...)
and then jumps right into the loop.

Also, how would I press 'A', enter text, grab that text and store it to be used when the function loops and I choose a different action to be used with the text entered?

Thanks!!!
You shouldn't call main(); in the middle of a program because it calls a new copy of main to run! So never ever ever do this! Ever.

There is no reason to try and call main() where you did anyway. Just take it out and the program will run fine.

To grab text, do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

int main()
{
    string s;
    cout << "Type something followed by return: \n";
    getline(cin, s);//Getline will grab everything until return key is pressed. 
    cout << "\n" << s;
    return 0;
}



Thanks for the tip Mats, I changer it to myMenu(); instead and it re-displays my menu like I want.

as for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    char myChoice;;
    myMenu();
    string s;

    do
    {
        cout << "\nPlease enter a selection: ";
        cin >> myChoice;
        
        switch (toupper(myChoice))
        {
            case 'A': cout <<"Please enter your text: ";
                      getline(cin, s);
                      cout << "\nYou entered: " << s;
                      cin.ignore(numeric_limits<streamsize>::max(), '\n');
                      break;


I enter the text and this is my output:
Text Edit 

A - New Text 
B - Append To Text 
C - Display Text 
D - Display Text Sorted 
E - Display Text Statistics 
F - Exit 

Please enter a selection: a
Please enter your text: 
You entered: Michael Gilbert

Please enter a selection: 


EDIT:
I would like for the output to be
Please enter a selection: a
Please enter your text: Michael Gilbert
You entered: Michael Gilbert


It completely skips
Please enter your text
and has me enter my text in front of
You entered:
Last edited on
Try putting cin.clear() before the switch statement. Should clear the stream of anything that can cause problems.
Try putting cin.clear() before the switch statement. Should clear the stream of anything that can cause problems.


I put that in but it doesn't seem to have changed anything. I am still getting the same problem.

I'm just confused why it won't allow me to enter my selection (a) then enter my text in front of where it asks me to, and then have it output
You entered: (Whatever I typed in)

I don't mean to be a pain in the buns and I am sorry if I'm being one, I'm just not 100% understanding yet :)
UPDATE:

Okay guys I changed it around a little bit and got it to read how I wanted:
1
2
3
4
5
6
7
8
9
10
switch (toupper(myChoice))
        {
            case 'A': cin.ignore();
                      cout <<"\nPlease enter your text: ";
                      getline(cin, s);
                cout << "\nYou entered: " << s << endl;
                      cout << endl << "(Press Enter key to continue)\n" << endl;
                      cin.ignore(numeric_limits<streamsize>::max(), '\n');
                      myMenu();
                      break;


This now outputs how I wanted and also pauses it and gives me a chance to press enter before my menu pops back up. I will continue to keep everyone update so maybe my posts can help someone with a similar problem!

Thanks again, any further help would be awesome too :)
Topic archived. No new replies allowed.