I had a problem that I had to create a menu that asks for user's input and depending on that input it will calculate area of circle, rectangle, or a triangle. I got that part worked out but the last part of the problem is "Do Not accept negative values for the user's input". How would I incorporate that into my problem? Here is my code.

``````    if (num < 1 || num > 4)
{
cout << "You have entered an invalid choice, Please try again\n";
}
else if (num == 1)
{
int rad;
double pie = 3.14159;
cout << "Please enter in the Radius of the Circle ";
cin >> rad;
double area = pie * (rad * rad);
cout << "The Area of the Cirle is " << area << endl;
}
else if (num == 2)
{
int len, wid;
cout << "Please enter the length of the Rectangle ";
cin >> len;
cout << "Please enter the width of the Rectangle ";
cin >> wid;
double area = wid * len;
cout << "The Area of the Rectangle is " << area << endl;
}
else if (num == 3)
{
int base, hei;
cout << "Please enter in the Base of the Triangle ";
cin >> base;
cout << "Please enter in the Height of the Triangle ";
cin >> hei;
double area = (.5 * base) * hei;
cout << "The Area of the Triangle is " << area << endl;
}
else if (num == 4)
{
cout << "You have opted out of the program, have a nice day!\n";
}
return 0;
}
``````

Well here

``if (num < 1 || num > 4)``

You're already checking if user input input is negative for the menu so just use that logic when you get input for the user's other values, for example

``if (base < 0)``

How would I enter that into my code? I tried:

``````else if (num == 1 || num < 0)
if (num < 0)
{
cout << "You cannot enter negative numbers, Please try again\n";
}
{
int rad;
double pie = 3.14159;

cout << "Please enter in the Radius of the Circle ";
cin >> rad;
double area = pie * (rad * rad);

cout << "The Area of the Cirle is " << area << endl;
``````

and else in front of the 2nd if statement as well.

``````int Num  = -1;
cin >> Num; //get input

while( Num < 0 ) //while input is less than 0 or negative
{
cout <<" Enter a positive number  : ";
cin >> Num;
}``````

I am suppose to make sure that the users do not enter a negative number after making a menu selection. I am not understanding what you are having me do firstperson. My code so far is:

``````#include <iostream>
using namespace std;

int main()
{
int num;

cout << "Geommetry Calculator Menu\n";
cout << "1. Calculate the Area of a Circle\n";
cout << "2. Calculate the Area of a Rectangle\n";
cout << "3. Calculate the Area of a Triangle\n";
cout << "4. Quit\n";
cout << "Please Enter in your choice: ";
cin >> num;

if (num < 1 || num > 4)
{
cout << "You have entered an invalid choice, Please try again\n";
}

else if (num == 1 || num < 0)
if (num < 0)
{
cout << "You cannot enter negative numbers, Please try again\n";
}
{
int rad;
double pie = 3.14159;

cout << "Please enter in the Radius of the Circle ";
cin >> rad;
double area = pie * (rad * rad);

cout << "The Area of the Cirle is " << area << endl;
}
else if (num == 2)
if (num < 0)
{
cout << "You cannot enter negative numbers, Please try again\n";
}
{
int len,
wid;

cout << "Please enter the length of the Rectangle ";
cin >> len;
cout << "Please enter the width of the Rectangle ";
cin >> wid;

double area = wid * len;
cout << "The Area of the Rectangle is " << area << endl;
}
else if (num == 3)
if (num < 0)
{
cout << "You cannot enter negative numbers, Please try again\n";
}
{
int base,
hei;

cout << "Please enter in the Base of the Triangle ";
cin >> base;
cout << "Please enter in the Height of the Triangle ";
cin >> hei;

double area = (.5 * base) * hei;
cout << "The Area of the Triangle is " << area << endl;
}
else if (num == 4)
{
cout << "You have opted out of the program, have a nice day!\n";
}
return 0;
}
``````

so after they make a selection for circle, rectangle, or triangle how can I prevent them from entering in negative numbers for when I ask for length, width, etc....

The code given to you by firstPerson makes the user input a new number while the current value (input of the variable) is less than zero. This repeats until the user enters a valid number.

you already have a non negative checker there :

``if (num < 1 || num > 4)``

just change it to :

``````while (num < 1 || num > 4){
cout<<"Invalid choice\n";
cin >> num;
}``````

And the rest after that, the else if(num == 1 || num < 0 { ... }
is not needed.

I was using num as an example. Forget about num, you're using num for your menu. You want to do it for each of your operations like this

``````else if (num == 1)
{
int rad = -1;
double pie = 3.14159;
while (rad < 0) {
cout << "Please enter in the Radius of the Circle ";
cin >> rad;
}
double area = pie * (rad * rad);
cout << "The Area of the Cirle is " << area << endl;
}``````

Why not use switch statment ? If you are familir with it.
This is a basic switch statment anyway.

``````switch(variable)
{
case 1:
//Do stuff
break;
case 2:
// Do stuff
break;
case number:
//do stuff
break;
Default:
/*Stuff in default will happen if the input to the switch statment doesn't correspond with any of the cases*/
break;
}``````

Here is a link

//K0ns3rv

We havent started using the While statement yet so I got confused on how to use it and incorporate it into my menu. I will try using the switch statement to see if that can resolve the non use of negative numbers (IE: -1, etc).

Anyways switch statments are easy to understand.
For your code it would be like this.

``````get variable(cin)
switch(variable)
{
case 1:
circle e.g
break;
case 2:
rectangle e.g
break;
triangle e.g
break;
default:
print "0 or other invalid value MAYDAY OMG ".
break;``````

//k0ns3rv

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.