Getting "Undeclared Identifier" for something I declared

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.

// Input from customer.
cout << "Enter number of beef meals: ";
cin >> beefMealAmount;
if(beefMealAmount < 0)
cout << "Invalid data. ";
return 1;
else if(beefMealAmount > 0)
cout << "Enter number of chicken meals: ";
cin >> chickenMealAmount;
if(chickenMealAmount < 0)
cout << "Invalid data. ";
return 1;
else if(chickenMealAmount > 0)
cout << "Enter number of vegan meals: ";
cin >> veganMealAmount;
if(veganMealAmount < 0)
cout << "Invalid data. ";
return 1;
else if(veganMealAmount > 0)

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.

return 0;

I've also got a few "illegal else without matching if" errors. The program isn't done yet, primarily because I've run into this snag.
Last edited on
Both of your problems are caused by you failing to put braces around the bodies of your if blocks.

If you do not use braces, the only thing considered part of the if block is the code up to the next semicolon. For example:

1
2
3
if(beefMealAmount > 0)
    cout << "Invalid data. ";  // this is part of the if block
return 1;  // this isn't 


What you want is this:

1
2
3
4
5
if(beefMealAmount > 0)
{  // <- use braces
    cout << "Invalid data. ";
    return 1;  // now both of these are part of the if block
} // <- if block ends here 
Thanks. I added brackets like this:

