Won't compile until I move declaration?

I jumped into C++ to make custom .dll addons for a space flight sim called Orbiter.(http://orbit.medphys.ucl.ac.uk/) Orbiter uses .dll files to create, among other things, different space vessels.

The visual representation of a given vessel is represented by a .msh file, which can be pretty clumsy and difficult to create and edit.

Before I ventured into the realm of the .dll, I decided to put my new coding skills to an easier test. I am trying to create a console application that you can use to design and create .msh files.

It isn't much to show right now, but I do have one question that presented itself while coding this program. The code: (cin >> a at the end is just so I can see the menu before closing)

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
//||===========================||
//||Mesh creation utility (MCU)||
//||===========================||
#include <iostream>
using namespace std;
//vectors (also used as points), lines, planes;
struct vector {
	float x;
	float y;
	float z;
};
struct line {
	vector a;
	vector b;
};
struct plane {
	vector a;
	vector b;
	vector c;
};
//Main function
int main () {
	int menu;
	cout << "Welcome to my Orbiter .msh creator!";
	do {
	cout << endl << endl << "1 - Create a new point\n2 - Create a line\n3 - Create a material\n--> ";
	cin >> menu;
		cout << "\nYou selected ";
		switch (menu) {
		case 1:
			cout << "to create a point. ";
			break;
		case 2:
			cout << "to create a line. ";
			break;
		case 3:
			cout << "to create a material. ";
			break;
		default:
			cout << " an invalid program! ";
			break;
					}
		cout << "Correct entry?\n\n1 - Yes, correct\n0 - No, retry\n--> ";
		cin >> menu;}
		while (menu!=1);
	int a;
	cin >> a;
	return 0;
}


The above code compiles correctly. My original code (below) didn't compile:

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
//||===========================||
//||Mesh creation utility (MCU)||
//||===========================||
#include <iostream>
using namespace std;
//vectors (also used as points), lines, planes;
struct vector {
	float x;
	float y;
	float z;
};
struct line {
	vector a;
	vector b;
};
struct plane {
	vector a;
	vector b;
	vector c;
};
//Main function
int main () {
	cout << "Welcome to my Orbiter .msh creator!";
	do {
	cout << endl << endl << "1 - Create a new point\n2 - Create a line\n3 - Create a material\n--> ";
	int menu;
	cin >> menu;
		cout << "\nYou selected ";
		switch (menu) {
		case 1:
			cout << "to create a point. ";
			break;
		case 2:
			cout << "to create a line. ";
			break;
		case 3:
			cout << "to create a material. ";
			break;
		default:
			cout << " an invalid program! ";
			break;
					}
		cout << "Correct entry?\n\n1 - Yes, correct\n0 - No, retry\n--> ";
		cin >> menu;}
		while (menu!=1);
	int a;
	cin >> a;
	return 0;
}

The only change from the working code is that the integer menu is declared 3 lines later, immediately before first being entered. The compilation error is this:

1
2
3
1
1>  meshmaker.cpp
1>c:\users\jill\documents\visual studio 2010\projects\mesh maker\meshmaker.cpp(45): error C2065: 'menu' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


My question is this: Why is int menu not regognized in the second code? I am completely drawing a blank. I ask because I don't want to make the error again.

TL;DR? Just read the first few lines of the main functions.

Thanks,

Apoapsis
Last edited on
It's because you are declaring it inside of the do while loop, so therefore it can't be used in the condition.
Topic archived. No new replies allowed.