I am trying to sort out a menu where the user gives the program some input then has five options to select from.
Three (A/B/C) do a thing with calculations and function calls, which work perfectly. Two (D/E) allow the user to either start the program over or end the program.
The core of the issue lies in starting the program over. Using a while loop, I can end the program no problem if the other options aren't selected -- as long as option D(start the program over) is omitted. But I cannot get the program to start over.
#include <iostream>
#include <cstring>
usingnamespace std;
// Main
int main() {
// Variables
char input = '0';
char choice = '0';
// Gather User Input
cout << "Give the program some input: ";
cin >> input;
// Ask user what they want to do
cout << "\nA) Does a thing.\n"
<< "B) Does another thing.\n"
<< "C) Does a third thing. \n"
<< "D) Start the program over.\n"
<< "E) Exit.\n\n";
cout << "Please make a choice: " ;
cin >> choice;
// Check to see if the user wants to continue.
while(choice != 'e' && choice != 'E')
{
if (choice == 'a' || choice == 'A')
{
cout << "You selected A! A function was called and a thing happened.";
break;
}elseif (choice == 'b' || choice == 'B')
{
cout << "You selected B! A function was called and a thing happened.";
break;
}elseif (choice == 'c' || choice == 'C')
{
cout << "You selected C! A function was called and a thing happened.";
break;
}elseif (choice == 'd' || choice == 'D')
{
cout << "Give the program some input: ";
cin >> input;
}
}
cout << "\nProgram has ended.";
return 0;
} // End Main
I figured out a solution. Instead of using a while loop I used switch statement nested in a do/while loop. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
do{
// menu
// option a
// option b
// option c
// option d start program over
// exit program
switch (choice)
{
case'A': // things are happening!
// case B
// case C
// so on and so forth
}
}while(); // parameters
Hope this helps anyone else experiencing a similar problem!