Need help fine tuning multiple errors.

Hey guys. new here, trying to debug this code and i keep getting this

1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------
1> project2.cpp
1>\ error C2601: 'secondProgram' : local function definitions are illegal
1> project2.cpp(5): this line contains a '{' which has not yet been matched
1>\ project2.cpp(203): fatal error C1075: end of file found before the left brace '{' at project2.cpp(51)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
 #include <iostream> 
using namespace std; 

void firstProgram() 
{
int choice;
int time;
double amountOfDistance;

const double CarbonDioxide = 258; //meters per second
const double Air = 331.5; //meters per second
const double helium = 972; //meters per second
const double hydrogen = 1270; //meters per second
cout << "\t\tThe Speed of Sound in Gas \n";
cout << "Make a Selection \n\n";
cout << "1. CarbonDioxide \n";
cout << "2. Air \n";
cout << "3. helium \n";
cout << "4. hydrogen \n";
cout << "Select the Desired Number and press Enter: ";
cin >> choice;
cout << "Now enter the time(in seconds) it took for sound to travel: ";
cin >> time;


if (time == 0)
{
cout << " NEIN! Value must be greater than ZERO!\n ";
}
else if (choice >= 1 && choice <=4)
{
switch (choice)
{
case 1: amountOfDistance = time * CarbonDioxide;
break;
case 2: amountOfDistance = time * Air;
break;
case 3: amountOfDistance = time * helium;
break;
case 4: amountOfDistance = time * hydrogen;

}

cout << "The distance traveled is ";
cout << amountOfDistance;
cout << " meters" << endl;
return; 
} 

void secondProgram() 
{
int choice;
int distance;
double amountOfTime;

const double Oxygen = 1100; //feet per second
const double Water = 4900; //feet per second
const double Steel = 16400; //feet per second
cout << "\t\tThe Speed of Sound in a Medium \n";
cout << "Select a Medium \n\n";
cout << "1. Air \n";
cout << "2. Water \n";
cout << "3. Steel \n";
cout << "Select a medium and press Return: ";
cin >> choice;
cout << "Now enter the distance (feet) you want the sound wave to travel, then press Return: ";
cin >> distance;


if (distance == 0)
{
cout << " NEIN! You must enter a value greater then zero! \n ";
}
else if (choice >= 1 && choice <=3)
{
switch (choice)
{
case 1: amountOfTime = distance / Oxygen;
break;
case 2: amountOfTime = distance / Water;
break;
case 3: amountOfTime = distance / Steel;
}

cout << "The time is takes for sound to travel in your selection is: ";
cout << amountOfTime;
cout << " seconds" << endl;
return; 
} 

void thirdProgram() 
{

int x1,y1,x2,y2,rec1,rec2;

cout << "Enter the length of the first rectangle (in.):";
cin >> x1;
cout << "Enter the width of the first rectangle (in.):";
cin >> y1;
cout << "Enter the length of the second rectangle (in.):";
cin >> x2;
cout << "Enter the width of the second rectangle (in.):";
cin >> y2;

rec1 = x1*y1;
rec2 = x2*y2;

if (rec1>rec2) cout << "The first rectangle is larger than the second ";
if (rec1<rec2) cout << "The second rectangle is larger than the first ";
if (rec1==rec2) cout << "Both rectangles are equal in size ";
return; 
} 

void fourthProgram() 

int getNumberEmployees(); 
int getNumberMissedDays(); 
double computeMean(int nbEmployees, int nbMissedDays); 

double computeMean(int nbEmployees, int nbMissedDays) 
{ 
return ((double)nbMissedDays / (double)nbEmployees ); 
} 

int getNumberEmployees(){ 
int i; 
cout << "Please enter the number of employees: "; 
cin >> i; 
return i; 
} 

int getNumberMissedDays(){ 
int i; 
cout << "Please enter the number of missed days: "; 
cin >> i; 
return i; 
} 

int main() 
{ 

int nbEmp = getNumberEmployees(); 
int nbDays = getNumberMissedDays(); 
double mean = computeMean(nbEmp , nbDays ); 

std::cout << "The average number of missed days is " << mean << std::endl;

return;
}
 


int main () { 
int choice; 

cout << "Lets make a choice.\n\n1: Approximate speed of sound in air, water, and steel.\n2: Approximate speed of sound at 0 degrees centigrade when traveling through carbon dioxide, air, helium, and hydrogen.\n3: Area of a rectangle. \n4: Average number of days a company’s employees are absent\n\nI want: "; 
cin >> choice; 

switch (choice) { 
case 1: 
firstProgram(); 
break; 
case 2: 
secondProgram(); 
break; 
case 3: 
thirdProgram(); 
break; 
case 4: 
fourthProgram(); 
break; 
default: 
cout << "\n\nLOL! wut.\n"; 
break; 
} 

return 0; 
}
Well the way you formatted it here is making it a little hard to figure out, but several of the issues is you forgot { or } during some of your functions. Slowly trying to fix it to post, but having to reformat it so I can read the code and figure out what is what.

You also can't have two int main() function calls.

Compare my code to yours (just missed the closing '}' on the two else statements surrounding the two switches in the function, one too many main functions, and a few other minor fixes):
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 #include <iostream> 
using namespace std; 

void firstProgram() 
{
	int choice;
	int time;
	double amountOfDistance;

	const double CarbonDioxide = 258; //meters per second
	const double Air = 331.5; //meters per second
	const double helium = 972; //meters per second
	const double hydrogen = 1270; //meters per second
	cout << "\t\tThe Speed of Sound in Gas \n";
	cout << "Make a Selection \n\n";
	cout << "1. CarbonDioxide \n";
	cout << "2. Air \n";
	cout << "3. helium \n";
	cout << "4. hydrogen \n";
	cout << "Select the Desired Number and press Enter: ";
	cin >> choice;
	cout << "Now enter the time(in seconds) it took for sound to travel: ";
	cin >> time;


	if (time == 0)
	{
		cout << " NEIN! Value must be greater than ZERO!\n ";
	}
	else if (choice >= 1 && choice <=4)
	{
		switch (choice)
		{
			case 1: amountOfDistance = time * CarbonDioxide;
			break;
			case 2: amountOfDistance = time * Air;
			break;
			case 3: amountOfDistance = time * helium;
			break;
			case 4: amountOfDistance = time * hydrogen;
		}
	}
	cout << "The distance traveled is ";
	cout << amountOfDistance;
	cout << " meters" << endl;
	return; 
} 

void secondProgram() 
{
	int choice;
	int distance;
	double amountOfTime;

	const double Oxygen = 1100; //feet per second
	const double Water = 4900; //feet per second
	const double Steel = 16400; //feet per second
	cout << "\t\tThe Speed of Sound in a Medium \n";
	cout << "Select a Medium \n\n";
	cout << "1. Air \n";
	cout << "2. Water \n";
	cout << "3. Steel \n";
	cout << "Select a medium and press Return: ";
	cin >> choice;
	cout << "Now enter the distance (feet) you want the sound wave to travel, then press Return: ";
	cin >> distance;


	if (distance == 0)
	{
	cout << " NEIN! You must enter a value greater then zero! \n ";
	}
	else if (choice >= 1 && choice <=3)
	{
		switch (choice)
		{
			case 1: amountOfTime = distance / Oxygen;
			break;
			case 2: amountOfTime = distance / Water;
			break;
			case 3: amountOfTime = distance / Steel;
		}
	}

	cout << "The time is takes for sound to travel in your selection is: ";
	cout << amountOfTime;
	cout << " seconds" << endl;
	return; 
} 

void thirdProgram() 
{

	int x1,y1,x2,y2,rec1,rec2;

	cout << "Enter the length of the first rectangle (in.):";
	cin >> x1;
	cout << "Enter the width of the first rectangle (in.):";
	cin >> y1;
	cout << "Enter the length of the second rectangle (in.):";
	cin >> x2;
	cout << "Enter the width of the second rectangle (in.):";
	cin >> y2;

	rec1 = x1*y1;
	rec2 = x2*y2;

	if (rec1>rec2) cout << "The first rectangle is larger than the second ";
	if (rec1<rec2) cout << "The second rectangle is larger than the first ";
	if (rec1==rec2) cout << "Both rectangles are equal in size ";
	return; 
} 

double computeMean(int nbEmployees, int nbMissedDays) 
{ 
	return ((double)nbMissedDays / (double)nbEmployees ); 
} 

int getNumberEmployees(){ 
	int i; 
	cout << "Please enter the number of employees: "; 
	cin >> i; 
	return i; 
} 

int getNumberMissedDays(){ 
	int i; 
	cout << "Please enter the number of missed days: "; 
	cin >> i; 
	return i; 
} 

void fourthProgram() 
{
	int nbEmp = getNumberEmployees(); 
	int nbDays = getNumberMissedDays(); 
	double mean = computeMean(nbEmp , nbDays ); 

	std::cout << "The average number of missed days is " << mean << std::endl;
}

int main () { 

	int choice; 

	cout << "Lets make a choice.\n\n"
                << "1: Approximate speed of sound in air, water, and steel.\n"
                << "2: Approximate speed of sound at 0 degrees centigrade when traveling through carbon dioxide, air, helium, and hydrogen.\n"
                << "3: Area of a rectangle. \n"
                << "4: Average number of days a company’s employees are absent\n\nI want: "; 
	cin >> choice; 

		switch (choice) { 
		case 1: 
		firstProgram(); 
		break; 
		case 2: 
		secondProgram(); 
		break; 
		case 3: 
		thirdProgram(); 
		break; 
		case 4: 
		fourthProgram(); 
		break; 
		default: 
		cout << "\n\nLOL! wut.\n"; 
		break; 
	} 

	return 0; 
}
Last edited on by closed account z6A9GNh0
Indentation helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> 
using namespace std; 

void firstProgram() 
{
  if (time == 0)
    {
    }
  else if (choice >= 1 && choice <=4)
    {
      switch (choice)
        {
        }
    } 

  void secondProgram() 
  {
  }

Count your braces, for one is missing.
weird. on my screen everything is lined up and indented.
I went back and fixed it and posted the fixed working code in my above post. Simply it was some misplaced functions, an extra main() that shouldn't have been there, and you missed the } in two of the else{switch(){}} parts of your function.
Last edited on by closed account z6A9GNh0
sweet. thanks a bunch
Last edited on
Topic archived. No new replies allowed.