self taught project

hello all, i recently purchase Serendipity and im having trouble with my code on not wanting to display my total, subtotal and tax rate. i manage to get this far but i feel like im missing something but i cant seem to pinpoint it.

here is part of the code.
the out put displays everything corrrectly but my total, tax and subtotal all displays 0.
any reason why...

i have a feeling it something minor but i cant pinpoint it.


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  char date[9]; // MM/DD/YY
        int *qty;
        char **isbn; // at least 9-999-99999-98
        char **title;
        double *price;
 
        int i;
 
        double subtotal;
        double tax;
        double total;
        char choice;
 
        int numberOfTitles;     // number of titles to purchase
 
        do {
                subtotal = 0.0;
                tax = 0.0;
                total = 0.0;
 
                // Get book info
                cout << right; // right justify output in field width
                cout << endl << endl;
 
                cout << setw(32) << "Enter date : ";
               cin >> date;
 
                cout << setw(32) << "Number of titles in purchase: ";
                cin >> numberOfTitles;
                cin.ignore();   // throw away carriage return
 
                // allocate memory
                qty = new int[numberOfTitles];
                price = new double[numberOfTitles];
                title = new char *[numberOfTitles];
                isbn = new char *[numberOfTitles];
 
                for (i = 0; i < numberOfTitles; i++)
                {
                        title[i] = new char[51];
                        isbn[i] = new char[15];
                }
 
 
 
                // Multiple titles, to repeat for each title
                for (i = 0; i < numberOfTitles; i++)
                {
                        cout << endl << setw(32)
                                << "BOOK " << i + 1 << endl;
 
                        cout << setw(32) << "Enter integer quantity: ";
                        cin >> qty[i];
                        cin.ignore(); // throw away carriage return
 
                        cout << setw(32) << "Enter ISBN number (<=14 chars): ";
                        cin.getline(isbn[i], 15);
 
                        cout << setw(32) << "Enter title (<=50 chars): ";
                        cin.getline(title[i], 51);
 
                        cout << setw(32) << "Enter unit price: ";
                        cin >> price[i];
                        cin.ignore(); // throw away carriage return
 
 
                }
 
                // End multiple titles
 
                cout << endl << endl;
 
                cout << "Serendipity Booksellers\n\n";
                cout << "Date:" << endl << endl;
                cout << left;
                cout << setw(7) << "Qty"
                        << setw(17) << "ISBN"
                        << setw(28) << "Title"
                        << setw(10) << "Price"
                        << setw(10) << "Total"
                        << endl;
 
                // draw a line
                cout.fill('-');
                cout << setw(72) << "-" << endl;
                cout.fill(' ');
 
 
                // calc tax and update totals
                //    subtotal = subtotal + (qty*price);
                tax = subtotal * TAX_RATE;
                total = subtotal + tax;
 
                cout << setprecision(2) << fixed << showpoint;
 
                for (i = 0; i < numberOfTitles; i++)
                {
 
                        cout << setw(7) << qty[i]
                                << setw(17) << isbn[i]
                                << setw(24) << title[i]
                                << right << setw(3) << "$" << right << setw(6) << price[i]
                                //        << right << setw(4) << "$" << right << setw(6) << qty*price
                                << left << endl;
 
                }
 
                cout << endl << endl;
 
                cout << setw(24) << ' '
                        << setw(36) << left << "Subtotal"
                        << "$" << right << setw(6) << subtotal
                        << endl;
                cout << setw(24) << ' '
                        << setw(36) << left << "Tax"
                        << "$" << right << setw(6) << tax
                        << endl;
                cout << setw(24) << ' '
                        << setw(36) << left << "Total"
                        << "$" << right << setw(6) << total
                        << endl;
 
                cout << endl << "Thank You for Shopping at Serendipity!"
                        << endl;
 
                // check to see if there are more transactions
                cout << endl << "More transactions? (Y/N): ";
                cin >> choice;
                cin.ignore(80, '\n');
 
                while ((choice != 'y') && (choice != 'Y') && (choice != 'n') && (choice != 'N')) {
                        cout << "Please enter 'Y' or 'N': ";
                        cin >> choice;
                        cin.ignore(80, '\n');
                }
 
                // repeat if 'y' or 'Y'
        } while ((choice == 'y') || (choice == 'Y'));
 
        cout << endl << endl;
 
please help anyone
1
2
3
  //    subtotal = subtotal + (qty*price);
          tax = subtotal * TAX_RATE;
          total = subtotal + tax;

Tax/total will both be 0 with that first line commented out but I assume you know that and did it for testing. You are currently using "qty" and "price" as dynamically allocated arrays with "numberOfTitles" as the number of elements as shown here.

1
2
 qty = new int[numberOfTitles];
 price = new double[numberOfTitles];

So subtotal = subtotal + (qty*price); isn't going to work, you need to pick a specific element from those arrays. For example subtotal = subtotal + (qty[0]*price[0]); would give you the values for the 1st element in the arrays. I am guessing you will want to loop through every element using a for loop as you did previously.
It has a logical error at line 91: tax = subtotal * TAX_RATE; and initially subtotal is 0, so all calculations are resulting to 0.
Oh i see now. I finally got it to work with your guidance. Thanks James2250
hello once again.. i realize working on my code i notice it doesnt want to calculate my second inpout i asked for. it only calculate the first input.

i have where i have the correct total and tax but it doesnt add the total of the second input
it only add the 1st price, how would i make it where it add all of it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                Enter date as : 2
  Number of titles in purchase: 2

                           BOOK 1
           Enter Book Quantity: 3
            Enter ISBN number : 4
                  Enter title : 5
              Enter unit price: 6

                           BOOK 2
           Enter Book Quantity: 3
            Enter ISBN number : 5
                  Enter title : 6
              Enter unit price: 3





Qty    ISBN             Title                       Price     Total
3      4                5                         $  6.00
3      5                6                         $  3.00


                        Subtotal                            $ 18.00
                        Tax                                 $  1.08
                        Total                               $ 19.08

More transactions? (Y/N):
Last edited on
I think you are using this line of code
subtotal = subtotal + (qty[0]*price[0]);

you should sumup for numberOfTitles times

1
2
3
4
5
6
7
 
for (int indx = 0; indx < numberOfTitles; indx++)
{
    subtotal = subtotal + (qty[indx]*price[indx]);
}
tax = subtotal * TAX_RATE;
total = subtotal + tax;
upX86 i tred that but im currenty getting this out put. the correct answered should have been 6*2 qty=12+(7*3qty)=21=33+tax =34.26 as total. but im getting 22. unless im doing something wrong

1
2
3
4
5
6
7
8
9
Qty    ISBN             Title                       Price     Total
2      2                3                         $  6.00
3      6                5                         $  7.00


                        Subtotal                            $ 21.00
                        Tax                                 $  1.26
                        Total                               $ 22.26

More transactions? (Y/N):
Last edited on
Please share your code after line:86 till message "More transactions? (Y/N):"
Upx86 it took me a while to see what i was missing which was subtotal = 0.0. not having that gave me miscalculations. oh and more transaction (Y/N) is asking if the user wants to do another transaction. thanks for your help
Topic archived. No new replies allowed.