943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1222
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2008
0

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.

Thanks.

[CODE]
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
0

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

Write a function that does not allow for negative input:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 18th, 2008
0

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

Write a function that does not allow for negative input:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by davids2004 ...
Where should this go in my code?

Thanks.

Where should the function go or where should the function call go?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 18th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
0

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

Where should the function go or where should the function call go?
Ok I got it. Here is my new code

[CODE]
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
0

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 18th, 2008
0

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
0

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.
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 18th, 2008
1

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

Click to Expand / Collapse  Quote originally posted by davids2004 ...
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:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008

This thread is solved

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.
Message:
Previous Thread in C++ Forum Timeline: GCC Exception handling w/ threads
Next Thread in C++ Forum Timeline: complier needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC