URGENT THIS PROGRAM NEEDS TO BE WORKING FOR TOMORROW

im searching through a file with a for loop
it says the number is not found even though the number is correct but if i keep pressing enter the details show up eventually

heres the outout:
Enter a number : 40029
The number 40029 was not found

The number 40029 was not found


The number 40029 was found

DOB             Student Results
1/5/1986        333333


and it will keep going for 15 lines

heres the code
1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < 15; i++)
{
	if (found && number == student[i].number)
		{
			cout << "The number " << number << " was found" << endl;			cout << "DOB" << "\t\t" << "Student Results" << endl;
			cout << student[i].dob.day << "/" << student[i].dob.month << "/" << student[i].dob.year << "\t" << student[i].scores[0] << student[i].scores[1] << student[i].scores[2] << student[i].scores[3]<< student[i].scores[4] << student[i].scores[5] << endl;
break;
		}
	else
	{
	        cout << "The number " << number << " was not found" << endl;
		cin.get ();cin.get ();
	}


it shouldnt be saying the number is not found because it was the correct number and i dont know why it goes through the loop a few more times then finds the number
thanks in advance
Last edited on
I don't see where you read from the file, and where you modify the found boolean value.
ok here is all of the code. sorry i thought the problem was just in the loop
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "stdafx.h"
using namespace std;

const int NUM_SCORES = 6;
typedef struct 
	{
		int day;
		int month;
		int year;
	} Date;


struct Student 
	{
		int number;
		Date dob;
		float scores[NUM_SCORES];
	} student;

const int MAX_ITEMS = 5;

struct ListType 
	{
		int length;
		Student info[MAX_ITEMS];
	}; ListType list;

int entries;

void readStudent (Student student[], int &entries);

void BinarySearch (Student list[], int& item, bool& found);

int _tmain(int argc, _TCHAR* argv[])
{
	Student student[15];
	
	
	readStudent (student, entries);


	int number;
	bool found = false;

	cout << "Enter a student number : ";
	cin >> number;
	BinarySearch(student, number, found);

	
	for (int i = 0; i < 15; i++)
	{
		if (found && number == student[i].number)
		{
				cout << "The number " << number << " was found" << endl << endl;
				cout << "DOB" << "\t\t" << "Student Results" << endl;
				cout << student[i].dob.day << "/" << student[i].dob.month << "/" << student[i].dob.year << "\t" 
				<< student[i].scores[0] << student[i].scores[1] << student[i].scores[2] << student[i].scores[3]
				<< student[i].scores[4] << student[i].scores[5] << endl;
				break;
		}
	else
	{
		cout << "The number " << number << " was not found" << endl;
		cin.get ();cin.get ();
	}
		
		
	}
	
	
	return 0;
	}


void BinarySearch (Student list[], int& item, bool& found)
	{
		int midPoint;
		int first = 0;
		int last = entries-1;
		bool moreToSearch = first <= last;
		found = false;

		while (moreToSearch && !found)
		{
			midPoint = (first + last) / 2;

		if (item < list[midPoint].number)
		{
			last = midPoint - 1;
			moreToSearch = first <= last;
		}
			else if (item > list[midPoint].number) {
			first = midPoint +1;
			moreToSearch = first <= last;
		}
	else 
	{
		item = list[midPoint].number;
		found = true;
	}
	} 

	}


void readStudent (Student student[], int &entries)
	{
		fstream in;

		int i=0;

		in.open("students.txt", std::ios::in);
		while (!in.eof()){

		in >> student[i].number>>student[i].dob.day>>student[i].dob.month>>student[i].dob.year>>student[i].scores[0]>>student[i].scores[1]>>student[i].scores[2]>>student[i].scores[3]>>student[i].scores[4]>>student[i].scores[5];
		
		i++;
	}

	entries = i;

	in.close();
	cout << endl;
	}



}
Last edited on
hello can anyone help with this?
Just basic glazing over the code, quick question, is the data being read in sorted?


The issue is that you do a binary search to see if the record is in the array, then if it is, you dutifully search through each record again using a sequential search. Have the binary search tell you the index of the found record. Also, look at the for block starting on line 50:
1
2
3
for(...)
if (found && ...)
{...}else{cout<<"Number not found"; ...;}

You never change found from within the loop, if it is false, you are going to spit out the else block 15 times in a row, no matter what else. Make found a condition of entering the loop, because if you know the number isn't there, you know you can skip the entire loop, because you will never find the number that isn't there. But more importantly, you should have the binary search tell you the index. I'm almost positive you would get marked down for using a linear search again later for completely missing the point of the exercise.

The assignment on line 98 is completely useless as well, it will always be assigning the integer it currently has to itself again, by definition.
Last edited on
Topic archived. No new replies allowed.