Saying that MID is not being initialized

I have been working on this code now for about a month and still am getting errors at line 88 when I am doing a binary search for a data structure. The full code is here. I am wondering if I am loosing a connection to the MID when I do the search. Thanks for any 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
96
97
98
99
100
101
102
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

struct STUDENT
{
	int SSN;
	string LAST_NAME;
	string FIRST_NAME;
	double BALANCE_DUE;
}CLASS[99];

void main()
{
	int L;
	int K;
	int N;
	int I;
	int GAP;
	int SWAPEM;
	int SWITCHEM;
	int FIRST;
	int MID;
	int LAST;
	char ANSWER;
	int SEARCHME;

	cout << "FINAL PROGRAM" << endl;
	cout << "Would you like to input values? (Y/N)" << endl;
	cin >> ANSWER;
	I=0;
	while (ANSWER == 'Y' || ANSWER == 'y')
	{
		cout << "Input SSN: " << endl;
		cin >> CLASS[I].SSN;
		cout << "Input First Name: " << endl;
		cin >> CLASS[I].FIRST_NAME;
		cout << "Input Last Name: " << endl;
		cin >> CLASS[I].LAST_NAME;
		cout << "Input Balance Due: " << endl;
		cin >> CLASS[I].BALANCE_DUE;
		I=I+1;
		cout << "Would you like to input values? (Y/N)" << endl;
		cin >> ANSWER;
	}
	N=I-1;
	GAP=N/2;
	while (GAP > 0)
	{
		SWITCHEM=1;
		while (SWITCHEM=1)
		{
			SWITCHEM=0;
			for (I=0;I<N-GAP;I++)
			{
				if (CLASS[I].SSN > CLASS[I+GAP].SSN)
				{
					SWAPEM=CLASS[I].SSN;
					CLASS[I].SSN=CLASS[I+GAP].SSN;
					CLASS[I+GAP].SSN=SWAPEM;
					SWITCHEM=1;
				}
			}
		}
		GAP=GAP/2;
	}
	cout << "Would you like to retrieve a SSN? (Y/N)" << endl;
	cin >> ANSWER;
	while (ANSWER == 'Y' || ANSWER == 'y')
	{
		cout << "Enter SSN: " << endl;
		cin >> SEARCHME;
		FIRST=0;
		LAST=N;
		while (FIRST < LAST)
		{
			MID=(FIRST+LAST)/2;
			if (SEARCHME=CLASS[MID].SSN)
			{
				cout << "SSN is: " << CLASS[MID].SSN << endl;
				cout << "First Name is: " << CLASS[MID].FIRST_NAME << endl;
				cout << "Last Name is: " << CLASS[MID].LAST_NAME << endl;
				cout << "Balance Due is: " << CLASS[MID].BALANCE_DUE << endl;
				FIRST=LAST+1;
			}
			else
				if (SEARCHME < CLASS[MID].SSN)
				{
					LAST=MID-1;
				}
				else
					FIRST=MID+1;
		}
		if (SEARCHME != CLASS[MID].SSN)
		{
			cout << "SSN Not Found!" << endl;
		}
		cout << "Would you like to retrieve a SSN? (Y/N)" << endl;
		cin >> ANSWER;
	}
}
bump
bump
bump
1) Use int main().
2) If FIRST <= LAST (like you have an array of only one element), then you could get to line 95 without actually having given MID a value. Not sure why the warning is on line 88 though.
3) I'd highly suggest using functions in this case to make the code cleaner, as it is it is really hard to read.
richardrhaul wrote:
getting errors at line 88 when I am doing a binary search for a data structure
what kind of errors?

In C++ you can declare variables where they are need. So you can omit line 24 and write at line 78 const int MID=(FIRST+LAST)/2; // Since it doesn't change it can be const and the other variables alike

and yes, better write a function for the binary search (which is usually recursive)
OK, Thank you both I am going to go and do that. The error that i am getting is it says that MID is not being initialized but it does link back up to the top where I stated in the variables that MID is an int.
oops, i overlooked line 95. so you can't change line 78 instead write before line 76 int MID = 0;
Topic archived. No new replies allowed.