I can't find the error. Logical error. Total and tax comes as 0.

After logging in,enter a desired item number. press 0. then total comes as 0.

#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>   


using namespace std;
void mainfunc ();
void Exit();



struct menuItemType {int itemno;double price;string menuItem;};

void getData(menuItemType * menu) {

menu[0].itemno = 1001;
menu[0].price = 400;
menu[0].menuItem = "Submarine";
menu[1].itemno = 1002;
menu[1].price = 490;
menu[1].menuItem = "Pizza";
menu[2].itemno = 1003;
menu[2].price = 310;
menu[2].menuItem = "Club Sandwitch";
menu[3].itemno = 1004;
menu[3].price = 250;
menu[3].menuItem = "Biriyani";
menu[4].itemno = 1005;
menu[4].price = 320;
menu[4].menuItem = "Singapore Fried Rice";
menu[5].itemno = 1006;
menu[5].price = 180;
menu[5].menuItem = "Chicken Burger";
menu[6].itemno = 1007;
menu[6].price = 120;
menu[6].menuItem = "Crush";
menu[7].itemno = 1008;
menu[7].price = 80;
menu[7].menuItem = "Ice Cream";
}

void showMenu (menuItemType * menu) {   
system("pause");
cout << "*************Welcome to Delicious Food Planet************" << endl;
cout<<endl << endl << "Tax rate: 5%" << endl<< endl << endl;
cout << "Item No\t\tMenu Item\t\tPrice" << endl;
for (int i=0;i<8;i++) {
cout << menu[i].itemno << "\t\t" << menu[i].menuItem << "\t\t$" << menu[i].price << endl;

}
}

int takenorder [8];

void printcheck (menuItemType * menu)
{
double total=0;
double tax;
int loop;
system("pause");
cout << "*************Welcome to Delicious Food Planet************" << endl;
cout << "Item No\t\tMenu Item\t\tPrice" << endl;

for (int i=0;i<8;i++) {
switch (takenorder [i])
    {
case 111:
cout << menu[0].itemno << "\t\t" << menu[0].menuItem << "\t\t$" << menu[0].price << endl;
total = total + menu[0].price;
break;

case 112:
cout << menu[1].itemno << "\t\t" << menu[1].menuItem << "\t\t$" << menu[1].price << endl;
total = total + menu[1].price;
break;
case 113:
cout << menu[2].itemno << "\t\t" << menu[2].menuItem << "\t\t$" << menu[2].price << endl;
total = total + menu[2].price;
break;

case 114:
cout << menu[3].itemno << "\t\t" << menu[3].menuItem << "\t\t$" << menu[3].price << endl;
total = total + menu[3].price;
            break;

case 115:
cout << menu[4].itemno << "\t\t" << menu[4].menuItem << "\t\t$" << menu[4].price << endl;
total = total + menu[4].price;
break;

case 116:
cout << menu[5].itemno << "\t\t" << menu[5].menuItem << "\t\t$" << menu[5].price << endl;
total = total + menu[5].price;
break;
case 117:
cout << menu[6].itemno << "\t\t" << menu[6].menuItem << "\t\t$" << menu[6].price << endl;
total = total + menu[6].price;
break;

case 118:
cout << menu[7].itemno << "\t\t" << menu[7].menuItem << "\t\t$" << menu[7].price << endl;
total = total + menu[7].price;
break;
}
}
//hwre
char decision,n='n';
tax = total * 0.05;
total = total + tax;
cout << endl << "Tax\t\t\t\t\t" << tax << endl;
cout << "Total Amount \t\t\t\t" << total << endl << endl << endl << "\t\tThank you!";
for (loop = 1;loop=-1;loop++)
{
cout << endl << endl << endl << endl << "Press 'n' for new Billing, press 'q' to exit!" << endl << ">";
cin>>decision;

if (decision=='n')
{
mainfunc ();
}
else if (decision=='q')
{
system("cls");
cout<<"\n\t\t\t     *** Thank You ***"<<endl;   
cout<<"\t\t\t *** Have a Taste Again ***"<<endl;


getch();
}
else
{
cout<<" sorry! Try again";
mainfunc ();
}
}
}

void mainfunc () {
int order;
int counter = 0;
menuItemType * menulist = new menuItemType[8];
getData(menulist);
showMenu(menulist);
for (counter = 0;counter<8;counter++)
{
cout << endl << endl << "Enter desired item ID! Type '0' to finalize bill!" << endl << ">";
cin >> order;
switch (order)
{
case 0:
counter = 8;
break;

case 1001:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1001;
break;
case 1002:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1002;
break;
case 1003:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1003;
break;
case 1004:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1004;
break;
case 1005:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1005;
break;
case 1006:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1006;
break;
case 1007:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1007;
break;
case 1008:
cout << "Order " << counter + 1<< " taken" <<endl;
takenorder[counter] = 1008;
break;
default:
cout << "Invalid order! Try again please!" <<endl;
counter = counter - 1;
break;
}
}
printcheck(menulist);
getch();
}

void main()
{

string user;
string password;
char res='Y';
while (res=='Y' || res=='y')
{
cout<<endl;
cout<<endl;
cout<<"\t\t\t Delicious Food Planet" <<endl;
cout<<"\t\t\t User Login"<<endl;
cout<<"\t\t\t **********" <<endl;
cout<<endl;
cout<<"\t\tInput User ID  :  ";cin>>user;
cout<<"\t\tInput Password :  ";cin>>password;
if (user=="ifam" && password=="ifam")
{
cout<<endl<<"\t\tYou have been logged in!"<<endl<<endl;
res='n';
mainfunc();
}
else 
{
cout<<endl;
cout<<"\t\tInvalid user!!!!!!!!!!!!!"<<endl;
cout<<endl;
cout<<endl;
cout<<"\t\tDo you want to try again:"<<endl;
cin>>res;
system("pause");
}
}
}

This is some hideous code. You need to put a lot more thought into things before you touch the keyboard.

Anyway, looks like you calculate the total cost of the order using the array int takenorder [8]; That looks like a global array. Global values are, as a general rule, a really really bad idea, and trashes the whole point of functions and data encapsulation and all that.

Looks like you only ever write to that array like this:
takenorder[counter] = 1001;
to
takenorder[counter] = 1008;

so in that array will be numbers between 1001 and 1008.

Then, when you make the total, you examine that array like this:
switch (takenorder [i])
and you make cases for the value being 111 to 118.

But you never put the number 111 or 112 or 113 or 114 or 115 or 116 or 117 or 118 in there.
You only put in numbers 1001 or 1002 or 1003 or 1004 or 1005 or 1006 or 1007 or 1008.
So your function that is supposed to calcualte the total is looking for values you never put in the array.

The code for your item 'pizza' is 1002, but your code for calculating the total is using 112 as the code for 'pizza'.

This sort of thing is why we have enums, or const values, or maps, or lots of other options that do not involve you manually entering the same number in three different places in your code. Every time you type in the same data is a chance to get it wrong.

Edited 3 Years Ago by Moschops

This question has already been answered. Start a new discussion instead.