ceil not working?

I cannot get this program to round the amount of gallons up to the nearest integer for some reason while using the ceil command inside the function FindGallons, any idea why?
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//*********************************************************************
//File name PaintRoom
// 

//Description: This program is given the length and width of a region to
//be painted, and the price of a gallon of paint as input. The square
//footage of the region is calculated, and the number of gallons of paint
//needed to cover the region, and the cost is determined. One gallon of
//paint will cover 200 square feet of the region.
//*********************************************************************

#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std ;


// function prototypes
int GetLength();
int GetWidth();
double GetGallonCost();
int ComputeArea( int, int );
int FindGallons( int );
double ComputeCost( double, int );
void PrintResults( int, int, double );

// global constants
const int COVERAGE = 200;       // 1 gallon of paint will cover 200 sq. ft.


int main()
  {
    // local variable declarations for main
    int   length;      // length of the region to be covered
    int   width;       // width of the region to be covered
    int   area;        // area of the region to be covered; long int
                       // would be required to store large areas
    int    gallons;     // number of gallons needed to cover the region
    double gallon_cost; // price of a gallon of paint
    double paint_cost;  // the cost of the paint needed

    // input data
    length      = GetLength();
    width       = GetWidth();    
    gallon_cost = GetGallonCost();
        
    // process data
    area       = ComputeArea( length, width );
    gallons    = FindGallons( area );   // find number of gallons to
    paint_cost = ComputeCost( gallon_cost, gallons );  // compute cost of paint
    // print results
    PrintResults( gallons, area, paint_cost );

    system ("pause");
    return(0);
  }



//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int GetLength()
  {
  int length;
  
  cout << "Please Input The Length Of The Room: ";
  cin >> length;
  cout << endl << "The Length You Input Was: " << length << endl;
  return length;
  }


//*********************************************************************
//Function Name: GetWidth
// Prompts the user to input the width of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int GetWidth()
  {
  int width;
  
  cout << "Please Input The Width Of The Room: ";
  cin >> width;
  cout << endl << "The Width You Input Was: " << width << endl;
  return width;
  }


//**********************************************************************
//Function Name: GetGallonCost
//Prmpts the user to input the cost of a gallon of paint
//Returns the value input by the user

//**********************************************************************
double GetGallonCost()
  {
  int GallonCost;
  
  cout << "Please Input The Cost Of Paint Per Gallon: ";
  cin >> GallonCost;
  cout << endl << "The Cost Per Gallon You Entered Was: " << GallonCost << endl;
  return GallonCost;
  }


//**********************************************************************
//Function Name: ComputeArea
// Formal parameters are the length and width of a rectangle
// Calculates and returns the rectangle area

//**********************************************************************
int ComputeArea( int length, int width )
{
   int area;
   
   area = length * width;
   cout << "The Area Of The Room Is: " << area << endl;
   return area;
}


//**********************************************************************
//Function Name: FindGallons

//Calculates and returns the number of gallons of paint needed 
// Formal parameter is the area to be painted
// Global constant COVERAGE tells the number of square feet
// covered by one gallon
// Calculates and returns the number of gallons
// Paint must be purchased by the gallon so if (Area / COVERAGE)
// yields a remainder, round up to the highest integer

//**********************************************************************
int FindGallons( int area )
{
 int gallons;
 
 gallons = area / COVERAGE;
 
 cout << "The Number Of Gallons Needed Are: " << ceil(gallons) << endl << endl;
 
 return gallons;
}


//**********************************************************************
//Function Name: ComputeCost
//Computes how much it would cost to buy all the paint necessary
//to cover the region.
// Formal parameters are the cost per gallon and the number of gallons 
// of paint needed


//**********************************************************************
double ComputeCost( double gallon_price, int num_gallons )
{
 return 1.0;
}


//**********************************************************************
//Function Name: PrintResults
// A void function
// Formal parameters are the number of gallons, the area to be painted,
// and the total cost of the paint required.
// Prints a message to the screen stating the number of gallons,
// the area, and the post of the piant.

//**********************************************************************
void PrintResults( int gallons, int area, double paint_cost )
  {

  }


// end of function declarations 
1
2
int gallons;
gallons = area / COVERAGE;

That's integer division you're doing there. Both operands are integers.
should both be doubles? or only area be a double.

I'm basically having to fix this code to make it allow user input of the length and width of a room and the cost of a gallon of paint, and it tells you how many gallons you need and how much that'll cost you. I hate having pre-written code that you have to fix because it takes me twice as long to fix it than it would to just write it myself.
One of the operands and the variable in which you'll store the value (gallons) must be doubles.
Last edited on
now it's telling me that in converting from an int to a double.
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//*********************************************************************
//File name PaintRoom
// 

