Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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;
}
Posted
Updated 20-Oct-15 15:22pm
v3
Comments
ZurdoDev 20-Oct-15 16:29pm    
What exactly is the problem?
Member 12074236 20-Oct-15 16:46pm    
The calculate drywall function reports the sheets for each room, but the calculate materials function has to receive the building totals of each sheet size. I'm stuck here
Albert Holguin 20-Oct-15 17:23pm    
Your problem statement is not clear at all...
Member 12074236 20-Oct-15 17:27pm    
function calculate drywall reports the totals of the various sheets of drywall for each individual room, but the calculate material function needs the totals for the whole building to calculate the materials based on square footage. I can't figure out how to get the totals to the materials function

1 solution

If you have more than one room, you need to take care of this in the main function.
You need to add the sums of the drywall sheets for each room in the while loop.

C++
double tmp_8ft, tmp_10ft, tmp_12ft, tmp_14ft, tmp_16ft;
while (infile)
{
    room_num++;
    
    in_feet1 = convert_to_feet(val1);
    in_feet2 = convert_to_feet(val2);

    tmp_8ft = 0.0;
    tmp_10ft = 0.0;
    tmp_12ft = 0.0;
    tmp_14ft = 0.0;
    tmp_16ft = 0.0;
    calculate_drywall(tmp_8ft, tmp_10ft, tmp_12ft, tmp_14ft, tmp_16ft, in_feet1, in_feet2, perimeter);
    tot_8ft  += tmp_8ft;
    tot_10ft += tmp_10ft;
    tot_12ft += tmp_12ft;
    tot_14ft += tmp_14ft;
    tot_16ft += tmp_16ft;
    
    get_data(val1, val2, error, infile);
}


Now you can pass the total values for the house into the function calculate_material.

You might realize the the variable in_feet_1 and in_feet_2 could have more descriptive names. like in_feet_width and in_feet_height.

[UPDATE]
Just realized that this can be done much easier inside the calculate_drywall.
(Must have been eating a stupid pill yesterday)
Just update the parameter that is affected, and the variable in the main function will be updated as well.
Then keep the main function as the original.
C++
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;

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900