EDIT: New problems, update post added at the bottom.
I'm new at this site. Name's "TheFool", because I am the fool when it comes to programming stuff like this. I'm in a C++ class, so I may show up here a lot. I'm using Microsoft Visual C++ 2008.
Anyway, I'm writing a program that calculates the bill for customers of a certain business. The assignment requires us to use named constants for a lot of things. At the bottom of my program, where I'm planning to have calculations done, I've got the following two lines:
const double mealCosts = (beefMeal * beefMealAmount) + (chickenMeal * chickenMealAmount) + (veganMeal * veganMealAmount); // Total costs of all meals.
const double gratuity = mealCosts / 100 * 18; // Gratuity for amount spent on meals.
And yet, whenever I hit "compile", the program tells me "mealCosts" is an "undeclared identifier". When I double click this error, it takes me to the second line out of the two above.
I don't get it. How is it undeclared? How can I fix this? Any and all help would be appreciated.
EDIT: Below is the entire program I've completed so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// Assignment 2. This program will calculate the total bill for customers of the **** company.
// Naming values and constants.
const double beefMeal = 12.95; // Cost of a single beef meal.
const double chickenMeal = 10.95; // Cost of a single chicken meal.
const double veganMeal = 8.95; // Cost of a single vegan meal.
double beefMealAmount; // Amount of beef meals the customer orders.
double chickenMealAmount; // Amount of chicken meals the customer orders.
double veganMealAmount; // Amount of vegan meals the customer orders.
const int room1 = 250; // Cost of Room 1, which seats 200 people.
const int room2 = 150; // Cost of Room 2, which seats 150 people.
const int room3 = 100; // Cost of Room 3, which seats 100 people.
const int room4 = 50; // Cost of room 4, which seats 30 people.
In lines 58-78 in your if else chain each of the for ROOMCOST variables you define are different variables, each one existing only in its own scope which is the enclosing braces. For example, the ROOMCOST on line 65 goes out of scope at the end of line 66 and hence ceases to exist. The same is true for the other three ROOMCOSTs. Consequently, when you reach the cout statement on line 83 no such variable exists. You need to declare ROOMCOST right before line 58:
int ROOMCOST = 0;
Now you can set the actual value in your if else chain with a simple assignment:
Thank you. Your method seems to work. Except I run into a snag...
The assignment requires that the room costs are constant. The method you showed me doesn't appear to work for constants. Any idea how I can make this work?
Yeah, I was originally planning to remove the ROOM1, ROOM2, etc. values once I figured out how to fix that snag I ran into. But it turns out my fix for the snag involved keeping those values after all.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main ()
{
// This program will calculate the total bill for customers of the XXXX Company.
// Naming values and constants.
constdouble BEEF_MEAL = 12.95; // Cost of a single beef meal.
constdouble CHICKEN_MEAL = 10.95; // Cost of a single chicken meal.
constdouble VEGAN_MEAL = 8.95; // Cost of a single vegan meal.
int beefMealAmount; // Amount of beef meals the customer orders.
int chickenMealAmount; // Amount of chicken meals the customer orders.
int veganMealAmount; // Amount of vegan meals the customer orders.
constint ROOM1 = 250; // Cost of Room 1, which seats 200 people.
constint ROOM2 = 200; // Cost of Room 2, which seats 150 people.
constint ROOM3 = 100; // Cost of Room 3, which seats 100 people.
constint ROOM4 = 50; // Cost of room 4, which seats 30 people.
// Input from customer.
cout << "Enter number of beef meals: ";
cin >> beefMealAmount;
if(beefMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
elseif(beefMealAmount >= 0)
{
cout << "Enter number of chicken meals: ";
cin >> chickenMealAmount;
}
if(chickenMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
elseif(chickenMealAmount >= 0)
{
cout << "Enter number of vegan meals: ";
cin >> veganMealAmount;
}
if(veganMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
elseif(veganMealAmount >= 0)
{
}
// Bill calculations.
cout.setf (ios::fixed | ios::showpoint);
cout.precision (2);
int totalPeople = (beefMealAmount + chickenMealAmount + veganMealAmount); // Total number of guests.
constdouble MEAL_COSTS = (BEEF_MEAL * beefMealAmount) + (CHICKEN_MEAL * chickenMealAmount) + (VEGAN_MEAL * veganMealAmount); // Total costs of all meals.
constdouble GRATUITY = MEAL_COSTS / 100 * 18; // Gratuity for amount spent on meals.
// Below are the calculations for room cost.
if(totalPeople > 200)
{
cout << "There is no room large enough. ";
return 1;
}
int ROOMCOST = 0;
if(totalPeople >= 1 && totalPeople <= 30)
{
ROOMCOST = ROOM4;
}
elseif(totalPeople >= 31 && totalPeople <= 100)
{
ROOMCOST = ROOM3;
}
elseif(totalPeople >= 101 && totalPeople <= 150)
{
ROOMCOST = ROOM2;
}
elseif(totalPeople >= 151 && totalPeople <= 200)
{
ROOMCOST = ROOM1;
}
constdouble STATE_TAX = ROOMCOST / 100 * 6.5;
// Section for everything the program will display.
cout.setf (ios::fixed | ios::showpoint);
cout.precision (2);
cout << " " << endl;
cout << "Caswell Caterers by Jesse Sails" << endl;
cout << " " << endl;
cout << setw (40) << left << "Number in party:" << totalPeople << endl;
cout << setw (60) << left << "Room Cost:" << "$" << ROOMCOST << endl;
cout << setw (60) << left << "Room Tax:" << "$" << STATE_TAX << endl;
if(beefMealAmount > 0)
{
cout << setw (40) << left << "Number of beef dinners:" << beefMealAmount << endl;
cout << setw (60) << left << "Cost of beef dinners:" << "$" << BEEF_MEAL * beefMealAmount << endl;
}
if(chickenMealAmount > 0)
{
cout << setw (40) << left << "Number of chicken dinners:" << chickenMealAmount << endl;
cout << setw (60) << left << "Cost of chicken dinners:" << "$" << CHICKEN_MEAL * chickenMealAmount << endl;
}
if(veganMealAmount > 0)
{
cout << setw (40) << left << "Number of vegan dinners:" << veganMealAmount << endl;
cout << setw (60) << left << "Cost of vegan dinners:" << "$" << VEGAN_MEAL * veganMealAmount << endl;
}
cout << setw (60) << left << "Food Gratuity:" << "$" << GRATUITY << endl;
cout << "---------------------------------------------------------------------------" << endl;
cout << setw (60) << left << "Total:" << "$" << MEAL_COSTS + ROOMCOST + STATE_TAX + GRATUITY << endl;
cout << " " << endl;
return 0;
}
As you can see, I simply made ROOMCOST not a constant, and kept the individual room values as constants.
Y'see, the professor didn't assign us specific code pieces. The assignment merely states that I need to keep room costs, room tax, meal costs, and meal tax as named constants. My fix might technically go against that requirement. But it's the best I could come up with. And considering how good my output looks in comparison to the sample images, I think I'll still get most of the points for this one.