d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick C++ Question On Boundaries
Add Reply New Topic New Poll
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 15 2013 12:04pm
I wrote a code for calculating perimeter and area of a rectangle. If I wanted the input to only accept positive integer values, how/where would I put that in my code?



Code
#include <iostream>
using namespace std;

int main()
{
   int length = 0;
   int width = 0;
   int perimeter = length + length + width + width;
   int area = length * width;
   
   cout << "Enter length of rectangle: ";
   cin >> length;
   cout << "Enter width of rectangle: ";
   cin >> width;
   perimeter = length + length + width + width;
   
   cout << "The perimeter of the rectangle is: " << perimeter <<endl;
   
   area = length * width;
   
   cout << "The area of the rectangle is: " << area<< endl;
   
   system("pause");
   return 0;
}


This post was edited by King3r on Sep 15 2013 12:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 15 2013 12:16pm
1) accept strings instead of ints
2) immediately cast to int. if fails, bitch at user
3) immediately check > 0. if fails, bitch at user
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 15 2013 01:47pm
while(true){
cout << "Enter length of rectangle: ";
cin >> length;
cout << "Enter width of rectangle: ";
cin >> width;

if (!(length <0) || !(width < 0)){
break;
}
cout << "Invalid input";
}

perimeter = length + length + width + width;

cout << "The perimeter of the rectangle is: " << perimeter <<endl;

area = length * width;

cout << "The area of the rectangle is: " << area<< endl;

system("pause");
return 0;
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Sep 30 2013 06:00am
While loop for when users enters a number smaller than 0.

like
while (x<0 || y<0){
if(x<0) {
cout << "Invalid length, please enter a positive number: ";
cin >> x;
}
if(y<0){
cout << "Invalid width, please enter a positive number: ";
cin >> y;
}

You might also want to use doubles rather than integers since length doesn't have to be an integer.
Member
Posts: 1,535
Joined: Sep 12 2007
Gold: 1,736.52
Oct 1 2013 06:40am
You could either check the value using an if statement, or you can use the http://www.cplusplus.com/reference/cstdlib/abs/ function, third option would to use types that does not support negative values such as unsigned int or unsigned short.
And you are aware that your code makes perimeter = 0 + 0 + 0 + 0 and area 0 * 0 and then setting them, if they are supposed to be 0 there why dont you skip the arithmetic operations?

Code
int perimeter = length + length + width + width;
int area = length * width;

to
Code
int perimeter;
int area;
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Oct 2 2013 01:01am
Quote (carteblanche @ Sep 15 2013 01:16pm)
1) accept strings instead of ints
2) immediately cast to int. if fails, bitch at user
3) immediately check > 0. if fails, bitch at user


in every program you ever do that involves numbers, do this
Member
Posts: 1,087
Joined: May 2 2005
Gold: 75.00
Oct 2 2013 09:30pm
You can use unsigned integers
You can test the value in a loop
You can test the value in a conditional
You can throw an exception and make the user correct their input
Etc...
There are various paths to the solution of most problems.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll