Reading in numbers from file with loop

I have a school assignment that's as the following.


Input: Use the following data, which can be found in the P4Boxes.txt file. Each row of data includes the 3 inner dimensions of a box (length, width, height) and the two outer dimensions of a jar (diameter, height). The dimensions are all in inches.

Box: Jar:
L W H D H

6.0 6.0 10.3 5.0 10.0
6.0 5.0 10.3 5.0 10.0
12.0 3.3 4.0 3.0 11.0
12.0 3.2 4.0 3.0 11.0
9.5 6.5 7.5 6.0 9.5
9.5 6.5 7.5 6.0 9.0
4.5 8.0 4.5 4.0 7.5
4.0 8.0 4.5 4.0 7.5
7.3 7.3 17.0 7.0 16.0
6.8 7.3 17.0 7.0 16.0
7.3 7.3 16.2 7.0 16.0
7.2 7.3 16.3 7.0 16.0

Output: Send the output to an external file. Line up decimal points in the output columns. The output file should include an appropriate header, the given points, and a “yes” or “no” verdict as to whether each jar will fit in its box.


I'm really lost as in how to read in the numbers and send them to my output correctly. When I compiled and opened the text file I sent it to only one line of numbers were listed under my heading. What exactly am I doing wrong? I know we can't use arrays yet cause this was assigned before we used arrays.


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
/* This program will read in various 
dimensions of jars and boxes then decide
if the jars firt in the box with .25 clearance
and send the output to external file */

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void  makeHeader ();
void Decide_and_Send(double L, double W, double Box_H, double D, double Jar_H);

int main()
{
    double L, W, Box_H;       // Box length, width, and height
    double D, Jar_H;          // Jar diameter and height   
    ifstream inFile;

    inFile.open("P4Boxes.txt");

    if (!inFile)
    {
        cout << "ERROR! File could not be opened." << endl;
        system("PAUSE");
        return 1;
    }

    inFile >> L;

    while (!inFile.eof() )
    {
        inFile >> W >> Box_H >> D >> Jar_H;
        Decide_and_Send(L, W, Box_H, D, Jar_H);
        inFile >> L;
    }


    inFile.close();
    return 0;
}



void Decide_and_Send(double L, double W, double Box_H, double D, double Jar_H)
{
    string fits_no;
    L = L - 0.25;
    W = W - 0.25;
    Box_H = Box_H - 0.25;

    if (L > D && W > D && Box_H > Jar_H)
        fits_no = "yes";
    else if (  W > D && Box_H > D && L > Jar_H)
         fits_no = "yes";
    else
        fits_no = "no";

//-----------------------------Begin writing into Output file

    ofstream outFile;              //Create object file and open header again
    outFile.open("header.txt");

    outFile << "Box:                  " << "Jar:          " << "Jar Fits?";
    outFile << "\n\nL" << setw(6) << "W" << setw(6) << "H" << setw(8)
         << "D" << setw(6) << "H" << setw(6) << " " << endl;   

    outFile << L << setw(6) << W << setw(6) << Box_H << setw(8)
         << D << setw(6) << Jar_H << setw(6) << fits_no << endl;  
 
    outFile.close();

}


You could open and close the output file in function main() and pass it by reference to the other function.
 
void Decide_and_Send(double L, double W, double Box_H, double D, double Jar_H, ofstream &outFile);


I'm not sure why you subtract 0.25 from each of the box dimensions?

Also, I didn't think of putting the jar on its side, that makes sense, but there are two ways it can fit, with the jar-height aligned with either the box length or its width (three ways altogether).
Topic archived. No new replies allowed.