Drywall program using functions

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
[code]I am trying to write a program using functions. I am having trouble with two of the functions. one in question is calculate_drywall. Which is passed two values in feet for length and width and returns the various size drywall sheets for the room. The sheets come in 4x 8, 10, 12, 14, 16. I am using a void with reference parameters. The second function I am having trouble with is calculate material which is passed the total number of sheets for the building and returns the amount of screws and mud required. Where I am confused is the drywall function returns the sheets for each room then in materials it is taking in the building total. Should I do something in main? Any help would be appreciated. 

Here is what I have so far

[code]#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<math.h>
using namespace std;

void get_data(double& val1, double& val2, bool& error, ifstream&);
double convert_to_feet(double val);
void calculate_drywall(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& perimeter, double& in_feet1, double& in_feet2);
void calculate_material(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& mud, double& screws, double& sqr_ft);
void print_output(double room_num, double in_feet1, double in_feet2, bool error, ofstream&, double perimeter, double tot_16ft, double sqr_ft);

int main()
{
	double val1, val2, room_num = 0, in_feet1, in_feet2, perimeter = 0;
	double	tot_8ft, tot_10ft, tot_12ft, tot_14ft, tot_16ft = 0;
	double  mud, screws, sqr_ft = 0;
	bool error;
	string proj_title;
	ifstream infile;
	ofstream outfile;
	infile.open("proj_info.txt");
	outfile.open("supplies.txt");

	getline(infile, proj_title);

	get_data(val1, val2, error, infile);
	while (infile)
	{
		room_num++;

	in_feet1 = convert_to_feet(val1);
	in_feet2 = convert_to_feet(val2);

	calculate_drywall(tot_8ft, tot_10ft, tot_12ft, tot_14ft, tot_16ft, in_feet1, in_feet2, perimeter);
	
		get_data(val1, val2, error, infile);
	}
	
	calculate_material(tot_16ft, tot_14ft, tot_12ft, tot_10ft, tot_8ft, mud, screws, sqr_ft);
}

void get_data(double& val1, double& val2, bool& error, ifstream& in)
{
	in >> val1 >> val2;
	if (val1 <= 0 || val2 <= 0)
		error = 0;
	else
		error = 1;
}

double convert_to_feet(double val)
{
	double in_feet;
	in_feet = val * 3.28;
	return in_feet;
}

void calculate_drywall(double& tot_8ft, double& tot_10ft, 
	                   double& tot_12ft, double& tot_14ft, 
	                   double& tot_16ft, double& in_feet1, 
	                   double& in_feet2, double& perimeter)
{

	perimeter = 2 * (in_feet1 + in_feet2);
	
	if (perimeter >= 16)
		tot_16ft = (perimeter / 16) * 2;

	else
		if (perimeter <= 16 && perimeter >= 14)
			tot_14ft = (perimeter / 14) * 2;

	else
		if (perimeter <= 14 && perimeter >= 12)
			tot_12ft = (perimeter / 12) * 2;
	else
		if (perimeter <= 12 && perimeter >= 10)
			tot_10ft = (perimeter / 10) * 2;
	else
			tot_8ft = (perimeter / 8) * 2;

}


void calculate_material(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& mud, double& screws, double& sqr_ft)
{
	sqr_ft = (tot_16ft * (16 * 4)) + (tot_14ft * (14 * 4)) + (tot_12ft * (12 * 4)) + (tot_10ft * (10 * 4)) + (tot_8ft * (8 * 4));

}

