Need help not allowing a negative number to be input
Expand Post »
I do not want a negative number input for radius, length, width, base, height. How would I do this. My code is below. It should be pretty self explanatory.
#include <iostream>
#include <cmath>
using namespace std;
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
int main ()
{
double const pi=3.14159;
int radius,
length,
width,
base,
height,
choice;
double area;
//Display the menu and get the user's choice
cout << " Geometry Calculator \n\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 Trainge\n";
cout << "4. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-4): ";
cin >> choice;
cout << "\n";
if (choice==1)
{cout << "Please enter the radius of the circle: ";
radius = GetNonNegativeInteger ();
area = radius * pi;
cout << "The area of the circle is: " << area << endl;}
else if (choice==2)
{cout << "Please enter the length of the rectangle: ";
length = GetNonNegativeInteger ();
cout << "Please enter the width of the rectangle: ";
width = GetNonNegativeInteger ();
area = length * width;
cout << "The area of the rectangle is: " << area << endl;}
else if (choice==3)
{cout << "Please enter the base length of the triangle: ";
cin >> base;
cout << "Please enter the height of the triangle: ";
cin >> height;
area = (base * height)/2;
cout << "The area of the triangle is: " << area << endl;}
else if (choice==4)
{cout << "Thanks for trying the Geometry Calculator\n";}
else
{cout << "You can only select options 1-4, run the program again and select a option 1-4\n"; }
return 0;
}
See red. You would do the same for base and height in option number 3. Have you used functions before? You need to change the commented parts of the function.
#include <iostream>
#include <cmath>
using namespace std;
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
int main ()
{
double const pi=3.14159;
int radius,
length,
width,
base,
height,
choice;
double area;
//Display the menu and get the user's choice
cout << " Geometry Calculator \n\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 Trainge\n";
cout << "4. Quit\n";
cout << "\n";
cout <<"Enter your choice (1-4): ";
cin >> choice;
cout << "\n";
if (choice==1)
{cout << "Please enter the radius of the circle: ";
radius = GetNonNegativeInteger ();
area = radius * pi;
cout << "The area of the circle is: " << area << endl;}
else if (choice==2)
{cout << "Please enter the length of the rectangle: ";
length = GetNonNegativeInteger ();
cout << "Please enter the width of the rectangle: ";
width = GetNonNegativeInteger ();
area = length * width;
cout << "The area of the rectangle is: " << area << endl;}
else if (choice==3)
{cout << "Please enter the base length of the triangle: ";
cin >> base;
cout << "Please enter the height of the triangle: ";
cin >> height;
area = (base * height)/2;
cout << "The area of the triangle is: " << area << endl;}
else if (choice==4)
{cout << "Thanks for trying the Geometry Calculator\n";}
else
{cout << "You can only select options 1-4, run the program again and select a option 1-4\n"; }
return 0;
}
See red. You would do the same for base and height in option number 3. Have you used functions before? You need to change the commented parts of the function.
I am very new at this. First time running C++. We have not learned functions yet. What do you mean change the commented parts of the function. I redid some code a little different then what you showed and it works.
We have not learned functions yet. What do you mean change the commented parts of the function. I redid some code a little different then what you showed and it works.
Yes, you did the same thing as what I was recommending, just you didn't use functions. What I meant by change the commented sections is to do what you did in your code:
Which is what you have in your code, just slightly different each time, which is correct. If you are not going to use a function, doing it the way you did it is correct and is equivalent. When you learn functions, you'll probably do it the way I did it since you are doing the same thing five times. You did it right.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.