View Single Post
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Need help not allowing a negative number to be input

 
0
  #1
Nov 17th, 2008
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]
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8.  
  9. {
  10. double const pi=3.14159;
  11. int radius,
  12. length,
  13. width,
  14. base,
  15. height,
  16. choice;
  17. double area;
  18.  
  19.  
  20.  
  21. //Display the menu and get the user's choice
  22. cout << " Geometry Calculator \n\n";
  23. cout << "1. Calculate the Area of a Circle\n";
  24. cout << "2. Calculate the Area of a Rectangle\n";
  25. cout << "3. Calculate the Area of a Trainge\n";
  26. cout << "4. Quit\n";
  27. cout << "\n";
  28.  
  29. cout <<"Enter your choice (1-4): ";
  30. cin >> choice;
  31. cout << "\n";
  32.  
  33. if (choice==1)
  34. {cout << "Please enter the radius of the circle: ";
  35. cin >> radius;
  36. area = radius * pi;
  37. cout << "The area of the circle is: " << area << endl;}
  38.  
  39. else if (choice==2)
  40. {cout << "Please enter the length of the rectangle: ";
  41. cin >> length;
  42. cout << "Please enter the width of the rectangle: ";
  43. cin >> width;
  44. area = length * width;
  45. cout << "The area of the rectangle is: " << area << endl;}
  46.  
  47. else if (choice==3)
  48. {cout << "Please enter the base length of the triangle: ";
  49. cin >> base;
  50. cout << "Please enter the height of the triangle: ";
  51. cin >> height;
  52. area = (base * height)/2;
  53. cout << "The area of the triangle is: " << area << endl;}
  54.  
  55. else if (choice==4)
  56. {cout << "Thanks for trying the Geometry Calculator\n";}
  57.  
  58. else
  59. {cout << "You can only select options 1-4, run the program again and select a option 1-4\n"; }
  60.  
  61.  
  62. return 0;
  63.  
  64. }
Last edited by davids2004; Nov 17th, 2008 at 11:25 pm.
Reply With Quote