For loop not working properly in a linear search?

I seem to be having a problem with a for loop, in creating a linear search of a .txt file.

What I am aiming to do is to have the output "Found @ literal position(s):" to be displayed twice if the same element is found at different positions in the .txt file.

Instead of outputting the former, the code seems to only output the statments:

"Enter a number to find in the array: Entered Number "
"To quit enter 1000"

And after it is outputted the code loops.

I believe the source of the problem is the if statement I created.

1
2
3
4
5
6
for (int i = 0; i<SIZE; i++)
	{
	if (myArray[i] == arryNum)
	cout<<"Found @ literal position(s):" <<results<<endl;
	}


If so, then how would I fix it?



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

using namespace std; 

int linearSearch(const int [], int, int);

int main()
{
  	const int SIZE = 99;
  	int myArray[SIZE], i[SIZE];

	ifstream file("Linear Numbers.txt");
	
	if (!file)
	{
		cout << "The Text File Will Not Open!" << endl;
		return 1;
	}
  
	int results;
	int arryNum;
	
	do
	{
	cout<<"Enter a number to find in the array: ";
	cin>>arryNum;
	results = linearSearch(myArray, SIZE, arryNum);
	
	for (int i = 0; i<SIZE; i++)
	{
	if (myArray[i] == arryNum)
	cout<<"Found @ literal position(s):" <<results<<endl;
	}
	
	cout<<"To quit enter 1000"<<endl;
	
 	}while(arryNum != 1000);
 	 return 0;
}

int linearSearch(const int array[], int size, int value)
{
	int index = 0;
	int position = -1;
	bool found = false;
	
	while (index < size && !found)
	{
		if(array[index] == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position; 
}
EarlyProgrammer wrote:
how would I fix it?


Well, you could try actually reading something from file, instead of just opening it and ... hoping!
Just like @lastchance said...
You are not getting information from the file...

this is the basic structure to get information from a file (sorry about any unintended declaration, this is a copy-paste):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void SeeBytesTXT()
{
	cout << "####### SEE BYTES #######\n";
	streampos Begin, End;
	ifstream myfile("example.txt");
	Begin = myfile.tellg();
	myfile.seekg(0, ios::end);
	End = myfile.tellg();
	cout << (End - Begin) << " bytes.\n";

	myfile.seekg(0, ios::beg); // Return to beginning
	while (myfile.tellg() < End && myfile.tellg() >= 0)
	{
		unsigned char Read; // Read[size] if you want to get more than one byte
		myfile.read((char*)&Read, sizeof(Read)); // Read "size" byte stream of a file
		/*
		add your arguments here!
		*/
	}
	myfile.close();
}


I'll try to write based on what i think you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void SeeBytesTXT()
{
	cout << "####### SEE BYTES #######\n";
	streampos Begin, End;
	ifstream myfile("example.txt");
	Begin = myfile.tellg();
	myfile.seekg(0, ios::end);
	End = myfile.tellg();
	cout << (End - Begin) << " bytes.\n";

	myfile.seekg(0, ios::beg); // Return to beginning
	while (myfile.tellg() < End && myfile.tellg() >= 0)
	{
		int Read;
		myfile.read((char*)&Read, sizeof(Read)); // Read "size" byte stream of a file
		if (Read == arryNum)
		{
			found = true;
			position = myfile.tellg();
		}
	}
	myfile.close();
}


In an example, I'll copy paste my code to find a string in a binary file (note that i already have a header position defined and some functions and classes are defined but not visible, but ignore it):
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
void GetFirstChunk()
{
	int ThisBytes;
	streampos Begin, End;
	int Actual, IntEnd;

	ifstream FILE("teste.file", ios::binary | ios::in | ios::out | ios::ate);
	End = FILE.tellg(); IntEnd = FILE.tellg();
	FILE.seekg(0, ios::beg);
	Begin = FILE.tellg();
	ThisBytes = End - Begin;
	cout << "Begin: " << Begin << " | End: " << End << " | ThisBytes: " << ThisBytes << endl;

	int Count = 0, Counta = 0; TextBox txbx; bool Isit = false; int Itis;
	vector<char> GetChnk; Chunk TChnk;
	FILE.seekg(Headern); Actual = FILE.tellg();
	string Name = "ABCDEFG"; TChnk.Name = Name;
	cout << "\n-------\nActual: " << Actual << "\n-------\n";
	while (FILE.tellg() < End && FILE.tellg() >= 0)
	{
		Actual = FILE.tellg();
		char Skip;
		FILE.read((char*)&Skip, sizeof(Skip));
		GetChnk.push_back(Skip);
		if (Skip == Name[Counta] && Isit == false)
		{
			cout << Skip << " | ";
			++Counta;
			if (Counta == Name.length())
			{
				Isit = true;
				Counta = 0;
				Itis = Actual - (Name.length());
				cout << "\n!!! 'Itis' in this byte: " << Itis << endl;
				TChnk.ByteMap = Itis;
			}
		}
		else { Counta = 0; }
	}
	TChnk.SendChunk(GetChnk);
	Chnk.push_back(TChnk);
}

Topic archived. No new replies allowed.