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.
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.