how to make it where the cin only execpt positive number and not negative or letters

closed account (yTRM92yv)
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
//Audy L Sain	
//Week 8 Assignment 1
//Geometry Calculator

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//Functions
void showMenu();


											    
int main()
{
	int choice; //Menu choices
	float pi = 3.14159;
	float radius, c, Length, Width, p, base, hight, y, thight, b1, b2,
				s, sr, srr;

	const int	Circle_Area = 1,
				Rectangle_Area = 2,
				Triangle_Area = 3,
				Trapezoid_Area = 4,
				Sphere_Area = 5,
				Exit = 6;

	do
	{
		showMenu();
		cin >> choice;

		while (choice < Circle_Area || choice > Exit)
		{															
			cout << "Please enter a valid menu choice: ";
			cin >> choice;
		}


	if (choice != Exit)
		{

			switch (choice)
			{
			case Circle_Area:
					cout << "What is the radius of the circle  :";
					cin >> radius;
					c = pi * pow(radius, 2);
					cout << "\nThe Area of the Circle is  :" << c << endl;
					break;

				case Rectangle_Area:
					cout << "What is the Rectangle length  :";
					cin >> Length;
					cout << "\nWhat is the Rectangle width  :";
					cin >> Width;
					p = Length * Width;
					cout << "\nThe Area of the Rectangle is  :" << p << endl;
					break;

				case Triangle_Area:
					cout << "What is the length of the Triangle base  :";
					cin >> base;
					cout << "\nWhat is the hight of the Triangle  :";
					cin >> hight;
					y = base * hight * 0.5;
					cout << "\nThe Area of the Triangle is  :" << y << endl;
					break;

				case Trapezoid_Area:
					cout << "What is the hight of the Trapezoid  :";
					cin >> thight;
					cout << "\nWhat is the first base length  :";
					cin >> b1;
					cout << "\nWhat is the secound base length  :";
					cin >> b2;
					s = hight / 2 * (b1 + b2);
					cout << "\nThe Area of the Trapezoid is  :" << s << endl;
					break;

				case Sphere_Area:
					cout << "What is the radius of the Sphere  :";
					cin >> sr;
					srr = 4 * pi * pow(sr, 2);
					cout << "\nThe area of the sphere is  :" << srr << endl;
					break;
			}
		}


	} 
	while (choice != Exit);

	return (0);
}

//Menu Function
void showMenu()
{
	cout << "Please choose a selection" << endl << endl
		<< "1. Calculate the Area of a Circle" << endl
		<< "2. Calculate the Area of a Rectangle" << endl
		<< "3. Calculate the Area of a Triangle" << endl
		<< "4. Calculate the Area of a Trapezoid" << endl
		<< "5. Calculate the Area of a Sphere" << endl
		<< "6. Exit" << endl << endl;
}

Last edited on
closed account (yTRM92yv)
it works fine. it's just i need it where it will tell the user to input a valid number. On the menu it got it to work but you can still put letter in and it goes into a repeating loop and where the cases are it allows me to put negative numbers and letters and i cant have that
On the menu it got it to work but you can still put letter in and it goes into a repeating loop

Since choice is an int you could do something like:
1
2
3
4
5
6
7
8
9
10
...
        showMenu();
        cin >> choice;
        while (!cin || choice < Circle_Area || choice > Exit){
            cout << "Please enter a valid menu choice: ";
            cin.clear();  // Clear the error state if any.
            cin.ignore(1000, '\n');  // Discard any characters remaining in the input buffer.
            cin >> choice;
        }
...


The !cin checks the stream state for an error. Remember if you try to enter a character when the stream expects an int the stream will enter an error state.

where the cases are it allows me to put negative numbers and letters and i cant have that

If you want to disallow certain entries you'll need to do some input validation. In this case I recommend you create a helper function that can get the user input and return a "valid" number.

Also you'll probably get more help if you edit your post and place the code into code tags. Highlight the code then press the "<>" icon.

closed account (yTRM92yv)
well that worked with the menu it still allows me to enter negative and letters

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
if (choice != Exit)
		{

			switch (choice)
			{
			case Circle_Area:
					cout << "What is the radius of the circle  :";
					cin >> radius;
					c = pi * pow(radius, 2);
					cout << "\nThe Area of the Circle is  :" << c << endl;
					break;

				case Rectangle_Area:
					cout << "What is the Rectangle length  :";
					cin >> Length;
					cout << "\nWhat is the Rectangle width  :";
					cin >> Width;
					p = Length * Width;
					cout << "\nThe Area of the Rectangle is  :" << p << endl;
					break;

				case Triangle_Area:
					cout << "What is the length of the Triangle base  :";
					cin >> base;
					cout << "\nWhat is the hight of the Triangle  :";
					cin >> hight;
					y = base * hight * 0.5;
					cout << "\nThe Area of the Triangle is  :" << y << endl;
					break;

				case Trapezoid_Area:
					cout << "What is the hight of the Trapezoid  :";
					cin >> thight;
					cout << "\nWhat is the first base length  :";
					cin >> b1;
					cout << "\nWhat is the secound base length  :";
					cin >> b2;
					s = hight / 2 * (b1 + b2);
					cout << "\nThe Area of the Trapezoid is  :" << s << endl;
					break;

				case Sphere_Area:
					cout << "What is the radius of the Sphere  :";
					cin >> sr;
					srr = 4 * pi * pow(sr, 2);
					cout << "\nThe area of the sphere is  :" << srr << endl;
					break;
			}
		}


	} 
	while (choice != Exit);

	return (0);
}
Last edited on
well that worked with the menu it still allows me to enter negative

That's why I suggested the "helper" function to do input validation.


closed account (yTRM92yv)
hey thanks a lot for your help
Topic archived. No new replies allowed.