First of all, thank you for the great information on this site, I've referred to it regularly during my intro to C++ course and have found all the answers I needed...until now.
My problem is the program crashes when an invalid food item is entered. Since this is my first attempt at modularizing a program I think the problem may lie there, but not sure.
Here is an excerpt from the book we are using that explains what is needed.
LAB 6-3: PARALLEL ARRAYS
In this lab, you use what you have learned about parallel arrays to complete a partially completed
C++ program. The program is described in Chapter 6, Exercise 6 in Programming Logic
and Design, Fifth Edition. The program should either print the name and price for a fast-food
item from the Billy Goat Fast Food Restaurant or it should print the message “Sorry, we do
not carry that.”
Read the problem description carefully before you begin. The data file provided for this lab
includes the necessary variable declarations and input statements. You need to write the part
of the program that searches for the name of the food item and either prints its name and
price or prints the error message if the item is not found. Comments in the code tell you
where to write your statements. You can use the expanded mail-order program shown in
Figure 6-6 as a guide.
1. Open the source code file named BillyGoat.cpp using Notepad or the text editor
of your choice.
2. Study the prewritten code to make sure you understand it.
3. Write the code that searches the array for the name of the food item ordered by the
customer.
4. Write the code that prints the name and price of the food item or the error message.
5. Save this source code file in a directory of your choice and then make that directory your
working directory.
6. Compile the source code file BillyGoat.cpp.
7. Execute the program using the following data and record the output:
Customer Number Item Ordered
123 Fries
234 Pepsi
345 Coffee
456 Chips
567 Rings
678 Coke
789 Hamburger
I'm running Dev C++ v4.9.9.2 that I downloaded from bloodshed.net site on a Windows 7 machine. Here's what I have so far.
// Lab11BillyGoat.cpp - This program looks up and prints the names and prices of fast-food items.
// Input: Interactive
// Output: Name and price of fast-food item or error message if fast-food item is not found
#include <iostream>
#include <string>
usingnamespace std;
// Global declaration of variables
string itemName; // Name of food item
int custNum; // Customer number
constint NUM_ITEMS = 4; // Named constant
string foodItems[] = {"Cheeseburger", "Pepsi", "Chips"}; // Initialized array of food items
const string ERROR_MSG = "Sorry, we do not carry that.";
double foodPrices[] = {2.49, 1.00, .59}; // Initialized array of food prices
bool foundIt = false; // Flag variable
int x; // Loop control variable
int quit = 0;
int getReady();
int findItem();
string finishUp();
double price;
int main()
{
cout << "Welcome to the Billy Goat Cafe.\n";
getReady(); // Get user input
while(custNum != 0)
{
findItem();
}
system("pause");
return 0;
} // End of main()
int getReady()
{
cout << "\nEnter customer number between 1 and 99999 or 0 to exit: ";
cin >> custNum;
while(custNum < 0 || custNum > 99999) //validate entry
{
cout << "That is an invalid number.\n";
cout << "Please enter a customer number from 1 to 99999 or 0 to exit: ";
cin >> custNum;
}
return(custNum);
} // End of getReady() module
int findItem()
{
foundIt = false; //initilizing flag
x = 0;
cout << "Enter item name: ";
cin >> itemName;
while(x < NUM_ITEMS && foundIt == false)
{
if(itemName == foodItems[x])
{
foundIt = true; // Setting flag
price = foodPrices[x];
}
x += 1; // Increment the subscript value
}
if(foundIt = true)
{
cout << "The price of " << itemName << " is $" << price;
}
else
{
cout << ERROR_MSG;
}
cout << "\nEnter the next customer number between 1 and 99999 or 0 to exit: ";
cin >> custNum;
return(custNum);
} // End of findItem() module
string finishUp()
{
cout << "Thank you for visiting Billy Goat Cafe,\n";
cout << "Please come again.\n";
} // End of finishUp() module
The error message when an invalid item is put in:
Lab12BillyGoatModular.exe has stopped working
Windows can check online for a solution to the problem.
Check online for a solution and close the program
Close the program
Problem signature:
Problem Event Name: APPCRASH
Application Name: Lab12BillyGoatModular.exe
Application Version: 0.0.0.0
Application Timestamp: 5293bde3
Fault Module Name: Lab12BillyGoatModular.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 5293bde3
Exception Code: c0000005
Exception Offset: 000145e8
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
constint NUM_ITEMS = 4; // Named constant
string foodItems[] = {"Cheeseburger", "Pepsi", "Chips"}; // Initialized array of food items
You have NUM_ITEMS set to 4, but foodItems contains only 3 elements.
Besides that, you're also missing a second = on line 70: if(foundIt = true)
Also, your finishUp function doesn't return anything, even though it says it's supposed to return a string.
Also, the use of global variables is typically frowned upon. You really don't need all of your variables to be global variables. (Declare them when you need them (inside the function), and if more than one function needs a variable, pass it as a parameter to that function.)
Does it crash? It won't even compile. finishUp does not return a value of type string.
Why findItem returns custNum? Don't give different functions common tasks. The point of modularizing is to separate modules' responsibilites, not to mingle them even more. findItem should return item, don't you think?
if(foundIt = true) here is a potential bug - == vs =.
Other than that - the program does not crash. If exaclty what you posted compiles and runs for you - change your IDE, because it allows to do some tricks with custom extensions that are not part of C++ and will in turn teach you bad habbits that result in unexpected crashes ;)
I'm running Dev C++ v4.9.9.2 that I downloaded from bloodshed.net site
Unfortunately that version was last updated in 2005 and is considered obsolete.
Please consider urgently upgrading to the Orwell version which is the maintained up to date version. http://orwelldevcpp.blogspot.co.uk/
long double main, I made the changes as you mentioned, all works great. Seems so easy but I couldn't see the problems after staring at the screen for hours.
Chervil & JockX, I will definitely get the Orwell version.