How to repeat my outer loop?

Hello all again, I have a Lumber Order program I need to write. I need to get the loop to repeat itself and continue reading the infile. As of right now the program reads in the first Customer Order. IF anyone could assist me I would be very grateful!


You are to write a program to compute the cost of a lumber order for a local lumber company. The company sells pine, fir, cedar, maple and oak lumber and the lumber is priced by ‘board feet’. One board foot equals one square foot, one inch thick. The price per board foot is given in the following table:
Pine 0.89 Fir 1.09 Cedar 2.26 Maple 4.50 Oak 3.10

The lumber is sold in different dimensions (specified in inches for width and for height, and feet for the length) that need to be converted to board feet. For ex, a 2 x 4 x 8 piece is 2 inches wide, 4 inches high, and 8 feet long, and is equivalent to 5.333 board feet.

The input will be one customer and his lumber order. The lumber order will have several types of woods and different sizes in each order.

The input format for an order will be first: a label ‘Customer #:’ followed by the customer’s ID number. The next lines contain the lumber order, starting with a letter ( P, F, C, M, O ) corresponding to the five kinds of wood) or a T meaning total. When the letter is a T, there are no numbers following or this is the end of the order. The program should print out the customer number and an itemized order and print out the total cost of the order.

Example input:
Customer #: 1020
P 10 2 4 8 Pine 10 pieces 2 inches by 4 inches by 8 feet
M 3 1 6 10 Maple 3 pieces 1 inches by 6 inches by 8 feet

T Flag for the program to print the total cost of
this order.
Output;
Customer Order Report
Customer # 1020
Count Size Wood Cost
10 2x4x8 Pine $47.47
3 1x6x10 Maple $11.25

Total: $1340.28

Two sample data to use to test your code.
Customer #: 1234
M 5 1 8 8
C 10 1 6 12
C 5 1 12 12
P 8 2 4 10
P 6 2 6 12
T
Customer #: 3820
P 15 2 4 10
F 20 1 6 16
O 5 1 10 8
P 5 2 8 16
C 3 4 4 12
P 3 1 12 10
T


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

int main()
{
    ifstream inf("Lumber.in");
    if(!inf)
        cout << "ERROR CHECK FILE!";

    ofstream out("Lumber.out");

     string customer_id;
       char type, pound, colon;
      float total, cost,
            price, boardfeet;
        int number, width, height, length, quantity;

    out << "\t\tCUSTOMER ORDER REPORT\n";

        while(inf >> customer_id >> pound >> colon >> number)
            {
                out << customer_id << " " << pound << " "
                    << colon << " " << number << " \n"
                    << "Count   Size \tWood\tCost\n";

                while (inf >> type >> quantity >> width >> height >> length)
                {
                    out << " " << quantity << "\t   " << width
                        << "x" << height <<"x" << length << "  ";

                    switch(type)
                    {
                        case 'P': price = 0.89;
                            out << " Pine";
                        break;
                        case 'F': price = 1.09;
                            out << " Fir";
                            break;
                        case 'C': price = 2.26;
                            out << " Cedar";
                            break;
                        case 'M': price = 4.50;
                            out << "  Maple";
                            break;
                        case 'O': price = 3.10;
                            out << " Oak";
                            break;
                    }

                    boardfeet = (width*height*length*12)/144;
                         cost = (quantity * price) * boardfeet;
                       total += cost;
                    out << setprecision(2) << fixed
                        << "\t$" << cost << endl;
                }
                out << "\n\t\t\t\tTotal:  $"<< total;
            }
out.close();
inf.close();
return 0;
}



Here's the input data:

Customer #: 1234
M 5 1 8 8
C 10 1 6 12
C 5 1 12 12
P 7 2 4 10
P 6 2 6 12
T
Customer #: 3820
P 15 2 4 10
F 20 1 6 16
O 6 1 10 8
P 5 2 8 16
C 2 4 4 12
P 3 1 12 10
T
Customer #: 7923
O 8 1 6 10
O 5 2 6 8
O 4 1 8 10
O 2 1 12 10
T
Customer #: 2482
C 10 1 8 10
C 8 2 5 16
M 9 4 6 12
F 5 6 6 12
C 2 1 12 16
T
Customer #: 7783
F 2 1 4 10
M 8 2 5 10
T
Customer #: 4913
P 15 2 4 9
P 20 2 6 24
P 14 2 10 20
P 20 2 4 10
F 1 1 6 10
F 10 2 6 12
T
Customer #: 1178
P 25 2 4 10
P 20 2 4 8
P 10 2 4 12
F 20 1 6 16
F 15 1 4 12
O 6 1 10 8
P 5 2 8 16
P 8 2 8 10
C 2 4 4 12
C 2 4 4 8
P 3 1 12 10
T

This is my output from the code above:
CUSTOMER ORDER REPORT
Customer # : 1234
Count Size Wood Cost
5 1x8x8 Maple $112.50
10 1x6x12 Cedar $135.60
5 1x12x12 Cedar $135.60
7 2x4x10 Pine $37.38
6 2x6x12 Pine $64.08

Total: $485.16
Last edited on
Well. I can start you off I suppose. I'm no expert, but I see the first issue.

