SUm of Items

I'm Trying to get the TOTAL of the Items that has been Entered,Basically The Program will ask How many Transaction then and each Transaction there is a MENU (1-7 Meals) after selecting the meal, it will ask how many order of that particular meal is then sum it up.the Problem is if I want to order the same meal with different transaction the program only recognize 1(one),instead of TOTAL of 80 it only show 40.can someone help me or give me some advice to enlighten my logic on this


cout<<"Select Menu ";
cin>>Menu;

switch(Menu)
{

case 1:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub = (Item * 40);
system("cls");
break;


case 2:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub = (Item * 50);
system("cls");
break;

default:
cout<<"wrong";
system("cls");
break;

TOTAL = Sub1+Sub2;
cout<<"Your Total Order is P"<<TOTAL<<"\n"
You don't show all relevant code.

I presume that you have some loop structure that repeatedly inquires for type and amount. The code that you do show does overwrite the "Sub" rather than adding to it. When you do change it to add, remember to zero-initialize the sum at start.
so far this is all i have

#include <iostream>
using namespace std;
main()
{

int Trans,Count,Menu,TOTAL,CASH,CHANGE;
int Item,Sub1,Sub2,Sub3,Sub4,Sub5,Sub6,Sub7;




while(TOTAL<=CASH)
{
cout<<"MY Canteen\n\n";
cout<<"1.Sample Meal P40\n";
cout<<"2.Sample Meal P50\n";
cout<<"3.Sample Meal P50\n";
cout<<"4.Sample Meal P60\n";
cout<<"5.Sample Meal P70\n";
cout<<"6.Sample Meal P80\n";
cout<<"7.Sample Meal P80\n";


cout<<"How Many Transaction? ";
cin>>Trans;
system("cls");

for(Count=1;Count<=Trans;Count++)
{

cout<<"Tansaction #"<<Count<<"\n\n";
cout<<"1.Sample Meal P40\n";
cout<<"2.Sample Meal P50\n";
cout<<"3.Sample Meal P50\n";
cout<<"4.Sample Meal P60\n";
cout<<"5.Sample Meal P70\n";
cout<<"6.Sample Meal P80\n";
cout<<"7.Sample Meal P80\n";




cout<<"Select Menu ";
cin>>Menu;

switch(Menu)
{

case 1:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub1 = (Item * 40);
system("cls");
break;


case 2:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub2 = (Item * 50);
system("cls");
break;


case 3:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub3 = (Item * 50);
system("cls");
break;


case 4:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub4 = (Item * 60);
system("cls");
break;


case 5:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub5 = (Item * 70);
system("cls");
break;


case 6:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub6 = (Item * 80);
system("cls");
break;


case 7:
cout<<"How Many order of Menu #"<<Menu<<": ";
cin>>Item;
Sub7 = (Item * 80);
system("cls");
break;


default:
cout<<"wrong";
system("cls");
break;

}
}


TOTAL = Sub1+Sub2+Sub3+Sub4+Sub5+Sub6+Sub7;

cout<<"Your Total Order is P"<<TOTAL<<"\n";
cout<<"Enter your CASH: ";
cin>>CASH;
CHANGE = CASH - TOTAL;
cout<<"Your Change is: "<<CHANGE<<"\n";
system("pause");
system("cls");


}

return 0;
}
Last edited on
You don't show all relevant code.

I presume that you have some loop structure that repeatedly inquires for type and amount. The code that you do show does overwrite the "Sub" rather than adding to it. When you do change it to add, remember to zero-initialize the sum at start.


sir,what do u mean by to zero-initialize the sum at start?????
A websearch with "zero-initialize" finds useful things.

Please, use the code tags. They make your code more readable.

Your code has essentially this:
1
2
3
4
5
6
7
int sub;
int item;
for ( int k = 0; k < 3; ++k )
{
  cin >> item;
  sub = item;
}

Lets rewrite that without the loop:
1
2
3
4
5
6
7
8
9
10
11
int sub;
int item;
// k=0
cin >> item;
sub = item;
// k=1
cin >> item;
sub = item;
// k=2
cin >> item;
sub = item;

Every time you do overwrite the sub with a new value. That is why you don't have a sum in the end.

You have to add:
1
2
3
4
5
6
7
8
9
10
11
int sub;
int item;
// k=0
cin >> item;
sub += item;
// k=1
cin >> item;
sub += item;
// k=2
cin >> item;
sub += item;

Now every new item increases the sub.
However, what is the value of sub before the first item is added to it?
A: Undefined
What is undefined + 1? Undefined.

Therefore, you have to set the value of sub before the loop.
1
2
3
4
5
6
7
8
int sub = 0; // initialize with zero
int item;
for ( int k = 0; k < 3; ++k )
{
  cin >> item;
  sub += item;
}
// Now sub is the sum of the items. 

Topic archived. No new replies allowed.