-
C++ (
http://www.daniweb.com/forums/forum8.html)
| davids2004 | Nov 17th, 2008 11:24 pm | |
| Need help not allowing a negative number to be input 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.
Thanks.
[CODE] #include <iostream>
#include <cmath>
using namespace std;
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: ";
cin >> radius;
area = radius * pi;
cout << "The area of the circle is: " << area << endl;}
else if (choice==2)
{cout << "Please enter the length of the rectangle: ";
cin >> length;
cout << "Please enter the width of the rectangle: ";
cin >> width;
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;
} |
| VernonDozier | Nov 18th, 2008 12:18 am | |
| Re: Need help not allowing a negative number to be input Write a function that does not allow for negative input:
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
Call this function from main when you need non-negative integer input. |
| davids2004 | Nov 18th, 2008 12:24 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by VernonDozier (Post 738365) Write a function that does not allow for negative input:
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
Call this function from main when you need non-negative integer input. | Now I am confused. |
| VernonDozier | Nov 18th, 2008 12:27 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by davids2004 (Post 738372) Where should this go in my code?
Thanks. |
Where should the function go or where should the function call go? |
| davids2004 | Nov 18th, 2008 12:28 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by VernonDozier (Post 738375) Where should the function go or where should the function call go? | With what you did what part of my code would I add that in. I am just not getting it. I do not have any variable called input. I am lost. |
| davids2004 | Nov 18th, 2008 12:36 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by VernonDozier (Post 738375) Where should the function go or where should the function call go? | Ok I got it. Here is my new code
[CODE] #include <iostream>
#include <cmath>
using namespace std;
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: ";
cin >> radius;
while (radius<0)
{cout << "Please enter a positive number: ";
cin >> radius;}
area = radius * pi;
cout << "The area of the circle is: " << area << endl;}
else if (choice==2)
{cout << "Please enter the length of the rectangle: ";
cin >> length;
while (length<0)
{cout << "Please enter a positive number: ";
cin >> length;}
cout << "Please enter the width of the rectangle: ";
cin >> width;
while (width<0)
{cout << "Please enter a positive number: ";
cin >> width;}
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;
while (base<0)
{cout << "Please enter a positive number: ";
cin >> base;}
cout << "Please enter the height of the triangle: ";
cin >> height;
while (height<0)
{cout << "Please enter a positive number: ";
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;
} |
| VernonDozier | Nov 18th, 2008 12:37 am | |
| Re: Need help not allowing a negative number to be input #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. |
| davids2004 | Nov 18th, 2008 12:40 am | |
| Re: Need help not allowing a negative number to be input Ok here is my final code. Cleaned it up a bit so it would not just say enter a positive number each time a negative number was entered
#include <iostream>
#include <cmath>
using namespace std;
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: ";
cin >> radius;
while (radius<0)
{cout << "Negative radius not allowed, enter a positive radius: ";
cin >> radius;}
area = radius * pi;
cout << "The area of the circle is: " << area << endl;}
else if (choice==2)
{cout << "Please enter the length of the rectangle: ";
cin >> length;
while (length<0)
{cout << "Negative length not allowed, enter a positive length: ";
cin >> length;}
cout << "Please enter the width of the rectangle: ";
cin >> width;
while (width<0)
{cout << "Negative width not allowed, enter a positive width: ";
cin >> width;}
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;
while (base<0)
{cout << "Negative base not allowed, enter a positive base: ";
cin >> base;}
cout << "Please enter the height of the triangle: ";
cin >> height;
while (height<0)
{cout << "Negative height not allowed, enter a positive height: ";
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;
} |
| davids2004 | Nov 18th, 2008 12:42 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by VernonDozier (Post 738380) #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. |
| VernonDozier | Nov 18th, 2008 1:02 am | |
| Re: Need help not allowing a negative number to be input Quote: Originally Posted by davids2004 (Post 738388) 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:
int GetNonNegativeInteger ()
{
int input;
cin >> input;
while (input < 0)
{
// display error message.
// ask for input again
}
return input;
}
Change the red code to this:
cout << "Negative number not allowed." << endl;
cin >> input;
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. |
| All times are GMT -4. The time now is 10:46 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC