calculating total

hey everyone. I've written a program but i can't seem to get it to calculate to total correctly could anyone take a look any give me a hand? thanks.

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int linearSearch (string [], int, string);
int main ()

{
    cout << "the available commands are ";
    cout << "/rawprices , /items , /invoice and\n"; 
    cout << "/ exits the program \n";
    cout << "please input one of the available commands: ";
    
    string item[100];
    string products[200];
    double prices[200];
    string str;
    int numPurchase = 0;
    int numProd = 0;
    int quantity[100];
    ifstream inFile;
    ofstream outFile;
    cin>> str;
    
   
    

    if( str == "/rawprices")
{
    
    cin >> str;
        
        while( str[0] !='/')
        {
               products[numProd] = str;
               cin>> prices[numProd];
               numProd++;
                cin >> str;  
        }     
}

    if( str == "/items")
{
    
    cin >> str;
        
        while( str[0] !='/')
        {
               
               item[numPurchase] = str;
               cin>> quantity[numPurchase];
               numPurchase++;
                cin >> str;  
        }     
}
    
      if (str == "/invoice")
      
      cout << "phantom company invoice\n\n";
      cout << "PRODUCT ID" << setw(13) << "QUANTITY" << setw(11) << "PRICE ($)" << setw(13) << "COST($)" << setw(11) <<"\n";
           
         int locations;
         double price;
         double cost;
         
         for (int i=0; i<numPurchase; i++)

{
    cout << item[i] << setw(13) << quantity[i] << setw(11);
    
    int location = linearSearch (products, numProd, item[i]);
    
    price = prices[location];
    
    cost = price * quantity[i];
    
    cout << price << setw(13) << cost << setw(11)<< "\n";
}  
  


system ("pause");
return 0;
}
 int linearSearch(string list[], int size, string key)
{
    int i;
    for (i = 0; i < size; i++)
    {if (list[i] == key)
    return i;
}
 return -1;
}
Last edited on
Firstly: http://www.cplusplus.com/forum/articles/1624/
Secondly: What error messages etc are you receiving? What are the symptoms of your problem?
I believe you are missing a { on the line if(str == "/invoice").
The program is ment to allow a user to input item names, they're price and quantity. It should then display the items like below


PHANTOM COMPANY INVOICE

PRODUCT ID QUANTITY PRICE ($) COST ($)
P010001 2 24.99 49.98
DVD-player5 1 58.95 58.95
P010002 20 8.85 177.00
...
TOTAL COST = $xxxxx.xx



My problem is i can't get an accurate total cost.
You have not included the code you use to calculate the total cost?
sorry i accidentally pasted an old file


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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int linearSearch (string [], int, string);
int main ()

{
    cerr << "the available commands are \n\n";
    cerr << "/rawprices, this is the first command needed when making an invoice.";
    cerr << " Here the \nproduct and price are entered\n\n"; 
             
    cerr << "/items is the seccond command used when making an invoice.\n";
    cerr << "The item name and then quantity are entered\n\n";
    
    cerr << "/invoice, once all data has been entered the user enters this\n";
    cerr << " command to view the outputed invoice\n\n";
    
    cerr << "/ this command exits the program\n\n";
    
    cerr << "please input one of the available commands: ";
    
    string item[100];
    string products[200];
    double prices[200];
    string str;
    int numPurchase = 0;
    int numProd = 0;
    int quantity[100];
    double total;
    cin>> str;
    
   
    

    if( str == "/rawprices")
{
    cerr << "\n\nenter the items name and then price\n";
    cerr << "for example type dog 1 and then press enter\n\n"; 
    cerr << "enter items and their prices one at a time, you can enter as many items\n";
    cerr << "as you like\n\n";
    cin >> str;
        
        while( str[0] !='/')
        {
               products[numProd] = str;
               cin>> prices[numProd];
               numProd++;
                cin >> str;  

        }     
}

    if( str == "/items")
{
    cerr << "\n\nnow enter the products name and how many items\n\n";
    cerr << "for example dog 50 and then press enter\n\n";
    cin >> str;
        
        while( str[0] !='/')
        {
               
               item[numPurchase] = str;
               cin>> quantity[numPurchase];
               numPurchase++;
                cin >> str;  
        }     
}
    
      if (str == "/invoice")
{
    cout << "phantom company invoice\n\n";
    cout << "PRODUCT ID" << setw(13) << "QUANTITY" << setw(11) << "PRICE ($)" << setw(13) << "COST($)" << setw(11) <<"\n";
           
         int locations;
         double price;
         double cost;
         
         for (int i=0; i<numPurchase; i++)

{
        
         cout << item[i] <<setw(17) << fixed << setprecision(0) << quantity[i];
         
         
             
    
    
    int location = linearSearch (products, numProd, item[i]);
    
    price = prices[location];
    
    cost = price * quantity[i];
    total = cost + cost;
    
    
    cout << setw(10) << fixed << setprecision(0)<< price;
    cout << setw(13) << fixed << cost;
    
    
    
}  
} 
   
   cout << setw(32) << "\n\n\nTOTAL COST = $" << total;


system ("pause");
return 0;
}
 int linearSearch(string list[], int size, string key)
{
    int i;
    for (i = 0; i < size; i++)
    {if (list[i] == key)
    return i;
}
 return -1;
}
Line 94: total = cost + cost; should be total += cost;
Line 30: double total; should be: double total = 0;
Last edited on
thanks mate
Topic archived. No new replies allowed.