//Description: This program is given the length and width of a region to
//be painted, and the price of a gallon of paint as input. The square
//footage of the region is calculated, and the number of gallons of paint
//needed to cover the region, and the cost is determined. One gallon of
//paint will cover 200 square feet of the region.
//*********************************************************************

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


// function prototypes
int GetLength();
int GetWidth();
double GetGallonCost();
int ComputeArea( int, int );
int FindGallons( int );
double ComputeCost( double, int );
void PrintResults( int, int, double );

// global constants
const double COVERAGE = 200;       // 1 gallon of paint will cover 200 sq. ft.


int main()
  {
    // local variable declarations for main
    int   length;      // length of the region to be covered
    int   width;       // width of the region to be covered
    int   area;        // area of the region to be covered; long int
                       // would be required to store large areas
    int    gallons;     // number of gallons needed to cover the region
    double gallon_cost; // price of a gallon of paint
    double paint_cost;  // the cost of the paint needed

    // input data
    length      = GetLength();
    width       = GetWidth();
    gallon_cost = GetGallonCost();
      
    // process data
    area       = ComputeArea( length, width );
    gallons    = FindGallons( area );   // find number of gallons to
    paint_cost = ComputeCost( gallon_cost, gallons );  // compute cost of paint
    // print results
    PrintResults( gallons, area, paint_cost );

    system ("pause");
    return(0);
  }



//**********************************************************************
//Function Name: GetLength
// Prompts the user to input the lenth of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int GetLength()
  {
  int length;
  
  cout << "Please Input The Length Of The Room: ";
  cin >> length;
  cout << endl << "The Length You Input Was: " << length << endl;
  return length;
  }


//*********************************************************************
//Function Name: GetWidth
// Prompts the user to input the width of a rectangular room
// Returns the int value input by the user
//**********************************************************************
int GetWidth()
  {
  int width;
  
  cout << "Please Input The Width Of The Room: ";
  cin >> width;
  cout << endl << "The Width You Input Was: " << width << endl;
  return width;
  }


//**********************************************************************
//Function Name: GetGallonCost
//Prmpts the user to input the cost of a gallon of paint
//Returns the value input by the user

//**********************************************************************
double GetGallonCost()
  {
  double GallonCost;
  double gallon_price;
  
  cout << "Please Input The Cost Of Paint Per Gallon: ";
  cin >> GallonCost;
  cout << endl << "The Cost Per Gallon You Entered Was: " << GallonCost << endl;
  
  gallon_price = GallonCost;
  
  return gallon_price;
  }


//**********************************************************************
//Function Name: ComputeArea
// Formal parameters are the length and width of a rectangle
// Calculates and returns the rectangle area

//**********************************************************************
int ComputeArea( int length, int width )
{
   int area;
   
   area = length * width;
   cout << "The Area Of The Room Is: " << area << endl;
   return area;
}


//**********************************************************************
//Function Name: FindGallons

//Calculates and returns the number of gallons of paint needed 
// Formal parameter is the area to be painted
// Global constant COVERAGE tells the number of square feet
// covered by one gallon
// Calculates and returns the number of gallons
// Paint must be purchased by the gallon so if (Area / COVERAGE)
// yields a remainder, round up to the highest integer

//**********************************************************************
int FindGallons( int area )
{
 double gallons;
 double num_gallons;
 
 gallons = area / COVERAGE;
 
 cout << "The Number Of Gallons Needed Are: " << ceil(gallons) << endl << endl;
 
 num_gallons = gallons;
 
 return num_gallons;
}


//**********************************************************************
//Function Name: ComputeCost
//Computes how much it would cost to buy all the paint necessary
//to cover the region.
// Formal parameters are the cost per gallon and the number of gallons 
// of paint needed


//**********************************************************************
double ComputeCost( double gallon_price, int num_gallons )
{
 double paint_cost;
 
 paint_cost = gallon_price * num_gallons;
 
 cout << paint_cost;
       
 return paint_cost; 
}


//**********************************************************************
//Function Name: PrintResults
// A void function
// Formal parameters are the number of gallons, the area to be painted,
// and the total cost of the paint required.
// Prints a message to the screen stating the number of gallons,
// the area, and the post of the piant.

//**********************************************************************
void PrintResults( int gallons, int area, double paint_cost )
  {

  }


// end of function declarations 
Topic archived. No new replies allowed.