views:

242

answers:

6

I am working with a basic C++ program to determine the area and perimeter of a rectangle. My program works fine for whole numbers but falls apart when I use any number with a decimal. I get the impression that I am leaving something out, but since I'm a complete beginner, I have no idea what.

Below is the source:

#include <iostream>

using namespace std;

int main()
{
    // Declared variables
int length;  // declares variable for length
int width;  // declares variable for width
int area;  // declares variable for area
int perimeter; // declares variable for perimeter


    // Statements
cout << "Enter the length and the width of the rectangle: ";  // states what information to enter
cin >> length >> width;  // user input of length and width
cout << endl;  // closes the input
area = length * width;  // calculates area of rectangle
perimeter = 2 * (length + width);  //calculates perimeter of rectangle
cout << "The area of the rectangle = " << area << " square units." <<endl;  // displays the calculation of the area
cout << "The perimeter of the rectangle = " << perimeter << " units." << endl;  // displays the calculation of the perimeter
system ("pause"); // REMOVE BEFORE RELEASE - testing purposes only

return 0;
}
+2  A: 

Change all your int type variables to double or float. I would personally use double because they have more precision than float types.

thyrgle
You're not likely to notice the extra accuracy, though. For instance, `double(1.0)-double(0.9) != double(0.1)`, just like with floats.
MSalters
I believe you mean precision, not accuracy.
Cade Roux
A: 

use floats not ints an integer (int) is a whole number, floats allow decimal places (as do doubles)

float length;  // declares variable for length
float width;  // declares variable for width
float area;  // declares variable for area
float perimeter; // declares variable for perimete
Aran Mulholland
A: 

You've defined your variables as integers. Use double instead.

Also, you can look up some formatting for cout to define the number of decimal places you want to show, etc.

James
+2  A: 

int datatype stands for integer (i.e. positive and negative whole numbers, including 0)

If you want to represent decimal numbers, you will need to use float.

Cade Roux
or double for more precision. :)
CrazyJugglerDrummer
You could also conceivably use fixed point... For example if you change the "units", eg. `int Milliseconds;` is not a float but represents a fraction of a second. As always, the best approach will depend on the specifics of your problem.
asveikau
A: 

Thanks, everyone. I knew it would be something simple. Just not that simple.

H Bomb1013
Artelius
+2  A: 

Use the float or double type, like the others already said.

But it ain't as simple as that. You need to understand what floating-point numbers actually are, and why (0.1 + 0.1 + 0.1) != (0.3). This is a complicated subject, so I won't even try to explain it here - just remember that a float is not a decimal, even if the computer is showing it to you in the form of a decimal.

Mike Baranczak
Thanks, Mike. I know that I need to go back over that section and reread. Any online sources you can think of that might explain it in a way that makes sense?
H Bomb1013
@H Bomb1013: I always recommend this resource (http://docs.sun.com/source/806-3568/ncg_goldberg.html). It's a must-read if you're new to floating point arithmetic.
Cam
Thanks. I will bookmark that for a read. Especially since I have the feeling it will be a reoccurring theme.
H Bomb1013