C++ Homework help!!

Ive been trying to do my homework for the past 2 hours and I can't seem to get the correct answer, I keep getting 5/6 answers correct. Please help!!
(1) Prompt the user to input a wall's height and width. Calculate and output the wall's area. (2 pts)

Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the next line, but such values don't actually appear as output when the program runs.

Enter wall height (feet):
12
Enter wall width (feet):
15
Wall area: 180 square feet

(2) Extend to also calculate and output the amount of paint in gallons needed to paint the wall. Assume a gallon of paint covers 350 square feet. Store this value using a const double variable. (2 pts)

Enter wall height (feet):
12
Enter wall width (feet):
15
Wall area: 180 square feet
Paint needed: 0.514286 gallons

(3) Extend to also calculate and output the number of 1 gallon cans needed to paint the wall. Hint: Use a math function to round up to the nearest gallon. (2 pts)

Enter wall height (feet):
12
Enter wall width (feet):
15
Wall area: 180 square feet
Paint needed: 0.514286 gallons
Cans needed: 1 can(s)



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
#include <iostream>
#include <cmath>                   // Note: Needed for math functions in part (3)
using namespace std;

int main() {
   double wallHeight = 0.0;
   double wallWidth  = 0.0;
   double wallArea   = 0.0;
   
   cout << "Enter wall height (feet):";
   cin  >> wallHeight;
   cout << endl;
   cout << "Enter wall width (feet):";
   cin >> wallWidth;
   cout << endl;
   
   wallWidth = 15.0;               // FIXME (1): Prompt user to input wall's width
   
   // Calculate and output wall area
   wallArea = wallHeight * wallWidth;                // FIXME (1): Calculate the wall's area
   cout << "Wall area: " << wallArea << " square feet" << endl;  // FIXME (1): Finish the output statement
   
   
   cout << "Paint needed: " << (wallArea / 350) << " gallons" << endl;// FIXME (2): Calculate and output the amount of paint in gallons needed to paint the wall

   cout << "Cans needed: " <<round((wallArea/350))<< " can(s)" << endl; // FIXME (3): Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer

   return 0;

that is the question along with the code I inputed, I just need to fix #3
}
 
 cout << "Cans needed: " << round((wallArea / 350)) << " can(s)" << endl;
Last edited on
Did you read the documentation on round()? It rounds to the nearest integral value.
http://www.cplusplus.com/reference/cmath/round/
Which means if less than 1/2 a can, it will round down.

What you want is ceil().
http://www.cplusplus.com/reference/cmath/ceil/

Line 17: This is going to overlay whatever the user entered on line 14.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
thank you AbstractionAnon ,
I tried doing the ceil method and don't quite understand unfortunately.
What don't you understand? ceil() is a direct replacement for round() with the difference being it rounds up.
AbstractionAnon,

I used it and still got the same answer. it didn't change anything.

the expected output is:

Enter wall height (feet):
Enter wall width (feet):
Wall area: 1000 square feet
Paint needed: 2.85714 gallons
Cans needed: 3 can(s)

and I am receiving:

Enter wall height (feet):
Enter wall width (feet):
Wall area: 300 square feet
Paint needed: 0.857143 gallons
Cans needed: 1 can(s)
What is the purpose of this code?
cin >> wallWidth;
cout << endl;

wallWidth = 15.0; // FIXME (1): Prompt user to input wall's width

Affordable help with C++ programming.
http://homeworkhelp4u.com/cpp_programming_homework_help.html
Last edited on
Topic archived. No new replies allowed.