Having trouble in adding using loop

What can i do ? The output of my sum is garbage value. what do you think is the problem ?

//This program will record the
#include <iostream> //pre-processor directives
#include <conio.h>
#include <string>
#include <cmath>
using namespace std; //directive


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
main ()
{
         int ch,ctr1,ctr2,n1,code[100],cost[100], sum=0,i,n=1;
         string name[100];                           //accepts the product name
         
         cout<<endl;
         cout<<"\n\tWW     WW  EEEEEE  LL      CCCCCCC  OOOOOOOO  MM     MM  EEEEEE"<<endl;//Welcome's the user.
         cout<<"\tWW     WW  EE      LL      CC       OO    OO  MMM   MMM  EE"<<endl;
         cout<<"\tWW WW  WW  EEEE    LL      CC       OO    OO  MM  M  MM  EEEE"<<endl;
         cout<<"\tWWW   WWW  EE      LL      CC       OO    OO  MM     MM  EE"<<endl;
         cout<<"\tWW     WW  EEEEEE  LLLLLL  CCCCCCC  OOOOOOOO  MM     MM  EEEEEE"<<endl;
         
         cout<<"\n\t\t\t    7-Eleven Inventory System";
   
         
         cout<<"\n\n\n\t\t\t -------------------------------\n\n\t\t\t\tSystem Reminders: ";
         cout<<"\n\n\n\t\t * This system will record all the products\n\t\t   that are sold for the whole day.";
         cout<<"\n\n\t\t * Enter the required information to record the product.";
         cout<<"\n\n\t\t * You will only be able to display the\n\t\t   items once you added records.";
         
         
         getch();
         
         
         for(;;)                                     //used to loop the program
         {
             system ("cls"); 
          
             cout<<"\n\n\n\n\t\t\t   **********************"<<endl;  //Menu optiona
             cout<<"\t\t\t   *  0 - Exit          *"<<endl;
             cout<<"\t\t\t   *  1 - Add Item      *"<<endl;
             cout<<"\t\t\t   *  2 - Display Items *"<<endl;
             cout<<"\t\t\t   **********************"<<endl; 
             
             cout<<"\n\t\t    Please select the number of your choice: "; // asks the user what to choose
             cin>>ch;
             
                     if (ch==0)                                         //if 0 was chosen,the program will exit.
                     {
                       cout<<"\n\t\t\tThank you for using the program!";
                       getch();
                       exit(0);                                         //used to exit the program
                     }
             
                     else if (ch==1)                                     //if 2 is pressed, the bosy of 2 will be executed
                     {
                        cout<<"\n\t\t\t    How many records : ";
                        cin>>n1;                                         //signal for the loop
                        system ("cls");
                               for(ctr1=1;ctr1<=n1;ctr1++)         
                               {
                               cout<<"\nItem No "<<ctr1<<" :";
                               cout<<"\n\tEnter Item Name: ";
                               cin>>name[n];                             //accepts the string name
                       
                               cout<<"\n\tEnter Item code: ";
                               cin>>code[n];                             //accepts the item code
                         
                               cout<<"\n\tEnter the Cost of the Item: ";
                               cin>>cost[n];                            //accepts the item cost
                         
                               n++;
                               }
                  
                        cout<<"\n*****************\n";
                        cout<<"\n"<<n1<<" records added !";
                        getch();
                        }
                        
                        
                        else if (ch=2)
                        {
                        system ("cls");
                        sum=0;
                        cout<<"\n\n\n\t\t\t----------Stock Information---------\n\n";
                        cout<<"\n\t\t\tName\t\tCode\t\tCost\n\n";
                        
                                         for(i=1;i<ctr1;i++)
                                         cout<<"\t\t\t"<<name[i]<<"\t\t"<<code[i]<<"\t\t"<<cost[i]<<"\n\n"; 
                                         //Displays all the information that the user input.
                        
                        sum = sum + cost[i];
                        cout<<"\n\t\t\t    Total Sales for Today : "<<sum;
                        cout<<"\n\n\t\t\t------------------------------------";
                     
                        getch();
                        }
                        
                        else
                            cout<<"\nInvalid Input! ";
                            getch();
                            
                            
                  }
}
Last edited on
Please use code tags...
...the main functon must return an int
getch(); is not C++ and in fact <conio.h> is not a standard header use cin.get();
If the values in the cost array is large you can use the long long type. This is C++11

Aceix.
Last edited on
im sorry im a noob . what do you mean tags ?
Instead of using

int main() {}

Write

[code]int main() {}[/cout]


This will look like:

int main() {}

Which makes it way easier to read and understand your code.
Last edited on
What is long long type ?
Last edited on
what's wrong in the syntax ?
sum = sum + cost[i];
i isn't defined. If you don't use braces in a for loop, the scope is only the first line.
@OP check my edit at the top(2nd post)

Aceix.
Topic archived. No new replies allowed.