Error code not in scope

I have been working this program for a while for my class so far it will not compile. The instructor was nice enough to let us take a screen shot but I only got the bottom half. I keep getting error not in scope every time I try to compile. I was wondering if I can get an idea about how to fix it from someone advanced. Thank in advance for your assistance.


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
 #include<iostream> //Header File for console I/O
using namespace std;

void getAccidents( int &, int &,int &,int &,int &) ;
void findLowerst(int, int, int, int, int);

// begin min Function Definition
int main()
{
	int crashN, crashE, crashS, crashC, crashW;
	
	getAccidents(crashN, crashE, crashS, crashC, crashW);
	
	findLowerst(crashN, crashE, crashS, crashC, crashW);
	
	system("pause");
	
	return 0;
} //end of main function.

void getAccidents(int regN, int regE, int regS, int regC, int regW)
{
char area;
int crashes;
cout << "text" << endl;
cin >> area;

switch(area)
{
	case NORTH   : cout << "NORTH";
				   break;
		
	case EAST   : cout << "EAST";
				   break;
	case SOUTH  : cout << "SOUTH";
				   break;
	case CENTRAL   : cout << "CENTRAL";
				   break;
	case WEST   : cout << "WEST";
					
}
	cout << "area";
	cin >> crashes;
	
	while (crashes < 0)
	{
		cout << "teh acciednt total cannot be negative"
		<< "please re-enter";
		cin >> crashes;
	}
	return crashes;
}

void findLowerst(int N, int E, int S ,int C ,int W )
{
	int lowest = N;
	
	if ( E < lowest) lowest = E
	if ( S < lowest) lowest = S
	if ( C < lowest) lowest = C
	if ( W < lowest) lowest = W

	cout << "" << "\n";
	
	if ( lowest == N) cout "North\n";
	if ( lowest == E) cout "EAST\n";
	if ( lowest == S) cout "SOUTH\n";
	if ( lowest == C) cout "CENTRAL\n";
	if ( lowest == W) cout "WEST\n";

	cout << "" << lowest << "accents\n";	
}

58 to 61 are missing ; at the end of the line.
65 to 69 are missing << on the cout

And getAccidents is declared void, but returns a result (which you then ignore).

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
$ g++ foo.cpp
foo.cpp: In function ‘void getAccidents(int, int, int, int, int)’:
foo.cpp:54:10: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
   54 |   return crashes;
      |          ^~~~~~~
foo.cpp: In function ‘void findLowerst(int, int, int, int, int)’:
foo.cpp:62:15: error: expected ‘;’ before ‘if’
   62 |     lowest = E if (S < lowest)
      |               ^~~
      |               ;
foo.cpp:68:9: error: expected ‘;’ before string constant
   68 |     cout "North\n";
      |         ^~~~~~~~~~
      |         ;
foo.cpp:70:9: error: expected ‘;’ before string constant
   70 |     cout "EAST\n";
      |         ^~~~~~~~~
      |         ;
foo.cpp:72:9: error: expected ‘;’ before string constant
   72 |     cout "SOUTH\n";
      |         ^~~~~~~~~~
      |         ;
foo.cpp:74:9: error: expected ‘;’ before string constant
   74 |     cout "CENTRAL\n";
      |         ^~~~~~~~~~~~
      |         ;
foo.cpp:76:9: error: expected ‘;’ before string constant
   76 |     cout "WEST\n";
      |         ^~~~~~~~~
      |         ;

Look at your function declaration at line 4
void getAccidents(int&, int&, int&, int&, int&);
Now look at the function definition at line 21
void getAccidents(int regN, int regE, int regS, int regC, int regW)
What's the difference? What's missing?

That function has a return type of void, yet you are trying to return a value on line 51.

Lines 65-69, you are missing the insertion operator (<<) in your output statements.

Lines 58-61 the semi-colons terminating the statements went on sabbatical.

The case labels in your switch statement starting at line 28 are borked. The labels NORTH et al are undefined. Did you mean something like
case 'N': cout << "NORTH"; ?
Line 10, none of these are initialised, so contain garbage values.

The values are not used at all in the getAccidents function, but they are in the findLowerst function.
Last edited on
Simply without input validation consider:

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

void getAccidents(int&, int&, int&, int&, int&);
void findLowerst(int, int, int, int, int);

int main() {
	int crashN {}, crashE {}, crashS {}, crashC {}, crashW {};

	getAccidents(crashN, crashE, crashS, crashC, crashW);
	findLowerst(crashN, crashE, crashS, crashC, crashW);
}

void getAccidents(int& regN, int& regE, int& regS, int& regC, int& regW) {
	std::cout << "Enter crashes for North: ";
	std::cin >> regN;

	std::cout << "Enter crashes for East: ";
	std::cin >> regE;

	std::cout << "Enter crashes for South: ";
	std::cin >> regS;

	std::cout << "Enter crashes for Central: ";
	std::cin >> regC;

	std::wcout << "Enter crashes for West: ";
	std::cin >> regW;
}

void findLowerst(int N, int E, int S, int C, int W) {
	int lowest {N};

	if (E < lowest) lowest = E;
	if (S < lowest) lowest = S;
	if (C < lowest) lowest = C;
	if (W < lowest) lowest = W;

	if (lowest == N) cout << "\nNORTH ";
	if (lowest == E) cout << "\nEAST ";
	if (lowest == S) cout << "\nSOUTH ";
	if (lowest == C) cout << "\nCENTRAL ";
	if (lowest == W) cout << "\nWEST ";

	cout << lowest << " accents\n";
}



Enter crashes for North: 34
Enter crashes for East: 56
Enter crashes for South: 45
Enter crashes for Central: 32
Enter crashes for West: 10

WEST 10 accents

Topic archived. No new replies allowed.