Debugging Global Variables

I need help debugging this program with functions.

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
/* Write a program that determines which of five geographic regions within a major city (north, south, east, west and central)
had the fewest reported automobile accidents last year. It should have the following two functions, which are called by main.
int getNumAccidents() is passed the name of a region. It asks the user for the number of automobile accidents reported in that 
region during the last year, validates the input, then returns it. It should be called once for each city region. void findLowest() 
is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its 
accident figure. Input Validation: Do not accept a number that is negative.*/

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

//function prototypes

int getNumAccidents(int);
void findLowest(int north,int south,int east,int west,int central);

int main()
{												//defining variables
	int getNumAccidents(string regionName);
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central;

 cout<<"There are 5 major regions within the city.\n";     
	              
			 regName="North: ";
			 north = getNumAccidents(regName);
	         
			 regName="South: ";
	         south = getNumAccidents(regName);
	         
			 regName="East: ";
	         east = getNumAccidents(regName); 
			
			 regName="West: ";
	         west = getNumAccidents(regName); 
	         
			 regName="Central: ";
	         central = getNumAccidents(regName);

			 void findLowest(int north,int south,int east, int west,int central);
			 system("pause");
			 return 0;

	} //End of main function
	 
int getNumAccidents(string regionName)
	 
	{  
	   int numAccidents;
         do
         {
                cout << "Enter the number of accidents for the " << regionName;                 
				cin >> numAccidents;

          } while (numAccidents < 0);		//validate the input
          return numAccidents;  

} //End of function getNumAccidents
	 
void findLowest(int north, int south, int east, int west, int central,int numAccidents)
{
	 string regName;
	 for (int i=1; i < numAccidents; i++)
	 {
		if (numAccidents > north  )
		{
			regName = north;
		}

		if (numAccidents > south  )
		{
			regName = south;
		}
		
		if (numAccidents > east  )
		{
			regName = east;
		}

		if (numAccidents > west  )
		{
			regName = west;
		}

		if (numAccidents > central  )
		{
			regName = central;
		}
	 }
		cout << "The region with least amount of accidents is " << regName << endl;
} //End of function findLowest 


It won't display the findLowest, the program ends after I input the number of accidents for north, south, and etc.
Last edited on
on line 45: it's again the prototype and not calling the function.

you need to write it like so:
findLowest(north,south,east, west,central);
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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

//function prototypes

int getNumAccidents(int);
void findLowest(int north,int south,int east,int west,int central);

int main()
{												//defining variables
	int getNumAccidents(string regionName);
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central;

 cout<<"There are 5 major regions within the city.\n";     
	              
			 regName="North: ";
			 north = getNumAccidents(regName);
	         
			 regName="South: ";
	         south = getNumAccidents(regName);
	         
			 regName="East: ";
	         east = getNumAccidents(regName); 
			
			 regName="West: ";
	         west = getNumAccidents(regName); 
	         
			 regName="Central: ";
	         central = getNumAccidents(regName);

			 findLowest(north,south,east,west,central);
			 system("pause");
			 return 0;

	} //End of main function
	 
int getNumAccidents(string regionName)
	 
	{  
	   int NumAccidents;
         do
         {
                cout << "Enter the number of accidents for the " << regionName;                 
				cin >> NumAccidents;

          } while (NumAccidents < 0);		//validate the input
          return NumAccidents;  

} //End of function getNumAccidents
	 
void findLowest(int north, int south, int east, int west, int central,int NumAccidents)
{
	 string regName;
	 for (int i=1; i < NumAccidents; i++)
	 {
		if (NumAccidents > north  )
		{
			regName = north;
		}

		if (NumAccidents > south  )
		{
			regName = south;
		}
		
		if (NumAccidents > east  )
		{
			regName = east;
		}

		if (NumAccidents > west  )
		{
			regName = west;
		}

		if (NumAccidents > central  )
		{
			regName = central;
		}
	 }
		cout << "The region with least amount of accidents is " << regName << endl;
} //End of function findLowest 


I did that but I'm getting errors still.

1>  LINK : C:\Users\Sena\Documents\Visual Studio 2010\Projects\CS140_2.15_pc4\Debug\CS140_2.15_pc4.exe not found or not built by the last incremental link; performing full link
1>CS140_2.obj : error LNK2019: unresolved external symbol "void __cdecl findLowest(int,int,int,int,int)" (?findLowest@@YAXHHHHH@Z) referenced in function _main
1>C:\Users\Sena\Documents\Visual Studio 2010\Projects\CS140_2.15_pc4\Debug\CS140_2.15_pc4.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
That is because the prototype on line 9 does not match the function on line 58. What is NumAccidents?
NumAccidents is the number of accidents/crashes input by user.
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
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

//function prototypes

int getNumAccidents(int NumAccidents);
void findLowest(int north,int south,int east,int west,int central,int NumAccidents);

