Help with validating user input

I am doing a problem from my book I think I have everything else right
except I can't seem to figure out how the validate choices. Help would be greatly appreciated.

(directions from the book ) Input validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do no accept negative values for the circle radius, the rectangle's length or width, or the triangle's base or height.

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
92
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()
{int choice ;// To hold menu choices
  
 const int choiceA = 1,// Constant choic3 value answers 1-4
		   choiceB = 2,
	       choiceC = 3,
	       choiceD = 4;
	
 

 
 // dislay the directions and intent of the program
 cout << " This program is a simple geometry calculator that will compute\n";
 cout << " the area of circle, rectangle, and triangle.\n";
 cout << " please select on of the following choices 1-4\n";
 cout << " any other other inputs besides 1-4 will cause the program to stop.\n\n";
 
 cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
 cout << " 2. To calculate Area of rectangle:\n";
 cout << " 3. To calculate Area of triangle:\n";
 cout << " 4. To quit:\n\n\n";

 cout << " What is your choice ?\n\n";
 cin >> choice;

	if (choice == choiceA)// The if / else for cirlce area
	{
	
		const double PI = 3.14159;// const for pi
		double circleArea;// variable to hold cirlce area equation
		double radius;// user input variable

		cout << " Enter the radius :\n";
		cin >> radius;
		circleArea = PI * pow(radius, 2.0);// to do radius to the snd power this is needed
		cout << " The area of a circle is " << setprecision(5)  << circleArea << "." << endl;
		
	}
	
	
		else if (choice == choiceB)// Display the output and choices #2
		{
				double lenght;//Variables for the lenght, width, and output
				double width;
				double recOutput;
				cout << " Enter the lenght of rectangle: ";
				cin >> lenght;
				cout << " Enter the width of rectangle: ";
				cin >> width;
				recOutput = lenght * width;
				cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";


		}

	else if ( choice == choiceC)//For the triangle
	{
		double triBase;//Variables for triangle base, height, output
		double triHeight;
		double triOutput;
		const double triangleopp = .5;// constant variable to hole opperand .5

		cout << " Enter the Base: ";
		cin >> triBase;
		cout << " Enter the Height: ";
		cin >> triHeight;
		triOutput = triangleopp * triBase * triHeight;
		cout << "The area of the triangle is " << setprecision(7) << showpoint << triOutput << ".\n";
	}

		else if (choice == choiceD)// the quit option
		{
		string optionQuit;
			cout << " Do you wan to quit the program? ";
			cin >> optionQuit;
			cout << " The program will now quit\n";
		}





}


Last edited on
Hello mollywhoppinRBG,

This with the comments in the code is as far as I have reached:
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

//using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

// <--- A better option.
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::showpoint;
using std::setprecision;
using std::string;

int main()
{
	int choice;// To hold menu choices

	const int choiceA = 1,// Constant choic3 value answers 1-4
		choiceB = 2,
		choiceC = 3,
		choiceD = 4;

	// dislay the directions and intent of the program
	cout << " This program is a simple geometry calculator that will compute\n";
	cout << " the area of circle, rectangle, and triangle.\n";
	cout << " please select on of the following choices 1-4\n";
	cout << " any other other inputs besides 1-4 will cause the program to stop.\n\n";

	// **********************************************************
	//                 NEEED TO VALIDATE INPTU.

	cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
	cout << " 2. To calculate Area of rectangle:\n";
	cout << " 3. To calculate Area of triangle:\n";
	cout << " 4. To quit:\n";  // <--- Changed.
	cout << "  What is your choice ? ";  // <--- Changed.
	cin >> choice;

	//                   Maybe a do/while loop.
	// **********************************************************

	if (choice == choiceA)// The if / else for cirlce area
	{

		const double PI = 3.14159;// const for pi
		double circleArea;// variable to hold cirlce area equation
		double radius;// user input variable

		cout << " Enter the radius :\n";
		cin >> radius;

		// **********************************************************
		//                 NEEED TO VALIDATE INPTU.
		// **********************************************************

		circleArea = PI * pow(radius, 2.0);// to do radius to the snd power this is needed

		cout << " The area of a circle is " << setprecision(5) << circleArea << "." << endl;
	}

	else if (choice == choiceB)// Display the output and choices #2
	{
		double lenght;//Variables for the lenght, width, and output
		double width;
		double recOutput;

		cout << " Enter the lenght of rectangle: ";
		cin >> lenght;

		// **********************************************************
		//                 NEEED TO VALIDATE INPTU.
		// **********************************************************

		cout << " Enter the width of rectangle: ";
		cin >> width;

		// **********************************************************
		//                 NEEED TO VALIDATE INPTU.
		// **********************************************************

		recOutput = lenght * width;

		cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
	}

	else if (choice == choiceC)//For the triangle
	{
		const double triangleopp = .5;// constant variable to hole opperand .5

		double triBase;//Variables for triangle base, height, output
		double triHeight;
		double triOutput;

		cout << " Enter the Base: ";
		cin >> triBase;

		// **********************************************************
		//                 NEEED TO VALIDATE INPTU.
		// **********************************************************

		cout << " Enter the Height: ";
		cin >> triHeight;

		triOutput = triangleopp * triBase * triHeight;

		cout << "The area of the triangle is " << setprecision(7) << showpoint << triOutput << ".\n";
	}

	else if (choice == choiceD)// the quit option
	{
		string optionQuit;

		cout << " Do you wan to quit the program? ";
		cin >> optionQuit;

		cout << " The program will now quit\n";
	}
}

What you would do for lines 38 - 43 would need to be done for the other inputs. If you are having a problem with this just say so.

Andy

Edit:

P.S. watch your indenting and use of {}s. Be consistent with their use.
Last edited on
Hello mollywhoppinRBG,

There are several ways to validate what you need. Also you need to consider what needs checked.

This is just one example of what you could do:
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
int main()
{
	int choice;// To hold menu choices

	const int // Constant choic3 value answers 1-4
		choiceA = 1,
		choiceB = 2,
		choiceC = 3,
		choiceD = 4;

	// dislay the directions and intent of the program
	cout  // <--- An alternative way of writing this.
		<< " This program is a simple geometry calculator that will compute\n"
		<< " the area of circle, rectangle, and triangle.\n"
		<< " please select on of the following choices 1-4\n" << " any other other"
		<< "inputs besides 1-4 will cause the program to stop.\n"  // <--- 2 "\n"s should work and flush the output buffer.
                << std::endl;  // <--- Or save the "endl" for after the last line.

	// **********************************************************
	//                 NEED TO VALIDATE INPUT.

	do
	{
		cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
		cout << " 2. To calculate Area of rectangle:\n";
		cout << " 3. To calculate Area of triangle:\n";
		cout << " 4. To quit:\n";  // <--- Changed.
		cout << "  What is your choice ? ";  // <--- Changed.
		cin >> choice;

		if(!cin || choice < 1 || choice > 4)
		{ 
			if (!cin)
			{
				std::cout << "\n    Invalid input! Your choice must be a number between (1 and 4)\n\n";

				std::cin.clear();
				std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
			}
			else if (choice < 1 || choice > 4)
			{
				std::cout << "\n    Invalid choice! Your choice must be a number between (1 and 4)\n\n";
			}
		}
	} while (choice < 1 || choice > 4);
	//                   Maybe a do/while loop.
	// ********************************************************** 

Just an idea to get you started.

Andy
Topic archived. No new replies allowed.