FUN with FUNCTIONS

Write a program to read in the dimension of a box, length, width and height. You are to compute the area of the box. Print out the dimensions of the box and the area of the box.

Functions to write:
Write a function (Read) to read in one value.
(Used to read in the three inputs.)
Write a function to compute area (Area) and return the area.
Write a function to print out the dimensions and the area of the box.

There will be several boxes for inputs, one per line.

Input File: Boxes.txt

Example input: 5 3 6
4 8 7


Output: Table
Dimension Area
5x3x6 90
4x8x7 224
….

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

//Function Prototypes
int Read();
int Area(int,int,int);
void Write(int,int,int,int,ofstream);
void PrintHeader();

ofstream out("Area.out");
ifstream inf("Area.in");


void PrintHeader(ofstream& out)
{
    out << "\t\t\tBOX PROGRAM\n"
        << " Dimension\tArea";
}

int Read(ifstream& inf)
{
    int z;
    inf >> z;
    return z;
}

int Area(int w, int h, int l)
{
    return w*h*l;
}

void Write(int a, int b, int c, int d, ofstream& out)
{
    out << " " << a << "x" << b << "x" << c
        << "\t" << d << endl;
}

int main()
{
    int x,y,z,j;
    PrintHeader(out);
    Read(inf);
    Read(inf);
    Read(inf);
    Area(x,y,z);
    Write(x,y,z,j,out);
inf.close();
out.close();
return 0;
}


I am having issues with this. I'm unsure how to correct an error. ON the line in MAIN for write(x,y,z,j,out); I get an error about it being overloaded. I'm not sure if I'm even doing this problem correctly.
Last edited on
Your Write function expects 4 arguments to be passed into it.
You're passing 5 arguments. Note that nowhere are you "[printing] ... the area of the box". Hint: What does the Area function return?
Last edited on
Your definition of function Write() on line 35 says that it takes 4 parameters: a, b, c, and out.

On line 49 you call Write() but you pass 5 parameters: x, y, z, j, and out. Since the number of parameters differs, the compiler doesn't know what to do.

To fix this:
- Change line 35 to include the 5th parameter (area).
- You don't set x, y, z or j. You need to assign these to the values returned by the appropriate functions. For example, line 45 should probably be x = Read(inf);

Other comments:
- This is computing volume, not area. You should change the names accordingly.
- Use a more descriptive name instead of j. When you start writing more complex code, you'll need descriptive names.

I will be altering the names to be more descriptive as soon as I am finished.
After the altering, I still receive the overload error on write(int,int,int,int,ofstream).


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
//Function Prototypes
int Read();
int Area(int,int,int);
void Write(int,int,int,int,ofstream);
void PrintHeader();

ofstream out("Area.out");
ifstream inf("Area.in");


void PrintHeader(ofstream& out)
{
    out << "\t\t\tBOX PROGRAM\n"
        << " Dimension\tArea";
}

int Read(ifstream& inf)
{
    int z;
    inf >> z;
    return z;
}

int Area(int w, int h, int l)
{
    return w*h*l;
}

void Write(int a, int b, int c, int d, ofstream& out)
{
    out << " " << a << "x" << b << "x" << c
        << "\t" << d << endl;
}

int main()
{
    int x,y,z,j;
    PrintHeader(out);
    x=Read(inf);
    y=Read(inf);
    z=Read(inf);
    j=Area(x,y,z);
    Write(x,y,z,j,out);
inf.close();
out.close();
return 0;
}


Last edited on
I also need to make this program read to the end of a file. Would I just include a FOR statement within the main function?
UPDATE: I've got the program to run and read in the first value. Thank you both for the advice to include the x=read(inf);.

Now I just gotta figure out how to complete the infile reading.
1
2
3
ifstream inf("Box.in");
    if(!inf)  
        cout<<"error";


Why do I receive an error with this statement?
error: expected unqualified-id before 'if'

I should include these are considered global. IF that matters?!
Last edited on
Solved the program.. I added while(!inf.eof()) {functions}
Topic archived. No new replies allowed.