1
2
3
4
5
6
 while(true) // infinite loop. do while(inf) instead
    {
        inf >> number; // aren't you skipping some text and a symbol here?
        out << "\nCustomer #:" << number << "\n";
        inf >> type;
    }


You need to either skip customer # or put it in a string or array.
Another issue is that you're storing, assuming your code was good, only the last line. You are overriding each variable with every line read in.

If you're not doing an array, your calculations and a running sum has to be totaled inside the while loop.
Last edited on
.
Last edited on
Edited the OP. Ignore the current comments
Last edited on
bump
Before you try any processing of data, make sure you can read the file properly.
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    ifstream inf("foo.txt");
    string line;
    
    string customer_id;
    char type, pound, colon;
    float total, cost,  price, boardfeet;
    int number, width, height, length, quantity;
        
    while ( getline(inf,line) ) {
        istringstream is(line);
        if ( is >> customer_id >> pound >> colon >> number && customer_id == "Customer" ) {
            cout << "L1:" << customer_id << " " << pound << " "
                    << colon << " " << number << " \n";
        }
        else {
            is.seekg(0).clear();
            if ( is >> type >> quantity >> width >> height >> length ) {
                cout << "L2:" << type << " " << quantity << "\t   " << width
                        << "x" << height <<"x" << length << "\n";
            }
        }
    }
    return 0;
}


$ ./a.out 
L1:Customer # : 1234 
L2:M 5	   1x8x8
L2:C 10	   1x6x12
L2:C 5	   1x12x12
L2:P 7	   2x4x10
L2:P 6	   2x6x12
L1:Customer # : 3820 
L2:P 15	   2x4x10
L2:F 20	   1x6x16
L2:O 6	   1x10x8
L2:P 5	   2x8x16
L2:C 2	   4x4x12
L2:P 3	   1x12x10
L1:Customer # : 7923 
L2:O 8	   1x6x10
L2:O 5	   2x6x8
L2:O 4	   1x8x10
L2:O 2	   1x12x10
L1:Customer # : 2482 
L2:C 10	   1x8x10
L2:C 8	   2x5x16
L2:M 9	   4x6x12
L2:F 5	   6x6x12
L2:C 2	   1x12x16
L1:Customer # : 7783 
L2:F 2	   1x4x10
L2:M 8	   2x5x10
L1:Customer # : 4913 
L2:P 15	   2x4x9
L2:P 20	   2x6x24
L2:P 14	   2x10x20
L2:P 20	   2x4x10
L2:F 1	   1x6x10
L2:F 10	   2x6x12
L1:Customer # : 1178 
L2:P 25	   2x4x10
L2:P 20	   2x4x8
L2:P 10	   2x4x12
L2:F 20	   1x6x16
L2:F 15	   1x4x12
L2:O 6	   1x10x8
L2:P 5	   2x8x16
L2:P 8	   2x8x10
L2:C 2	   4x4x12
L2:C 2	   4x4x8
L2:P 3	   1x12x10


Now add a couple of lines to recognise the T by itself on a line.

When you're happy with that, you can work on the calculations and output.
Salem, thank you for your help, unfortunately, I can not use your suggestion. Professor hasn't covered sstream, therefore I can't use it. Any sort of loop? The Value of 'T' is my sentential value if that helps at all?

I've tried all sorts of variations of while(type != 'T') or if(type == 'T') else if(type != 'T'), they all end up in an infinite loop.
- Read the customerID as a whole line. After all, you print it out exactly as read.
- The inner loop reads one character and then decides what to do. If the character is 'T' then you read the newline(!) and exit the loop. Otherwise you read the board feet etc.
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int
main()
{
    string customer_id;
    string type;
    float total, cost, price, boardfeet;
    int width, height, length, quantity;

    cout << "\t\tCUSTOMER ORDER REPORT\n";

    // Read the entire customer ID line
    while (getline(cin, customer_id)) {
	cout << customer_id << '\n';

	// Don't forget to set the total.
	total = 0.0;
	// Sometimes the exit condition of a loop is in the middle
	// of the loop itself. Don't be afraid to code it that way.
	while (true) {
	    cin >> type;
	    if (type == "T") {
		// You read the "T" but not the newline after it. So ignore
		// Through the newline
		cin.ignore(1000, '\n');
		break;
	    }
	    
	    // If you get here, the type is a a wood type. Read the
	    // rest of the line:
	    cin >> quantity >> width >> height >> length;
	    cout << " " << quantity << "\t   " << width
		<< "x" << height << "x" << length << "  ";

	    switch (type[0]) {
	    case 'P':
		price = 0.89;
		cout << " Pine";
		break;
	    case 'F':
		price = 1.09;
		cout << " Fir";
		break;
	    case 'C':
		price = 2.26;
		cout << " Cedar";
		break;
	    case 'M':
		price = 4.50;
		cout << "  Maple";
		break;
	    case 'O':
		price = 3.10;
		cout << " Oak";
		break;
	    }

	    boardfeet = (width * height * length * 12) / 144;
	    cost = (quantity * price) * boardfeet;
	    total += cost;
	    cout << setprecision(2) << fixed << "\t$" << cost << endl;
	}
	cout << "\n\t\t\t\tTotal:  $" << total << "\n\n";
    }
    return 0;
}

Topic archived. No new replies allowed.