Little help with using SENTINEL

Okay, so I'm sort of stuck on using SENTINEL to end my program when someone types Q or q. Any help on this would be appreciated, I'm not sure where I'm going wrong at, so if someone could give me a shove in the right direction, I'd appreciate it. As stated my issue is when pressing Q, the program ignores it and just continues on as if I was typing in the letter A. Program will terminate after asking for user to input Length, Width and Depth but not before asking that, as it should.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;


const int SENTINEL = 'Q';  // Type this in to close our loop
int main()
{
    char opLetter = ' ';
    float volume, length, width, depth, area, girth;
    
    while(opLetter != SENTINEL)
    
{
    cout << "For Area type A, or a\n"
         << "For Volume type V, or v\n"
         << "For Girth type G, or g\n"
         << "To exit this program type Q, or q"<< endl;
    cin >> opLetter;
    
//Asking user to input Length, Width and Depth

cout << "Please enter your length, width and depth " << endl;
cin >> length >> width >> depth;

//Determines which letter was pressed to compute what user asked for
switch (opLetter)
{
       case 'a':
       case 'A':
area = 2 * (length*width) + 2 * (width*depth) + 2 *(length*depth);
cout << "The area is " << area << endl;
break;
       case 'v':
       case 'V':
volume = length * width * depth;
cout << "The volume is " << volume <<endl;
break;
       case 'g':
       case 'G':
girth = 2 * (width + length) + depth;
cout << "The Girth is " << girth <<endl;
break;
}


system("pause");
return 0;

}

}
Last edited on
First off the SENTINEL is only equal to 'Q' and not 'q' so the user has to enter 'Q'..
and if you want it to terminate if that hit 'Q' then just add an if statement after you cin >> opLetter
@colter I'm a little confused by what you mean, but, if I am reading it correctly, it should look something like this right


1
2
3
4
5
6
if (opLetter == 'Q')
	{
		cout<<" Program Terminated" <<endl;
                break;
                
	}


something similar to this right?

Also, yea, I think I need to add a tolower in the define of Q that way either or will work.
or you could just get rid of the sentinel and do
if (opLetter == 'Q' || opLetter == 'q')
{
cout << "Program terminated" << endl;
return 0; or break;
}
Worked like a charm, thanks again for the push Colter.
lol no problem :)
Topic archived. No new replies allowed.