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
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
and try to enter
it still runs
.