++ -- expressions

Apr 3, 2015 at 4:54pm
so I'm trying to write a code that calculates the volume of a rectangular prism and I'm asking the user to input length, width and height.

after this i'm suppose to increase the length and width of whatever the user entered by 2...
how do i go about doing this? I'm still confused on using ++ and -- in mathematical expressions.
This is what i have so far:

(the second part doesn't work...)

double recPrismVolume; //variable for rectangle prism volume
double recPrismVolumeNew; // variable for second prism volume


cout << "Please enter the height of your rectangular prism: \n ";
cin >> height;

cout << "Enter length: \n";
cin >> length;

cout << "Enter width: \n";
cin >> width ;

recPrismVolume = height * length * width; // formula for rectangular
prism volume

cout << "This rectangular prism has a volume of " << recPrismVolume <<
endl;

recPrismVolumeNew = length++ * width++ * height; //we will add two to whatever the user
//has entered for length and width

cout << "The new rectangular prism volume is " << recPrismVolume <<
endl;

Apr 3, 2015 at 4:59pm
If you want to increase something by 2 or whatever, you do this

1
2
3
4
5
int x = 5;

x = x + 2;

cout << x << endl;


If you want to increase it by one, you could do the same

x = x + 1; or take the shortcut x++;
Last edited on Apr 3, 2015 at 5:00pm
Apr 3, 2015 at 5:04pm
but what if i want to increase a number that the user entered.?
Apr 3, 2015 at 5:05pm
I just showed you how. Its the same...

1
2
3
4
cout << "Enter width: \n";
cin >> width ;

width = width + 2; // Now its increased by 2. 
Last edited on Apr 3, 2015 at 5:05pm
Topic archived. No new replies allowed.