int main()
{												//defining variables
	int getNumAccidents(string regionName);
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central,
		NumAccidents;

 cout<<"There are 5 major regions within the city.\n";     
	              
			 regName="North: ";
			 north = getNumAccidents(regName);
	         
			 regName="South: ";
	         south = getNumAccidents(regName);
	         
			 regName="East: ";
	         east = getNumAccidents(regName); 
			
			 regName="West: ";
	         west = getNumAccidents(regName); 
	         
			 regName="Central: ";
	         central = getNumAccidents(regName);

			 findLowest(north,south,east,west,central,NumAccidents);
			 system("pause");
			 return 0;

	} //End of main function
	 
int getNumAccidents(string regionName)
	 
	{  
	   int NumAccidents;
         do
         {
                cout << "Enter the number of accidents for the " << regionName;                 
				cin >> NumAccidents;

          } while (NumAccidents < 0);		//validate the input
          return NumAccidents;  

} //End of function getNumAccidents
	 
void findLowest(int north, int south, int east, int west, int central, int NumAccidents)
{
	 string regName;
	 for (int i=1; i < NumAccidents; i++)
	 {
		if (NumAccidents > north  )
		{
			regName = north;
		}

		if (NumAccidents > south  )
		{
			regName = south;
		}
		
		if (NumAccidents > east  )
		{
			regName = east;
		}

		if (NumAccidents > west  )
		{
			regName = west;
		}

		if (NumAccidents > central  )
		{
			regName = central;
		}
	 }
		cout << "The region with least amount of accidents is " << regName << endl;
} //End of function findLowest 


Hmm now I'm getting "The variable 'NumAccidents' is being used without being initialized." error.
Last edited on
line 13 is another prototype. replace line 8 with line 13.

Hmm now I'm getting "The variable 'NumAccidents' is being used without being initialized." error.
Yes, you provide no value for that variable in main(). But you may not want it as a parameter?

you use wrongfully NumAccidents for the loop and the comparison. if you want to find out the least amount of accidents you need no loop and NumAccidents is not a parameter but a local variable. Initialize NumAccidents and regName with their values for north. Leave the rest of the ifs as they are. Remove the for loop

By the way: this regName = north; and below is not correct. You are assigning the number of accidents not the name of the region.
Sorry for the delay. Here's my new code.

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
/* Write a program that determines which of five geographic regions within a major city (north, south, east, west and central)
had the fewest reported automobile accidents last year. It should have the following two functions, which are called by main.
int getNumAccidents() is passed the name of a region. It asks the user for the number of automobile accidents reported in that 
region during the last year, validates the input, then returns it. It should be called once for each city region. void findLowest() 
is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its 
accident figure. Input Validation: Do not accept a number that is negative.*/
 
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

//function prototypes

int getNumAccidents(int NumAccidents);
void findLowest(int north,int south,int east,int west,int central); //global variables
int NumAccidents;

int main()
{												//defining variables
	int getNumAccidents(string regionName);
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central,
		NumAccidents;

 cout<<"There are 5 major regions within the city.\n";     
	              
			 regName="North: ";
			 north = getNumAccidents(regName);
	         
			 regName="South: ";
	         south = getNumAccidents(regName);
	         
			 regName="East: ";
	         east = getNumAccidents(regName); 
			
			 regName="West: ";
	         west = getNumAccidents(regName); 
	         
			 regName="Central: ";
	         central = getNumAccidents(regName);

			 findLowest(north,south,east,west,central);
			 system("pause");
			 return 0;

	} //End of main function
	 
int getNumAccidents(string regionName)
	 
	{  
	   int NumAccidents;				//hold values
         do
         {
                cout << "Enter the number of accidents for the " << regionName;                 
				cin >> NumAccidents;

          } while (NumAccidents < 0);		//validate the input
          return NumAccidents;				//return number of accidents

} //End of function getNumAccidents
	 
void findLowest(int north, int south, int east, int west, int central)
{
	 string regName;
	 int crash = north;
	 {
		if (crash > south)
		{
			regName = south;
			cout<<"South";
			crash = south;
		}

		else if (crash > east)
		{
			regName = east;
			cout<<"East";
			crash = east;
		}

		else if (crash > west)
		{
			regName = west;
			cout<<"West";
			crash = west;
		}

		else if (crash > central)
		{
			regName = central;
			cout<<"Central";
			crash = central;
		}
	 }
		cout << " is the region with the least amount of accidents with " << crash << " acciddents.";
} //End of function findLowest 


Although there's still some errors, I need more debugging help. I fixed the lowest function so that its now comparing the crashes. I think the only issue is with my findLowest function here, which is giving me the error "unreferenced local variable."
Last edited on
which is giving me the error "unreferenced local variable."
This is not an error but a warning. Remove line 17, you don't need it.

Replace line 15 with line 21.

Look at line 74 and then at line 35. Are you able to spot the difference?
What is south and what is regName? What do you think will happen when you assign them? The same applies to east, west, etc... You need to understand the problem.
But well, since you don't use regName anymore you can remove it anyway

Remove all else you find within findLowest()
Topic archived. No new replies allowed.