Upper case inclusion/Invalid Entries

Thank you Framework for the help yesterday. I kind of went down a different path with my code though. I got it to work somewhat, but I have a couple more questions.

1) How do I also include that if a user inputs upper case Q, the program will quit?
2) How would I include statements where it would state "invlaid Entry" or something to that affect if you try inputting a letter other than V,A,G,or Q as well as one for if you try inputting a letter for the length, width or depth?

Thank You


#include <iostream>
#include <iomanip>

using namespace std;

const char q = 'Q';

int main ()
{
char option = ' ';
float volume, area, girth, length, width, depth;

while (option != q)
{
cout << "To determine Volume, enter V \n"
<< "To determine Surface Area, enter A \n"
<< "To determine Girth, enter G \n"
<< "To quit, enter Q\n";
cin >> option;

if (option == 'q')
break;

cout << "Please input the length: ";
cin >> length;
cout << "Please input the width: ";
cin >> width;
cout << "Please input the depth: ";
cin >> depth;

switch (option)
{
case 'v':
case 'V':
volume = length * width * depth;
cout << "The volume of the object is: " << volume << endl;
break;

case 'a':
case 'A':
area = 2 *(length * width) + 2 *(width * depth) + 2 *(length * depth);
cout << "The surface area of the object is: " << area << endl;
break;

case 'g':
case 'G':
girth = 2 * (length + width) + depth;
cout << "The girth of the object is: " << girth << endl;
break;
}

system("pause");
return 0;
}
}
Last edited on
a) Just check for both: if (option == 'q' || option == 'Q').
b) Invalid entries (i.e. no 'case' for it) can be caught by the "default" case, which will trigger for any option not covered by another case. I don't think cin >> needs typechecking. If you cin >> an int, it will ignore chars etc.
@Gaminic
You are misinformed about cin and type-checking. STL does not define anything about cin handling a case where you enter a char for an integer. Usually this will case a program to hit an exception and crash because of the improper casting.

@OP
To check for q or Q, simple use tolower on the user's input and reassign it to your variable. Then check. Then you only have to check for lowercase version of the characters.

You may need to include cctype, but testing this on most modern compilers reveals no need.
option = tolower(option);

For the cin check I offer this:
You need to have a loop after each cin for floats.
1
2
3
4
5
6
7
while(!cin) // If a flag is set on cin, such as badbit or EOF
{
   cin.ignore(0x7FFFFFFF, '\n'); // Remove everything from the stream up to the first '\n' (When you hit enter.)
   cin.clear(); // Reset all flags
   cout << "ERROR: Invalid Input. Please renter the length.\n >"; // Error message. Tell user something went wrong.
   cin >> length; // Get new value.
}


Change the cout statement and the cin statement to fit your need. Or you could change it to:
1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Enter the length.\n >";
while (true)
{
   cin >> length; // Get input for float.
   if (!cin) // Was the input bad?
   {
      cout << "Bad Input. Retry.\n >"; // Error message.
      cin.ignore(0x7FFFFFFF, '\n'); // Ignore the input
      cin.clear(); // Clear flags.
   }
   else
      break; // DO NOT PUT IN A {} BLOCK OR YOU WILL GET AN INFINITE LOOP
}


Of course a safer input would be to get the value as a std::string, test each character if it is number safe. (Also allow decimal points if doubles?). Then if all goes well cast that to the data value type. This method is a bit more complex, so I don't think you'll use it.

EDIT. I said integers everywhere but this will work for floats and doubles.
EDIT 2:
Proof for Gaminic on cin being a bit jumpy about its typecasting.
Pretty self explanatory. Compiled with GNU G++ 4.2 on Posix System and run from the terminal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// typcheckcin.cpp
// Test if cin will do a typecheck or the program will just crash.
#include <iostream>

int main ()
{
	double amIGonnaCrash;
	char safe;
	std::cout << "Enter a character that is not number safe. (A-Z,a-z)\n >";
	std::cin >> amIGonnaCrash;

	std::cout << "I should not ask for more input because cin is freaking out.\n >";
	std::cin >> amIGonnaCrash;
	std::cout << " >";
	std::cin >> safe;

	return 0;
}
Last edited on
Anyone able to tell me why when I enter an invalid length, width, depth it repeatedly loops ERROR: Invalid Input. Please reenter the .....??

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

using namespace std;

const char q = 'Q';

int main()
{
 char option = ' ';
 float volume, area, girth, length, width, depth;
 option = tolower(option); // converts option input to lower case
 
 while (option != q)
       {   
        cout << "To determine Volume, enter V \n"
             << "To determine Surface Area, enter A \n"
             << "To determine Girth, enter G \n"
             << "To quit, enter Q\n";
        cin >> option;
        
 if (option == 'q' || option == 'Q')
        break;
          
        cout << "Please input the length: ";
        cin >> length;
 while(!cin) // If a flag is set on cin, such as badbit or EOF
{
        cin.ignore(0x7FFFFFFF, '\n'); // Remove everything from the stream up to the first '\n' (When you hit enter.)
        cin.clear(); // Reset all flags
        cout << "ERROR: Invalid Input. Please reenter the length.\n >"; // Error message. Tell user something went wrong.
        cin >> length; // Get new value.
}
        cout << "Please input the width: ";
        cin >> width;
while(!cin)
{
        cin.ignore(0x7FFFFFFF, '\n');
        cin.clear();
        cout << "ERROR: Invalid Input. Please reenter the width.\n >";
        cin >> width;
}
        cout << "Please input the depth: ";
        cin >> depth;
while(!cin)
{
        cin.ignore(0x7FFFFFFF, '\n');
        cin.clear();
        cout << "ERROR: Invalid Input. Please reenter the depth.\n >";
        cin >> depth;
}
     
 switch (option)
       {
        case 'v':
        volume = length * width * depth;
        cout << "The volume of the object is: " << volume << endl;
        break;
        
        case 'a':
        area = 2 *(length * width) + 2 *(width * depth) + 2 *(length * depth);        
        cout << "The surface area of the object is: " << area << endl;
        break;
        
        case 'g':
        girth = 2 * (length + width) + depth;
        cout << "The girth of the object is: " << girth << endl;
        break;
       }  
        
        system("pause");
        return 0;
       }
}
Topic archived. No new replies allowed.