code help

So I am getting errors in my code. Can someone help me figure out what I am doing wrong?
this are the errors I am getting:
49: error: a function-definition is not allowed here before the '{' token.
98: error: expected '}' at end of input.
98: error: expected '}' at end of input.

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
#include <iostream>
using namespace std;

int getNumAccidents(string);
void findLowest(int, int, int, int, int);
int main()
{
	//Variables
	int choice;
	
	//Asks user to enter choice
	cout << "Please select on of the choices below: " << endl;
	cout << "1. Safest Driving Area\n";
	cout << "2. Lowest Score Drop\n"; 
	cout << "3. Quit" << endl;
	cin >> choice;
	
	if (choice == 1)
	{
		int north, south, east, west, central;
		for (int i = 0; i < 5; i++)
		{
			switch (i)
			{
			case 0:;
				north = getNumAccidents("North");
				break;
			case 1:;
				south = getNumAccidents("South");
				break;
			case 2:;
				east = getNumAccidents("East");
				break;
			case 3:;
				west = getNumAccidents("West");
				break;
			case 4:;
				central = getNumAccidents("Central");
			}
	{

	findLowest (north, south, east, west, central);

	return 0;
}

int getNumAccidents(string region)
{
	int accidents = 0;
	
	cout << "Please enter the number of accidents for region " << region << " : ";
	cin >> accidents;

	while (accidents = 0)
	{
		cout << " The number of accidnts can not be a negative number. Please re-enter accidents: ";
		cin >> accidents;
	}
	return (accidents);
}

void findLowest(int region1, int region2, int region3, int region4, int region5)
{
	int lowest = 0;
	string region = "";
	
	if (region1 < region2 && region1 < region3 && region1 < region4 && region1 < region5)
	{
		lowest = region1;
		region = "North";
	}
	else if (region2 < region1 && region2 < region3 && region2 < region4 && region2 < region5)
	{
		lowest = region2;
		region = "South";
	}
	else if (region3 < region1 && region3 < region2 && region3 < region4 && region3 < region5)
	{
		lowest = region3;
		region = "East";
	}
	else if (region4 < region1 && region4 < region2 && region4 < region3 && region4 < region5)
	{
		lowest = region4;
		region = "West";
	}
	esle if (region5 < region1 && region5 < region2 && region5 < region3 && region5 < region4)
	{
		lowest = region5;
		region = "Central"
	}
	
	cout << "The region with the lowest accidents is: " << region << ". " << endl;
	cout << "The total number of accidents in that region was: " << lowest << " . " <<endl;
}
Last edited on
Your last 'else if' is misspelled as 'esle' if
yes I corrected it but the same errors keep coming up. Why?
Inside the main function. You have some errors with missing } braces. You need a closing brace (}) after you call the findLowest function. And the brace before that has to be a closing brace also. Not an open brace.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
			switch (i)
			{
			case 0:;
				north = getNumAccidents("North");
				break;
			case 1:;
				south = getNumAccidents("South");
				break;
			case 2:;
				east = getNumAccidents("East");
				break;
			case 3:;
				west = getNumAccidents("West");
				break;
			case 4:;
				central = getNumAccidents("Central");
			}


Case 1:;... there shouldn't be semi-colons after the colons in all of your case statements.
And, in your main routine, where your if (choice == 1) is.. .you open it with { but you try to close it with { as well, that should close with a }. Your for statement has no closing bracket either... basically lots of mistakes. you also are trying to use an std::string but aren't including string... #include <string>

Here is a version that compiles for your viewing pleasure:
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
#include <iostream>
#include <string>
using namespace std;

int getNumAccidents(string);
void findLowest(int, int, int, int, int);
int main()
{
	//Variables
	int choice;

	//Asks user to enter choice
	cout << "Please select on of the choices below: " << endl;
	cout << "1. Safest Driving Area\n";
	cout << "2. Lowest Score Drop\n";
	cout << "3. Quit" << endl;
	cin >> choice;

	if (choice == 1)
	{
		int north, south, east, west, central;
		for (int i = 0; i < 5; i++)
		{
			switch (i)
			{
			case 0:
				north = getNumAccidents("North");
				break;
			case 1:
				south = getNumAccidents("South");
				break;
			case 2:
				east = getNumAccidents("East");
				break;
			case 3:
				west = getNumAccidents("West");
				break;
			case 4:
				central = getNumAccidents("Central");
			}
		}
		findLowest(north, south, east, west, central);
	}

	return 0;
}

int getNumAccidents(string region)
{
	int accidents = 0;

	cout << "Please enter the number of accidents for region " << region << " : ";
	cin >> accidents;

	while (accidents = 0)
	{
		cout << " The number of accidnts can not be a negative number. Please re-enter accidents: ";
		cin >> accidents;
	}
	return (accidents);
}

void findLowest(int region1, int region2, int region3, int region4, int region5)
{
	int lowest = 0;
	string region = "";

	if (region1 < region2 && region1 < region3 && region1 < region4 && region1 < region5)
	{
		lowest = region1;
		region = "North";
	}
	else if (region2 < region1 && region2 < region3 && region2 < region4 && region2 < region5)
	{
		lowest = region2;
		region = "South";
	}
	else if (region3 < region1 && region3 < region2 && region3 < region4 && region3 < region5)
	{
		lowest = region3;
		region = "East";
	}
	else if (region4 < region1 && region4 < region2 && region4 < region3 && region4 < region5)
	{
		lowest = region4;
		region = "West";
	}
	else if (region5 < region1 && region5 < region2 && region5 < region3 && region5 < region4)
	{
		lowest = region5;
		region = "Central";
	}

	cout << "The region with the lowest accidents is: " << region << ". " << endl;
	cout << "The total number of accidents in that region was: " << lowest << " . " << endl;
}


I didn't actually run it, so I can't promise it works like you expect, but it does compile at least!
Last edited on
Hi,

If your IDE doesn't auto-magically do closing braces for you, then get into the practise of typing both opening and closing braces together (or parentheses, angle brackets, whatever comes in pairs) , then go back and fill in what is supposed to be in the braces. That way you will never have mismatched braces.

55
56
57
58
59
60
61
62
while (accidents == 0) // == for comparison, = for assignment
   	{
                // conditional doesn't test for negative, could use unsigned type to help with that
                // otherwise use the <= operator
		cout << " The number of accidnts can not be a negative number. Please re-enter accidents: ";
		cin >> accidents;
	}
	return (accidents);


With the function declarations (before main), it's better to provide variable names for the parameters, and make them the same names as what is in the function definition:

1
2
3
4
5
6
7
8
9
int getNumAccidents(const string& region); // & means pass by reference for anything that might be big, 
                                                         // const means we can't change it inside the function
                                                         // probably advanced ideas for now, but there you go :+)

void findLowest(const int region1, // Ilike this formatting for multiple parameters
                      const int region2, // no references here, they are only ints
                      const int region3, // consider using unsigned type
                      const int region4, 
                      const int region5);


As a defensive measure, it's a good idea to always put a default: case in a switch. It's not needed here, but it might save you one day :+)

Good Luck !!
Topic archived. No new replies allowed.