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
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
}
elseif (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();
}
elseif (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();
}
}
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