Help with basic Carpet pricing program?

Hello. It's nice to be here.

I am trying to create a basic program that will calculate the cost of carpeting of a simple rectangular room. This assignment is for my C++ class at college. I got a basic code set up on my own without any syntax errors so far, but not all of this is meeting my teacher's requirements/instructions.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//This program will calculate the cost of carpeting in a rectangular room
//Written by Justin McCollum
//Created on 10-6-14

#include <iostream>
using namespace std;
int main ()
{
 int ppsf; //price per square foot
 int length; //length of the room
 int width; //Width of the room
 int area; //area of the room
 int price; //price of the carpet

  cout << "What is the room's length in feet? With decimal places if necessary:";
  cin >> length;
  cout << "What is the room's width in feet? With decimal places if necessary:";
  cin >> width;
  cout << "Enter the price of the carpet per square foot: ";
  cin >> ppsf;
 
  area = length * width;

cout << "Area of the Room =" << area << endl;

  price = ppsf * area;

  cout << "The total price of the room is: $" << price <<".";
  return (0);
}


Now, here is where I need help with...

* My professor wants the program to ask the carpet price by square yard first, then convert it into price by square yard. I just have the input for square yard only. That's not what he's looking for.

* Next, my professor wants us to use real numbers. How can I get the program to calculate everything using real numbers?

* Next, my professor wants us to have a symbolic constant named SFPERSY (for Square Feet per Square Yard), defined to represent the literal value 9.



I'm just having a little bit of trouble understanding symbolic constants and literal values in general.
I have a text book, my professor's own website and I've tried looking online, but I can't seem to grasp the concepts of symbolic constants and literal values.

* And finally, my professor wants the area and overall cost of the carpets to be rounded to two decimal places. We've only covered a little bit about this, and I don't have a full understanding of that either.

I have a feeling this may be a bit much, but these are just the four things I need help with. All help is greatly appreciated! Thank you for your time.

- Justin McCollum
Last edited on
Use float or double as your variable for real number
Use const data_type value for constant variable
For roundn to two decimal places there are function called setprecision(int n) inside iomanip header
Last edited on
Thanks for your reply. Okay, so I put the double in the body for real numbers. Here is my new code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//This program will calculate the cost of carpeting in a rectangular room
//Written by Justin McCollum
//Created on 10-6-14

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
 double ppsf, length, width, area, price;

  cout << "What is the room's length in feet? With decimal places if necessary:";
  cin >> length;
  cout << "What is the room's width in feet? With decimal places if necessary:";
  cin >> width;
  cout << "Enter the price of the carpet per square foot: ";
  cin >> ppsf;
 
  area = length * width;

cout << "Area of the Room =" << area << endl;

  price = ppsf * area;
  std::cout << setprecision (2) << ppsf << endl;
  std::cout << setprecision (2) << length << endl;
  std::cout << setprecision (2) << width << endl;
  std::cout << setprecision (2) << area << endl;
  std::cout << setprecision (2) << price << endl;
  cout << "The total price of the room is: $" << price <<".";
  return (0);
}


But here is what the program looks like...

http://i57.tinypic.com/2vajue1.png

It's adding e+003 at the end of the price. That doesn't look right. What am I doing wrong?
Add the fixed i/o manipulator.

1
2
3
4
5
6
7
8
9
10
  std::cout << setprecision (2) << fixed << ppsf << endl;
  std::cout << setprecision (2) << fixed << length << endl;
  std::cout << setprecision (2) << fixed << width << endl;
  std::cout << setprecision (2) << fixed << area << endl;
  std::cout << setprecision (2) << fixed << price << endl;
What is the room's length in feet? With decimal places if necessary:10
What is the room's width in feet? With decimal places if necessary:12
Enter the price of the carpet per square foot: 3
Area of the Room =120
3.00
10.00
12.00
120.00
360.00
The total price of the room is: $360.00.

I did add the fixed i/o manipulator, but it's still giving me e+0.002 at the end! It's not as clean as your results. :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//This program will calculate the cost of carpeting in a rectangular room
//Written by Justin McCollum
//Created on 10-6-14

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
 double ppsf, length, width, area, price;

  cout << "What is the room's length in feet? With decimal places if necessary:";
  cin >> length;
  cout << "What is the room's width in feet? With decimal places if necessary:";
  cin >> width;
  cout << "Enter the price of the carpet per square foot: ";
  cin >> ppsf;
 
  area = length * width;

cout << "Area of the Room =" << area << endl;

  price = ppsf * area;
  std::cout << setprecision (2) << fixed << ppsf << endl;
  std::cout << setprecision (2) << fixed << length << endl;
  std::cout << setprecision (2) << fixed << width << endl;
  std::cout << setprecision (2) << fixed << area << endl;
  std::cout << setprecision (2) << fixed << price << endl;
  std::cout << "The total price of the room is: $" << fixed << price <<".";
  return (0);
}


I still don't know where it's going wrong.
I still have three of things I need help with in my program, PLEASE! This is due in 2 days!

1. I want to know how to get rid of the "e" in the calculations from the last code
2. I need to program input for price per square yard first, then convert it to square feet
3. And I need to add a symbolic constant called "SFPERSY (for Square Feet per Square Yard) with a literal value of 9.

Help is greatly appreciated. Please help me, I have zero experience with C++ programming, obviously.
Last edited on
1. I want to know how to get rid of the "e" in the calculations from the last code
I can't reproduce this. Can you show what inputs you're typing when your program asks for them?
2. I need to program input for price per square yard first, then convert it to square feet
You're already getting input from the user. Just change your prompt so that it asks for square yards. To convert it to feet, multiply divide the input from the user by SFPERSY.
3. And I need to add a symbolic constant called "SFPERSY (for Square Feet per Square Yard) with a literal value of 9.
Literally all this means is define a variable that you can't change. Do this before you do the conversion of sq. yards to sq. feet so that you can use the constant in the expression instead of literally typing 9.
const int SFPERSY = 9;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//This program will calculate the cost of carpeting in a rectangular room
//Written by Justin McCollum
//Created on 10-6-14

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{	double ppsf, length, width, price;
	const int SFPSY = 9;
	double ppsy;
	int	yl, yw, sqyds;

	cout << "What is the room's length in feet? With decimal places if necessary:";
	cin >> length;
	cout << "What is the room's width in feet? With decimal places if necessary:";
	cin >> width;
	cout << "Enter the price of the carpet per square foot: ";
	cin >> ppsf;
	ppsy = ppsf * SFPSY;	//	Convert price to square yards
	cout << "The price per square yard is: " << setprecision(2) << fixed << ppsy << endl;
	yl = (int)ceil(length / 3);  //	round up to next yard
	yw = (int)ceil(width / 3);	//	round up to next yard
	cout << "The size of your room in yards is: " << yl << " x " << yw << endl; 
	sqyds = yl * yw;			//	area in sq yards
	cout << "Area of the Room (sq yds) =" << sqyds << endl;
	price = ppsy * sqyds;
	cout << "The total price of the room is: $" << fixed << price <<"." << endl;
	system ("pause");
	return 0;
} 
What is the room's length in feet? With decimal places if necessary:10
What is the room's width in feet? With decimal places if necessary:12
Enter the price of the carpet per square foot: 2.99
The price per square yard is: 26.91
The size of your room in yards is: 4 x 4
Area of the Room (sq yds) =16
The total price of the room is: $430.56.
Press any key to continue . . .


Note: I've made the assumption that carpet is sold in full square yards only. If that's not a requirement, you can remove the ceil() functions.
@AbstractionAnon

The code looks almost EXACTLY what I am looking for, except there is one problem! I need the program to ask the input of Square Yards first, then convert it into square feet!

I'm guessing all I have to do is change the input of ppsf to ppsy, and modify the equation "ppsy = ppsf * SFPSY;"

But how?

ppsf = ppsy/SFPSY; ?

And we also haven't talked about (int) ceil (length / 3), yet. So that could be a problem with my professor, I might just have to remove it. But it's good to know that you can do that!

I just need one more step to go. Just how can I reverse it to converting to Square Yards first, then convert it into square feet?
Last edited on
Just one more step, this is due tomorrow evening!

What do I do to make the program ask for the input of Square Yards first, then convert it into square feet, based off of the last code above?

The code above asks for the inverse. The code above wants Square feet first, then converts it into square yards. I need it the other way around.
Last edited on
In the beginning of your program you are asking the user to input the number of square feet as input. I would change your:
cout << "What is the room's length in feet? With decimal places if necessary:";

to say
cout << "What is the room's length in square yards? \nWith decimal places if necessary:";

The /n drops the following down a line.
Once you have received the input from user as Square yards, to convert to Square feet you would multiply that input by 9. Which I'm guessing is what the constant the instructor wanted you to create is for. So you could do....

length=length*SFPSY;

I only did a quick look over, so if SFPSY isn't for that then....
length=length*9;
Last edited on
Topic archived. No new replies allowed.