// Input from customer.
cout << "Enter number of beef meals: ";
cin >> beefMealAmount;
if(beefMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(beefMealAmount > 0)
cout << "Enter number of chicken meals: ";
cin >> chickenMealAmount;
if(chickenMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(chickenMealAmount > 0)
cout << "Enter number of vegan meals: ";
cin >> veganMealAmount;
if(veganMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(veganMealAmount > 0)


I'm still getting "undeclared identifier" for mealCosts though. But the other errors disappeared.
You missed some if blocks. I see 3 that you missed in your 2nd post.
Last edited on
Alright! It's working now!

This is what I did:

{

// Input from customer.
cout << "Enter number of beef meals: ";
cin >> beefMealAmount;
if(beefMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(beefMealAmount > 0)
{
cout << "Enter number of chicken meals: ";
cin >> chickenMealAmount;
}
if(chickenMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(chickenMealAmount > 0)
{
cout << "Enter number of vegan meals: ";
cin >> veganMealAmount;
}
if(veganMealAmount < 0)
{
cout << "Invalid data. ";
return 1;
}
else if(veganMealAmount > 0)
{
}

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 now I'm not getting any errors. I'll update after I've completed the calculation section.
Last edited on
Okay, update. I'm getting "undeclared identifier" for something I declared, yet again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	//  This program will calculate the total bill for customers of the **** Company.

	// Naming values and constants.
	const double BEEF_MEAL = 12.95; // Cost of a single beef meal.
	const double CHICKEN_MEAL = 10.95; // Cost of a single chicken meal.
	const double VEGAN_MEAL = 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.

	// Input from customer.
	cout << "Enter number of beef meals: ";
	cin >> beefMealAmount;
	if(beefMealAmount < 0)
	{
		cout << "Invalid data. ";
		return 1;
	}
	else if(beefMealAmount > 0)
	{
	cout << "Enter number of chicken meals: ";
	cin >> chickenMealAmount;
	}
	if(chickenMealAmount < 0)
	{
	cout << "Invalid data. ";
		return 1;
	}
	else if(chickenMealAmount > 0)
	{
	cout << "Enter number of vegan meals: ";
	cin >> veganMealAmount;
	}
	if(veganMealAmount < 0)
	{
		cout << "Invalid data. ";
		return 1;
	}
	else if(veganMealAmount > 0)
	{
	}

	// Bill calculations.
	double totalPeople = (beefMealAmount + chickenMealAmount + veganMealAmount); // Total number of guests.
	const double MEAL_COSTS = (BEEF_MEAL * beefMealAmount) +  (CHICKEN_MEAL * chickenMealAmount) + (VEGAN_MEAL * veganMealAmount); // Total costs of all meals.
	const double 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;
	}
	if(totalPeople >= 1 && totalPeople <= 30)
	{
		const int ROOMCOST = 50;
	}
	else if(totalPeople >= 31 && totalPeople <= 100)
	{
		const int ROOMCOST = 100;
	}
	else if(totalPeople >= 101 && totalPeople <= 150)
	{
		const int ROOMCOST = 150;
	}
	else if(totalPeople >= 151 && totalPeople <= 200)
	{
		const int ROOMCOST = 250;
	}

	// Section for everything the program will display.
	cout << " " << endl;
	cout << "Number in party: " << setw (25) << right << totalPeople << endl;
	cout << "Room Cost: " << setw (50) << right << ROOMCOST << endl;
return 0;
}

I'm getting "undeclared identifier" for "ROOMCOST" on the last line there. Any ideas why?
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:

1
2
3
4
if(totalPeople >= 1 && totalPeople <= 30)
{
	ROOMCOST = 50;
}


Similarly for the other if clauses. Note that the declaration of ROOMCOST cannot be const since is value is being set later.
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?
Last edited on
Is it possible that the named constants for room costs your instructor wanted you to use are the ones on lines 16-19:

1
2
3
4
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. 


Which room to use cannot be known until runtime and hence the cost of the room for a specific customer cannot be constant.

One other note: Your ROOM1, ROOM2, ROOM3 and ROOM4 constants are never used. The place to use them is in assigning to ROOMCOST in your if else chain:

1
2
3
4
if(totalPeople >= 1 && totalPeople <= 30)
{
	ROOMCOST = ROOM4;
}


Similarly for the other ROOMCOST assignments.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	// This program will calculate the total bill for customers of the XXXX Company.

	// Naming values and constants.
	const double BEEF_MEAL = 12.95; // Cost of a single beef meal.
	const double CHICKEN_MEAL = 10.95; // Cost of a single chicken meal.
	const double 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.
	const int ROOM1 = 250; // Cost of Room 1, which seats 200 people.
	const int ROOM2 = 200; // 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.

	// Input from customer.
	cout << "Enter number of beef meals: ";
	cin >> beefMealAmount;
	if(beefMealAmount < 0)
	{
		cout << "Invalid data. ";
		return 1;
	}
	else if(beefMealAmount >= 0)
	{
	cout << "Enter number of chicken meals: ";
	cin >> chickenMealAmount;
	}
	if(chickenMealAmount < 0)
	{
	cout << "Invalid data. ";
		return 1;
	}
	else if(chickenMealAmount >= 0)
	{
	cout << "Enter number of vegan meals: ";
	cin >> veganMealAmount;
	}
	if(veganMealAmount < 0)
	{
		cout << "Invalid data. ";
		return 1;
	}
	else if(veganMealAmount >= 0)
	{
	}

	// Bill calculations.
	cout.setf (ios::fixed | ios::showpoint);
	cout.precision (2);
	int totalPeople = (beefMealAmount + chickenMealAmount + veganMealAmount); // Total number of guests.
	const double MEAL_COSTS = (BEEF_MEAL * beefMealAmount) +  (CHICKEN_MEAL * chickenMealAmount) + (VEGAN_MEAL * veganMealAmount); // Total costs of all meals.
	const double 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;
	}
	else if(totalPeople >= 31 && totalPeople <= 100)
	{
		ROOMCOST = ROOM3;
	}
	else if(totalPeople >= 101 && totalPeople <= 150)
	{
		ROOMCOST = ROOM2;
	}
	else if(totalPeople >= 151 && totalPeople <= 200)
	{
		ROOMCOST = ROOM1;
	}
	const double 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.
Topic archived. No new replies allowed.