Help me with something

I was asked:
Write a program that calculates the cost of painting a room. Ask the user for the number of square feet of surface to be painted. Given that a one gallon can of paint covers 300 square feet and costs $35, calculate the number of gallons required and the total cost of the paint needed. Print the results to the screen ini a well-formated display. (Careful -- can only buy whole gallons of paint.)


And I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <math.h> 
using namespace std;

int main()
{
	//First, the variables
	int can_area = 300; //300 square feet per can
	int can_cost = 35; //The cost of 1 can
	float paint_area;
	int cans = ceil(paint_area/can_area); //This is the number of cans needed.
    int cost = can_cost*cans; //The cost is the cost of each, multiplied by all cans.

    //Now we'll ask the user for the total area:
    cout << "What is the area in square feet? :";
    cin >> paint_area; //The user answers with a float or integer number.
    cout << "\n";
    cout << "\n";
	cout << "The number of gallons required is: " << cans;
	cout << "\nThe total cost is              : " << cost;
	return 0;
}


But it gives me a negative number always! What did I do wrong?
you initialized paint area after you did the math. It executes from top to bottom, so you should do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <math.h> 
using namespace std;

int main()
{
    //Now we'll ask the user for the total area:
    cout << "What is the area in square feet? :";
    cin >> paint_area; //The user answers with a float or integer number.
	//First, the variables
	int can_area = 300; //300 square feet per can
	int can_cost = 35; //The cost of 1 can
	float paint_area;
	int cans = ceil(paint_area/can_area); //This is the number of cans needed.
        int cost = can_cost*cans; //The cost is the cost of each, multiplied by all cans.


    cout << "\n";
    cout << "\n";
	cout << "The number of gallons required is: " << cans;
	cout << "\nThe total cost is              : " << cost;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.