Need help not allowing a negative number to be input

Thread Solved
Reply

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 Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #2
Nov 18th, 2008
Write a function that does not allow for negative input:

  1. int GetNonNegativeInteger ()
  2. {
  3. int input;
  4. cin >> input;
  5. while (input < 0)
  6. {
  7. // display error message.
  8. // ask for input again
  9. }
  10.  
  11. return input;
  12. }

Call this function from main when you need non-negative integer input.
Reply With Quote Quick reply to this message  
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
  #3
Nov 18th, 2008
Originally Posted by VernonDozier View Post
Write a function that does not allow for negative input:

  1. int GetNonNegativeInteger ()
  2. {
  3. int input;
  4. cin >> input;
  5. while (input < 0)
  6. {
  7. // display error message.
  8. // ask for input again
  9. }
  10.  
  11. return input;
  12. }



Call this function from main when you need non-negative integer input.
Now I am confused.
Last edited by davids2004; Nov 18th, 2008 at 12:28 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #4
Nov 18th, 2008
Originally Posted by davids2004 View Post
Where should this go in my code?

Thanks.

Where should the function go or where should the function call go?
Reply With Quote Quick reply to this message  
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
  #5
Nov 18th, 2008
Originally Posted by VernonDozier View Post
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.
Last edited by davids2004; Nov 18th, 2008 at 12:29 am.
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #7
Nov 18th, 2008
#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.
Reply With Quote Quick reply to this message  
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
  #8
Nov 18th, 2008
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

  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 << "Negative radius not allowed, enter a positive radius: ";
  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 << "Negative length not allowed, enter a positive length: ";
  49. cin >> length;}
  50. cout << "Please enter the width of the rectangle: ";
  51. cin >> width;
  52. while (width<0)
  53. {cout << "Negative width not allowed, enter a positive width: ";
  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 << "Negative base not allowed, enter a positive base: ";
  63. cin >> base;}
  64. cout << "Please enter the height of the triangle: ";
  65. cin >> height;
  66. while (height<0)
  67. {cout << "Negative height not allowed, enter a positive height: ";
  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. }
Last edited by davids2004; Nov 18th, 2008 at 12:44 am.
Reply With Quote Quick reply to this message  
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
  #9
Nov 18th, 2008
Originally Posted by VernonDozier View Post
#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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
1
  #10
Nov 18th, 2008
Originally Posted by davids2004 View Post
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:

  1. cout << "Negative number not allowed." << endl;
  2. 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC