While loop inside a for loop problem

Hi so this program is supposed to read from a txt file three quantities for 10 customers. It is supposed to add the price of these goods (a random amount for each customer) apply a discount and/or service charge and give the total fee for each customer. The problem is we're supposed to use zeros to end each customer's transaction. Depending on the condition I use for the while loop it will only run once and then go through the for loop 10 times without going through the while loop again. Or the opposite it only goes through while loop and then goes through the for loop 9 times after the while loop is done.

Apologies for being a super noob

I've tried for 2 days to figure this out on my own, any help would be appreciated.

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
#include <iostream>
#include <fstream>
#include <cstdlib>


using namespace std;

int main()
{
    int number, amount,customer;
    float price, discount, oldprice, newprice, total;

    ifstream infile("crooked.txt");
    ofstream outfile("homework 2.txt");

    if ( !infile.is_open() ) {
        cout << endl << "ERROR: Unable to open file" << endl;
        system ("PAUSE");
        exit(1);
    }

    outfile.setf(ios::fixed,ios::floatfield);
    outfile.precision(2);

    outfile << "Number\t" << "Amount\t" << "Old Price\t"
            << "Discount\t" << "New Price" << endl;

        for (customer = 1; customer <= 10;customer++) {
             total = 0;
             infile >> number;

    while (!(number == 0 && amount == 0 && price == 0)) {

        infile >> amount;
        infile >> price;
        oldprice = amount * price;

        if (amount <= 30)
            discount = oldprice * 0.05;
        else
            if (amount <= 50)
            discount = oldprice * 0.10;

            else
                if (amount <= 75)
                discount = oldprice * 0.25;

            else
                discount = oldprice * 0.40;

        newprice = oldprice - discount;

        total += newprice;

        outfile.setf(ios::right);
        outfile << number << "\t" << amount << "\t\t" << oldprice
        << "\t\t" << discount << "\t\t" << newprice << endl;

        infile >> number;


        }
            if (total < 500){
            total += 20;
            outfile << "\n\n\n\nTotal order is: " << total << endl;
            outfile << " $20 Service Charge\n\n\n\n\n";

            }else {
            if (amount < 50)
            total = total * 0.95;
            outfile << "\n\n\n\nTotal order is: " << total << endl;
            outfile << " 5% Discount\n\n\n\n\n";
        }

    }
        system("PAUSE");
        return 0;
}


Edit:

crooked.txt

1111 25 2.32
2222 14 1.81
3333 30 4.61
0 0 0

4444 35 8.61
5555 78 10.62
0 0 0

6666 5 52.61
7777 8 4.91
8888 10 .24
0 0 0

9999 52 1.32
1000 121 .68
2000 24 2.69
0 0 0

3000 2 152.68
4000 40 29.60
5000 1 1.40
0 0 0

6000 14 100.00
7000 38 .52
8000 70 1.02
9000 2 90.45
0 0 0

1234 40 .75
5678 15 1.25
0 0 0

9876 50 2.30
1357 90 1.75
2468 10 3.62
0 0 0

9753 5 10.00
9631 36 4.50
1010 45 8.27
0001 12 14.20
0 0 0

0011 7 25.00
0111 13 85.99
2111 65 4.95
0 0 0
Last edited on
It is always useful to give the crooked.txt contents particulary as it only has 10 customers so the code can be tested.
You're biting off things in bigger chunks that you should. Start smaller and build up to what you want to do. You should also write out in English what your algorithm is before you start coding. Presenting that description with your code will make it easier to see what you're having trouble with.

I would start with a simple program that just lists the values read from the file for each customer:

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
#include <fstream>
#include <iostream>

int main()
{
    const char* in_file_name = "crooked.txt";

    std::ifstream in(in_file_name);

    std::ostream& out = std::cout;
    // std::ofstream out("homework 2.txt");

    if (!in.is_open())
    {
        std::cerr << "ERROR: Unable to open file \"" << in_file_name << "\"\n";
        return 0;
    }

    int item_id, item_count;
    double price;

    while (in >> item_id >> item_count >> price)
    {
        out << item_id << '\t' << item_count << '\t' << price << '\n';
        
        if (item_id == 0 && item_count == 0 && price == 0.0)
            out << '\n';
    }
}


Notice that we loop on the success of extracting input from the stream. In this way we don't have to worry about the number of actual records in the file.

Having verified that was correct, I would move on to providing the line item discount, probably via a function.

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
#include <fstream>
#include <iostream>

double apply_discount(int item_count, double total)
{
    if (item_count <= 30)
        return total * 0.95;
    if (item_count <= 50)
        return total * 0.80;
    if (item_count <= 75)
        return total * 0.75;
    return total * 0.60;
}

int main()
{
    const char* in_file_name = "crooked.txt";

    std::ifstream in(in_file_name);

    std::ostream& out = std::cout;
    // std::ofstream out("homework 2.txt");

    if (!in.is_open())
    {
        std::cerr << "ERROR: Unable to open file \"" << in_file_name << "\"\n";
        return 0;
    }

    int item_id, item_count;
    double price;

    while (in >> item_id >> item_count >> price)
    {
        if (item_id == 0 && item_count == 0 && price == 0.0)
        {
            out << '\n';
        }
        else
        {
            double total = price * item_count;
            double discounted_total = apply_discount(item_count, total);

            out << item_id << '\t' << item_count << '\t' << price << '\t';
            out << total << '\t' << discounted_total << '\n';
        }
    }
}


And once I had verified that was correct I'd probably move on to totaling things for each individual customer record:

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 <fstream>
#include <iostream>

double apply_discount(int item_count, double total)
{
    if (item_count <= 30)
        return total * 0.95;
    if (item_count <= 50)
        return total * 0.80;
    if (item_count <= 75)
        return total * 0.75;
    return total * 0.60;
}

int main()
{
    const char* in_file_name = "crooked.txt";

    std::ifstream in(in_file_name);

    std::ostream& out = std::cout;
    // std::ofstream out("homework 2.txt");

    if (!in.is_open())
    {
        std::cerr << "ERROR: Unable to open file \"" << in_file_name << "\"\n";
        return 0;
    }

    int item_id, item_count;
    double price;


    double record_total = 0.0;
    while (in >> item_id >> item_count >> price)
    {
        if (item_id == 0 && item_count == 0 && price == 0.0)
        {
            out << "Total order is: " << record_total << "\n\n";
            record_total = 0.0;
        }
        else
        {
            double total = price * item_count;
            double discounted_total = apply_discount(item_count, total);

            out << item_id << '\t' << item_count << '\t' << price << '\t';
            out << total << '\t' << discounted_total << '\n';

            record_total += discounted_total;
        }
    }
}


And I believe you should probably be able to adapt that to your purposes.
Thanks I got it, output is crooked in some spots though and I don't know how to align except for with \t.
Topic archived. No new replies allowed.