Hello all. I'm having a hard time with my menu, as we have only just begun to learn loops. I tried to find what I specifically needed before I decided to create a post, but all I found were switch menus, etc.
INFO: Create a menu that has 4 selections. Ask the user to enter any sentence: ex. "This is a Test". Then ask the User to make a selection:
(1)Display middle character if applicable.
(2)Convert the entire string to UpperCase.
(3)Convert entire string to LowerCase.
(4)Display the entire string backwards.
Enter -111 to end the program.
PROBLEM: I enter a sentence, then I make my selection. Instead of executing the selection, it's looping back to the menu selection:
RESTRICTIONS: Not allowed to use the tolower() method, toupper() method, vectors, or any method that reverses the sentence automatically. only allowed to have one “return 0” statement in the program. If a function is used that hasn't been covered, it must be cleared first.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string sntinput; // sentence input
string upper = sntinput;
string lower = sntinput;
int usrinput; // user input
int menu = true;
while (menu == true)
{
cout << "=============================================================================" << endl;
cout << "Welcome to my program. Enter a sentence and select one of the options below. " << endl;
cout << "Enter -111 to exit the program. " << endl;
cout << "=============================================================================" << endl;
cout << "1. Display middle character if applicable." << endl; // if user selects 1, display middle character if word is odd
cout << "2. Convert to uppercase." << endl; // if user selects 2, convert the whole sentence to UC
cout << "3. Convert to lowercase." << endl; // if user selects 3, convert the whole sentence to LC
cout << "4. Display backwards." << endl; // if user selects 4, display the sentence backwards
cout << "Enter a sentence: " << endl;
getline(cin, sntinput);
cout << "Selection: " << endl; // if selection is 1 - 4 run option; if selection is < 1 or > 5 , display error until a proper selection is entered.
cin >> usrinput;
while (usrinput !=1 && usrinput !=2 && usrinput !=3 && usrinput !=4)
{
if (usrinput == 1) // display middle character // *figure out how to do this.
{
cout << "Middle:" << endl;
cout << "=======" << endl;
}
else if (usrinput == 2) // convert to uppercase // fix this
{
for (int i = 0; i < sntinput.length(); i++)
{
if (sntinput.at(i) > 'a' && sntinput.at(i) < 'z')
{
upper.at(i) = upper.at(i) - 32;
}
}
cout << "Uppercase:" << endl;
cout << "==========" << endl;
}
else if (usrinput == 3) // convert to lowercase // fix this
{
for (int i = 0; i < sntinput.length(); i++)
{
if (sntinput.at(i) > 'A' && sntinput.at(i) < 'Z')
{
upper.at(i) = upper.at(i) + 32;
}
cout << "Lowercase:" << endl;
cout << "==========" << endl;
}
else if (usrinput == 4) // display sentence backwards // *figure out how to do this.
{
cout << "Backwards:" << endl;
cout << "==========" << endl;
}
else (usrinput == -111); // display "goodbye"
{
cout << "Thanks for using my program. Goodbye!" << endl;
menu = false;
}
}
//you have good input
}
return 0;
}
|