C++ Menu using loops

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;
}
Read out loud your logic on line 32. It will prevent you doing all the important bits inside that block.
@lastchance

should i change '!=' to '==' ?

Think it through. Can (a == 1 && a == 2) ever be true?
I assume you want to use || (OR).
@ganado

Ah I see now. I've been working/reading about this for hours. I should probably take a break. Thanks a lot.
Why not just
 
while (usrinput >= 1 && usrinput <= 4)



Have you covered iterators yet - or are you allowed to use them? If yes, then fir output reverse:

1
2
3
4
5
	// Output reverse
	for (auto stitr {sntinput.crbegin()}; stitr != sntinput.crend(); ++stitr)
		std::cout << *stitr;

	std::cout << '\n';



PS You're first checking for values 1 - 4 and then within the loop checking for -111???

You need something like:

 
while ((usrinput >= 1 && usrinput <= 4) || usrinput == -111)


but why this loop at all - you don't need it.

Also don't use a constant such as 32. Instead use 'a' - 'A'

PPS Have you covered - or can you use - range-based for loop? eg:

1
2
3
4
	// To lower
	for (auto& c : sntinput)
		if (c >= 'a' && c <= 'z')
			c -= 'a' - 'A';


Also note that you test for upper/lower case is incorrect - you use > and < rather than >= and <= - so you exclude 'a'/'A' and 'z'/'Z' !
Last edited on
You shouldn't have a loop at line 32 at all. After all, you don't change usrinput inside the loop, so the value won't change. If they enter a value 1-4 then the loop will run forever. Instead, add a final else at line 80 that does something like cout << usrinput << " is invalid\n";

You ask for the sentence every time you print the menu. I think you want to enter the sentence first, before you go into the menu loop. So move lines 27 & 28 above line 17. That means you'll want to move lines 19-22 also, and maybe change the wording since they won't see the menu until after they enter the sentence.

Get the basic menu structure working first. In other words, don't worry if the output from a given menu selection is wrong. Once you have the menu structure working, you can worry about whether each selection does the right thing.

all I found were switch menus, etc

The switch statement is really handy for processing menu selections because it's very clear that your doing something different for different values of a single expression:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch(usrinput) {
case 1:
      // code to handle menu option 1
      break;
case 2:
      // code to handle menu option 2
      break;
case 3:
      // code to handle menu option 3
      break;
case 4:
      // code to handle menu option 4
      break;
case -111:
      // code to handle menu option -111
      break;
default:
      // code to handle bad menu selection
      break;
}
Topic archived. No new replies allowed.