errors

I keep getting these two errors-
-40: error: a function-definition is not allowed here before '{' token.
-65: error: expected '}' at the end of input.

this is my code:

#include <iostream>

using namespace std;

int main()
{
int choice;
int getNumAccidents();
int accidents [5]; //Total in the array
int n = 0;
int count;
int findLowest();
// Display menu for choices
cout << "Select your choice from the menu: " << endl;
cout << "1. Safest Driving Area" << endl;
cout << "2. Lowest Score Drop" << endl;
cout << "3. Quit" << endl;
cout << "Which one would you like to do? : ";
cin >> choice; //Gets users choice
//Starts Safest Driving Area
if (choice == '1')
{
cout << "How many accidents were there in the North, South, East, West, and Central part of the city?";
for (count = 1; count <= 5; count++) // Loop for the users input
{
cout << count << ".\t";
cin >> accidents [n];
while (accidents [n] < 0)//Validates that # of accident is not lower than 0
{
cout << "Please enter a number greater than 0: ";
cin >> accidents [n];
}
cout << endl;
n++;
}

}
//Finds the least number of accidents
void findLowest()
{
int findLowest();
int min = accidents [0];
for (int i = 1; i < 5; i++) // Loop to find least # of accidents
{
if (min > accidents [i])
min = accidents [i];
}
if (min = accidents [0])
{
cout << "The North side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [1])
cout << "The South side of the city has the safest driving area with " << min << " accidnets\n";
else if (min = accidents [2])
cout << "The East side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [3])
cout << "The West side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [4])
cout << "The Central part of the city has the safest driving area with " << min << " accidents\n";
}
}
}

}
return 0;
}

Hello. Please update your code and use code tags - http://www.cplusplus.com/articles/jEywvCM9/

A lot of errors in your code.

1
2
3
} 
return 0;
}


This does nothing, remove it.

Also remove the } above it. Hard to explain without code tags.

1
2
3
4
5
6
7
8
9
10
11
12
if (min = accidents [0])
{
cout << "The North side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [1])
cout << "The South side of the city has the safest driving area with " << min << " accidnets\n";
else if (min = accidents [2])
cout << "The East side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [3])
cout << "The West side of the city has the safest driving area with " << min << " accidents\n";
else if (min = accidents [4])
cout << "The Central part of the city has the safest driving area with " << min << " accidents\n";
}


You can't have a bunch of else if statements without a starting if statement.

int min = accidents[0]; you have this in a function, not in main. So the array you created in main is not recognized here. You have to send it over via the parameters.

Also, add a } at the end of main.
These are the syntax errors I get:

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
#include <iostream>

using namespace std;

int main()
{
  int choice;
  int getNumAccidents();
  int accidents [5]; //Total in the array
  int n = 0;
  int count;
  int findLowest();
  // Display menu for choices
  cout << "Select your choice from the menu: " << endl;
  cout << "1. Safest Driving Area" << endl;
  cout << "2. Lowest Score Drop" << endl;
  cout << "3. Quit" << endl;
  cout << "Which one would you like to do? : ";
  cin >> choice; //Gets users choice
  //Starts Safest Driving Area
  if (choice == '1')
  {
    cout << "How many accidents were there in the North, South, East, West, and Central part of the city?";
    for (count = 1; count <= 5; count++) // Loop for the users input
    {
      cout << count << ".\t";
      cin >> accidents [n];
      while (accidents [n] < 0)//Validates that # of accident is not lower than 0
      {
        cout << "Please enter a number greater than 0: ";
        cin >> accidents [n];
      }
      cout << endl;
      n++;
    }

  }
  //Finds the least number of accidents
  void findLowest()
  {
    int findLowest();
    int min = accidents [0];
    for (int i = 1; i < 5; i++) // Loop to find least # of accidents
    {
      if (min > accidents [i])
        min = accidents [i];
    }
    if (min = accidents [0])
    {
      cout << "The North side of the city has the safest driving area with " << min << " accidents\n";
    else if (min = accidents [1])
      cout << "The South side of the city has the safest driving area with " << min << " accidnets\n";
    else if (min = accidents [2])
      cout << "The East side of the city has the safest driving area with " << min << " accidents\n";
    else if (min = accidents [3])
      cout << "The West side of the city has the safest driving area with " << min << " accidents\n";
    else if (min = accidents [4])
      cout << "The Central part of the city has the safest driving area with " << min << " accidents\n";
    }
  }
}

}
return 0;
}



main.cpp(40): error C2556: 'void findLowest(void)' : overloaded function differs only by return type from 'int findLowest(void)'
main.cpp(12) : see declaration of 'findLowest'
main.cpp(40): error C2371: 'findLowest' : redefinition; different basic types
main.cpp(12) : see declaration of 'findLowest'
main.cpp(40): error C2333: 'findLowest' : error in function declaration; skipping function body
main.cpp(64): error C2059: syntax error : 'return'
main.cpp(65): error C2059: syntax error : '}'
main.cpp(65): error C2143: syntax error : missing ';' before '}'
main.cpp(65): error C2059: syntax error : '}'


Some logic error:
 
if (min = accidents [0])


I guess it should be
if (min == accidents [0])

BTW. It's usually a good practice to write and test your code in small chunks - also to compile frequently.
Topic archived. No new replies allowed.