Hello!

I have this homework problem, that I thought I had figured out, but once I fixed all the syntax errors and debugged it again it gave me some weird numbers...something I totally didn't expect. Can anyone give me a clue as to where I went wrong? I suspect that it has something to do with how my array has been passed. Or maybe how the information has been input into the strut, but I'm not sure how to fix it.

The Problem: Write a program to help a local restaurant automate it's breakfast billing system.

Must Have:

  1. Function getData: loading input into array menuList.
  2. Function showMenu: show the menu choices and display instructions.
  3. Function printCheck: calculate the bill and print the check.

What output after I debugged:
Welcome to Johnny's Resturant.

(1)Plain Egg $1.45
(2)Bacon and Egg $2.45
(3)Muffin $0.99
(4)French Toast $1.99
(5)Fruit Basket $2.49
(6)Cereal $0.69
(7)Coffee $0.50
(8)Tea $0.75

Enter the number corresponding with your choice then press the space bar.
Enter - 999 to stop.

Enter the menu choice(s): 1 2 3 - 999
Welcome to Johnny's Resturant

$-9.25596e+061
Tax $-4.62798e+060
Amount Due $-9.71876e+061
Press any key to continue...

My code:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

// Define Struct

struct menuItemType
{
	string menuItem;
	double menuPrice;
};

void printCheck(int compMenuList[], int counter2);
void getData(int compMenuList[], int counter1);
void showMenu();

int main()
{
	int menuSelection[8];
	int counter = 0;

	showMenu();

	getData(menuSelection, counter);
	
	printCheck(menuSelection, counter);

	return 0;

}

void showMenu()
{
	cout << "Welcome to Johnny's Resturant." << endl;
	cout << endl;
	cout << "(1)Plain Egg" << setw(20) << setfill(' ') << "$1.45" << endl;
	cout << "(2)Bacon and Egg" << setw(20) << setfill(' ') << "$2.45" << endl;
	cout << "(3)Muffin" << setw(20) << setfill(' ') << "$0.99" << endl;
	cout << "(4)French Toast" << setw(20) << setfill(' ') << "$1.99" << endl;
	cout << "(5)Fruit Basket" << setw(20) << setfill(' ') << "$2.49" << endl;
	cout << "(6)Cereal" << setw(20) << setfill(' ') << "$0.69" << endl;
	cout << "(7)Coffee" << setw(20) << setfill(' ') << "$0.50" << endl;
	cout << "(8)Tea" << setw(20) << setfill(' ') << "$0.75" << endl;
	cout << endl;
	cout << "Enter the number corresponding with your choice then press the space bar. Enter -999 to stop." << endl;
	cout << endl;

}

void getData( int compMenuList[], int counter1)
{
	counter1 = 0;

	cout << "Enter the menu choice(s): ";

	for(counter1 = 0; counter1 < 7; counter1++)
	{
		cin >> compMenuList[counter1];

		if(compMenuList[counter1] = -999)
			break;
	}

	

}

void printCheck(int compMenuList[], int counter2)
{
	menuItemType menuList[8];
	int i;
	int j;
	double sum = 0;
	double amountDue;
	double taxAmount;

	for(i = 0; i < counter2; i++)
	{
		switch(compMenuList[i])
		{
		case 1:
			menuList[i].menuItem = "Plain Egg";
			menuList[i].menuPrice = 1.45;
			break;
		case 2:
			menuList[i].menuItem = "Bacon and Egg";
			menuList[i].menuPrice = 2.45;
			break;
		case 3:
			menuList[i].menuItem = "Muffin";
			menuList[i].menuPrice = 0.99;
			break;
		case 4:
			menuList[i].menuItem = "French Toast";
			menuList[i].menuPrice = 1.99;
			break;
		case 5:
			menuList[i].menuItem = "Fruit Basket";
			menuList[i].menuPrice = 2.49;
			break;
		case 6:
			menuList[i].menuItem = "Cereal";
			menuList[i].menuPrice = 0.69;
			break;
		case 7:
			menuList[i].menuItem = "Coffee";
			menuList[i].menuPrice = 0.50;
			break;
		case 8:
			menuList[i].menuItem = "Tea";
			menuList[i].menuPrice = 0.75;
			break;
		default:
			break;
		}
	}

	
	cout << "Welcome to Johnny's Resturant" << endl;
	cout << endl;

	for(j = 0; j <= i; j++)
	{
		cout << menuList[j].menuItem << setw(20) << setfill(' ') << "$" << menuList[j].menuPrice << endl;

		sum = sum + menuList[j].menuPrice;
	}

	taxAmount = sum * 0.05;
	amountDue = (sum * 0.05) + sum;

	cout << "Tax" << setw(20) << setfill(' ') << "$" << taxAmount << endl;
	cout << "Amount Due" << setw(20) << setfill(' ') << "$" << amountDue << endl;
}

Your counter values do not exist outside of the loop scope
e.g.

void getData( int compMenuList[], int counter1)
{
	counter1 = 0;

	cout << "Enter the menu choice(s): ";
for(counter1 = 0; counter1 < 7; counter1++)
	{
		cin >> compMenuList[counter1];

		if(compMenuList[counter1] = -999)
			break;
	}
}

Firstly, counter1 should be passed in by reference void getData( int compMenuList[], int & counter1) since you use it in your other function. But even if you take it in, you use it in a loop as a local loop variable and it's out of scope by the time you would be returning the function. You then tried to use this in PrintCheck and your loops were not being run since your final loop condition was zero. That way you ended up with whatever junk was in memory at the address of the structs. So the solution is to increment it as a counter (counter1++) within the body of the for loop so the value is preserved. Then change the for loop in the Print function to i<counter2 and j<counter2 (not <= as you had it)

Also, I don't think cin is getting all your numbers in a row like that. I'm trying to reformulate it with a while loop.

Thank you so much for your advice. It made me think of something. Now I have it working great! Thank you so much for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.