how to only tax certain items that are in an array, also some output help

closed account (EAk1vCM9)
Hello everyone, so im working on a program were I have to read from a file and tax items and present them in a specific way . My issue now is im not sure how to choose those specific items and not add tax to them but still print out that the tax was 0.00. Also I wanted to know how I could fix my program so my output would be printed like this:
Item                  Cost      Tax    Subtotal
-----------         ------    -----    --------
apple                 0.89     0.00        0.89
book                135.00    12.15      147.15
eraser                0.59     0.05        0.64
flashdrive            7.99     0.72        8.71
folder                1.29     0.12        1.41
highlighter           0.98     0.09        1.07
...
                                       --------
                                Total    7??.??


currently my program outputs this:
Item              Cost      Tax    Subtotal    
-----------     ------    -----    --------    
                           0.08      0.97
                          12.15    147.15
                           0.05      0.64
                           0.72      8.71
                           0.12      1.41
                           0.09      1.07
                          45.00    545.00
                           1.35     16.33
                           0.45      5.45
                           0.21      2.50
                           0.03      0.38
                           0.18      2.16
                           0.09      1.09
                                  --------
                            Total  732.85
apple             0.89
book            135.00
eraser            0.59
flashdrive        7.99
folder            1.29
highlighter       0.98
laptop          500.00
mouse            14.98
notebook          5.00
paper             2.29
pencil            0.35
pen               1.98
water             1.00


By the way the two items that shouldnt be taxed are apples and water


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
 ifstream infile("studentList.txt");
    if (!infile)
    {
        cout << "Could not open input file\n";
        return 1;
    }

    // Ignore first line
    string dummy;
    getline(infile,dummy);

    int count = 0;



    while ( count < SIZE &&  infile >> item[count] >> cost[count] ) //reads from file
    {
        count ++;
    }
    infile.close();

    headings(); //prints out headings
    tax(cost,SIZE);


    // print out whatever is stored in the arrays
    for (int i=0; i<count; ++i)
    {
        cout<< left <<setw(11)<< item[i] << " "<< right << setw(10)<< fixed << setprecision(2)<< cost[i] <<endl;
	}

}

double tax(double cost[],int SIZE)
{
	int i;
	double tax=0.0, subtotal=0.0,total=0.0;
    const double TAX_RATE= 0.09;


    for(i=0; i< SIZE; i++)
    {
    	tax = (TAX_RATE * cost[i]); // calculates tax
    	subtotal = (tax + cost[i]); //calculates subtotal
    	total+=subtotal;
    	cout << right << setw(31)<< fixed << setprecision(2) << tax << setw(10) << fixed << setprecision(2) << subtotal <<endl;
    }
    cout << setw(42) << "--------" <<endl;
    cout << setw(34) <<" Total "  << setw(7) << total<<endl;

    return tax;
}

void headings()
{
    // these are the headings
    cout << left << setw(16) <<"Item" << left << setw(10) << "  Cost" << left << setw(9)  << "  Tax"<< left << setw(12) << "Subtotal" << endl;
    cout << left << setw(16) <<"-----------" << left << setw(10) << "------" << left << setw(9) << "-----" << left  << setw(12) << "--------" << endl; // these are the headings


}
Last edited on
While normaly it is good to seperate code into independent functions in this case it is not.

The easiest would be to pack the content of loop 1 (line 27) and 2 (line 41) into one (line 16). Move line 22 before line 16 and 48/49 after line 19.

So, you wouldn't need arrays anymore.

For special cases like apple/water use an if:
1
2
3
4
5
6
if(item == "apple")
  tax = 0;
else if(...)
  ...
else
  tax = (TAX_RATE * cost[i]); // calculates tax 
Topic archived. No new replies allowed.