void print_output(double room_num, double in_feet1, double in_feet2, bool error, ofstream& out, double perimeter, double tot_16ft, double sqr_ft)
{
	out << ceil (tot_16ft) <<  sqr_ft << endl;[code]
Last edited on
Also I get garbage from my sqr_ft calculation
You probably don;t want an if/else tree here.
1
2
3
4
5
6
7
8
9
10
    if (perimeter >= 16)
        tot_16ft = (perimeter / 16) * 2;
    else if (perimeter <= 16 && perimeter >= 14)
        tot_14ft = (perimeter / 14) * 2;
    else if (perimeter <= 14 && perimeter >= 12)
        tot_12ft = (perimeter / 12) * 2;
    else if (perimeter <= 12 && perimeter >= 10)
        tot_10ft = (perimeter / 10) * 2;
    else 
        tot_8ft = (perimeter / 8) * 2;


Consider a room that has a perimeter of 40 feet (ignoring the height). You want 2 16 foot sheets and 1 8 foot sheet.

You also probably want the number of sheets of each size to be an int and not a double.

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.
Ok that makes sense using an int, instead. What would you suggest instead of an if/else tree. A switch?
or a for loop?
A for loop implies you have something to iterate over a known number of times.
Here you have separate variables for each of the various lengths.

Line 15,41: You have an argument mismatch:
15
16
// Prototype & function:
void calculate_drywall(double& tot_8ft, double& tot_10ft, double& tot_12ft, double& tot_14ft, double& tot_16ft, double& perimeter, double& in_feet1, double& in_feet2);

41
42
// Actual call
calculate_drywall(tot_8ft, tot_10ft, tot_12ft, tot_14ft, tot_16ft, in_feet1, in_feet2, perimeter);

Note that the order of in_feet1, in_feet2 and perimeter do not match.

I would do something like the following:
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//  Calculate how many sheets of specified length are required
//  Note that perimeter is decremented
int calculate_sheets (double & perimeter, int length)
{   int cnt = 0;

    if (perimeter >= 16)
	{  cnt = (int)((perimeter / 16) * 2); 
	   perimeter -= (length*cnt);  // decrement the remaining perimeter
	}
    return cnt;
}
	
void calculate_drywall(double perimeter, int& tot_8ft, int& tot_10ft, 
	                   int& tot_12ft, int& tot_14ft, 
	                   int& tot_16ft)
{   tot_16ft = calculate_sheets (perimeter, 16);
	tot_14ft = calculate_sheets (perimeter, 14);
	tot_12ft = calculate_sheets (perimeter, 12);
    tot_10ft = calculate_sheets (perimeter, 10);
    tot_8ft = calculate_sheets (perimeter, 8);
    if (perimeter > 0)
        tot_8ft++;      //  May need an odd sheet for remainder
}

I changed the arguments to calculate_drywall to pass perimeter by value since we trash the value of it.

Thanks for editing you post and adding the code tags. You do have some extra
[code]
tags though. But at least you have line numbers I can refer to.


Last edited on
Awesome thanks for the help! In the function calculate sheets that you added, would you need to put another if for each size where it is perimeter/16. or does the number in the call to calculate sheets each time replace this 16?
This is the almost final product any suggestions or problems that anyone can spot that I can't. Looking at the screen so long everything starts to blend together. My only problem is that the output won't line up correctly and I can't seem to find a way to fix it. The output will be bellow the 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
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<math.h>
using namespace std;

void get_data(double& val1, double& val2, bool& error, ifstream&);

double convert_to_feet(double val);

int calculate_sheets(double& perimeter, int length);

void calculate_drywall(int& num_8ft, int& num_10ft, 
	                   int& num_12ft, int& num_14ft, 
	                   int& num_16ft, double& perimeter, 
	                   double& in_feet_width, double& in_feet_length,
					   int& tot8, int& tot10, int& tot12,
	                   int& tot14, int& tot16);

void calculate_material(int& tot8, int& tot10, int& tot12, 
	                    int& tot14, int& tot16, int& cans_mud, 
	                    int& lbs_screws, int& num_5lb, int& num_25lb, 
	                    double& sqr_ft, int& tot_sheets,
	                    double& totcost);

void print_output(double room_num, double in_feet_width, 
	              double in_feet_length, bool error, 
	              ofstream&, double perimeter, 
	              double sqr_ft, int num_16ft, int num_14ft, 
	              int num_12ft, int num_10ft, int num_8ft, 
	              int tot16, double val1, double val2);

int main()
{
	double val1, val2, room_num = 0, in_feet_width, 
		   in_feet_length, perimeter = 0, sqr_ft = 0, totcost;
	int	num_8ft, num_10ft, num_12ft, num_14ft, num_16ft, tot8 = 0, 
		tot10 = 0, tot12 = 0, tot14 = 0, tot16 = 0, cans_mud, 
		lbs_screws, num_5lb = 0, num_25lb = 0, tot_sheets;
	bool error;
	string proj_title;
	ifstream infile;
	ofstream outfile;
	infile.open("proj_info.txt");
	outfile.open("supplies.txt");

	getline(infile, proj_title);
	outfile << "Project: " << proj_title << endl
		    << setw(70) << right << "8' " << "10' "
		    << "12' " << "14' " << "16' " << endl;

	get_data(val1, val2, error, infile);
	while (infile)
	{
		room_num++;

	in_feet_width = convert_to_feet(val1);
	in_feet_length = convert_to_feet(val2);

	calculate_drywall(num_8ft, num_10ft, num_12ft, num_14ft, 
		              num_16ft, perimeter, in_feet_width, 
		              in_feet_length, tot8, tot10, tot12, 
		              tot14, tot16);
	tot8  += num_8ft;
	tot10 += num_10ft;
	tot12 += num_12ft;
	tot14 += num_14ft;
	tot16 += num_16ft;

	calculate_material(tot8, tot10, tot12, tot14, tot16, 
		               cans_mud, lbs_screws, num_5lb, 
		               num_25lb, sqr_ft, tot_sheets, totcost);
	

    print_output(room_num, in_feet_width, in_feet_length, 
		         error, outfile, perimeter, sqr_ft,
		         num_16ft, num_14ft, num_12ft,
	             num_10ft, num_8ft, tot16, val1, val2);
		
   get_data(val1, val2, error, infile);
	}
	
	outfile << "House Totals: " << setw(54) << right
		<< tot8 << "   " << tot10 << "   " << tot12
		<< "   " << tot14 << "   " << tot16 << endl
		<< "Total Materials" << endl << endl
		<< "Drywall Sheets       " << tot_sheets << endl
		<< "Drywall Square Feet  " << sqr_ft << endl
		<< "Drywall Screws       " << num_25lb
		<< " 25 lb Boxes" << endl << "                     " 
		<<num_5lb
		<< " 5 lb Boxes" << endl
		<< "Drywall Mud          " << cans_mud
		<< " Cans Mud" << endl << endl
		<< "Total Cost:          $" << totcost << endl;

}

void get_data(double& val1, double& val2, bool& error, ifstream& in)
{
	in >> val1 >> val2;
	if (val1 <= 0 || val2 <= 0)
		error = 0;
	else
		error = 1;
}

double convert_to_feet(double val)
{
	double in_feet;
	in_feet = val * 3.28;
	return in_feet;
}

int calculate_sheets(double& perimeter, int length)
{
	int cnt = 0;

	if (perimeter >= 16)
	{
	    cnt = (int)((perimeter / 16) * 2);
		perimeter -= (length * cnt);
	}
  return cnt;
}

void calculate_drywall(int& num_8ft, int& num_10ft, 
	                   int& num_12ft, int& num_14ft, 
	                   int& num_16ft, double& perimeter, 
	                   double& in_feet_width, 
	                   double& in_feet_length,
	                   int& tot8, int& tot10, int& tot12, 
	                   int& tot14, int& tot16)
{
   perimeter = 2 * (in_feet_width + in_feet_length);
   
   num_16ft = calculate_sheets(perimeter, 16);
   num_14ft = calculate_sheets(perimeter, 14);
   num_12ft = calculate_sheets(perimeter, 12);
   num_10ft = calculate_sheets(perimeter, 10);
   num_8ft  = calculate_sheets(perimeter, 8);
   if (perimeter > 0)
	   num_8ft++;
	

}

void calculate_material(int& tot8, int& tot10, int& tot12, 
	                    int& tot14, int& tot16, int& cans_mud,
	                    int& lbs_screws, int& num_5lb, int& num_25lb,
	                    double& sqr_ft, int& tot_sheets,
	                    double& totcost)
{
	sqr_ft = (tot8 * (8 * 4)) + (tot10 * (10 * 4)) + 
		     (tot12 * (12 * 4)) + (tot14 * (14 * 4)) + 
		     (tot16 * (16 * 4));

	cans_mud = ceil(sqr_ft / 350);

	lbs_screws = ceil(sqr_ft / 185);

	if (lbs_screws >= 25)
	{
		num_25lb = (lbs_screws / 25);
		lbs_screws -= num_25lb;
	}
	else
		num_5lb = (lbs_screws / 5);

	tot_sheets = tot16 + tot14 + tot12 + tot10 + tot8;

	totcost = (num_25lb * 50.00) + (num_5lb * 14.00) + (cans_mud * 13.50);
}

void print_output(double room_num, double in_feet_width, 
	              double in_feet_length, bool error, ofstream& out,
	              double perimeter, double sqr_ft, int num_16ft, 
	              int num_14ft, int num_12ft,
	              int num_10ft, int num_8ft, int tot16, double val1, 
	              double val2)
{
	out << "Room: " << setw(2) << right << room_num
		<< setw(15) << right << in_feet_width
		<< " ft by " << in_feet_length << " ft"
		<< setw(30) << right << num_8ft << "   "
		<< num_10ft << "   " << num_12ft << "   "
		<< num_14ft << "   " << num_16ft << endl;
}


Here is the output

Project: Big House at the Corner - Project 2015-23
8' 10' 12' 14' 16'
Room: 1 11.152 ft by 9.184 ft. 0 0 0 0 5
Room: 2 33.456 ft by 44.936 ft 0 0 0 0 19
Room: 3 50.512 ft by 68.552 ft 0 0 0 0 29
Room: 4 4.592 ft by 1.64 ft 1 0 0 0 0
Room: 5 19.024 ft by 11.152 ft 0 0 0 0 7
Room: 6 20.336 ft by 19.024 ft 0 0 0 0 9
Room: 7 7.872 ft by 8.856 ft 0 0 0 0 4
Room: 8 9.84 ft by 10.824 ft 0 0 0 0 5
Room: 9 11.808 ft by 12.792 ft 0 0 0 0 6
Room: 10 13.776 ft by 14.76 ft 0 0 0 0 7
Room: 11 15.744 ft by 15.744 ft 0 0 0 0 7
Room: 12 3.936 ft by 3.936 ft 1 0 0 0 0
Room: 13 4.92 ft by 4.92 ft 0 0 0 0 2
Room: 14 5.904 ft by 5.904 ft 0 0 0 0 2
Room: 15 6.888 ft by 6.888 ft 0 0 0 0 3
Room: 16 12.792 ft by 11.808 ft 0 0 0 0 6
House Totals: 2 0 0 0 111
Total Materials

Drywall Sheets 113
Drywall Square Feet 7168
Drywall Screws 1 25 lb Boxes
4 5 lb Boxes
Drywall Mud 21 Cans Mud

Total Cost: $389.5


A few comments:

Line 14: A function prototype for calculate_sheets is not needed since the implementation of calculate_sheets() occurs before it is called. I'm not a fan of using function prototypes for every function, but that is a matter of style.

Line 130: I noticed you changed calculate_drywall() to pass perimeter by reference. I had very specifically made it a pass by value (and moved the calculation of perimeter to line 64). As I noted in my comments, the value of perimeter is decremented as we call calculate_sheets(). Therefore, the value of perimeter is 0 when we exit calculate_materials(). By passing it by value, we don't affect the value of it in main(). Also by moving the calculation of perimeter, we don't need to pass in_feet_width and in_feet_length since they are no longer used.

line 166-167: You're not calculating the number of 5 pound boxes required. Consider if lbs_screws is 45. You want to calculate the # of 25 pound boxes, then the # of 5 pound boxes from any remainder.

Line 170: You're doing integer division. You're going to be a box short unless the lbs_screws is exactly divisible by 5.

I don't believe your calculations take into account height of the room. Since you're decrementing perimeter by the length of each sheet, you're assuming a room height of the width of the sheet (4ft). Shouldn't you be either asking for the height of the room, or assuming a standard height of 8 feet (2 sheets high)?

Is the output you posted actually from your program? Or what it is supposed to look like? If it is from your program, I don't see how you arrived at 1 25 lb box and 4 5 lb boxes. Your code doesn't appear to support that.

Last edited on
Topic archived. No new replies allowed.