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

Re: Need help not allowing a negative number to be input

 
0
  #6
Nov 18th, 2008
Originally Posted by VernonDozier View Post
Where should the function go or where should the function call go?
Ok I got it. Here is my new code

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