Code not reading in Array from .txt file?

I seem to be having a problem reading in an array from a .txt file. I am looking to use a binary search to search an array so that the position of the number entered is outputted.

Whenever I run the program it will not read in any of the array numbers in the .txt file, but instead it will repeat the following:

Enter a number to find in the array: Entered Number
The number you entered does not exist in the array.

It seems that the problem is that the array from the .txt file is not being read whenever the code is compiled.

How can I fix this problem?



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

using namespace std; 

int binarySearch(const int [], int, int);
const int SIZE = 99;

int main()
{
	ifstream file("Numbers.txt");
    if(file.is_open())
    {
        string myArray[99];

        for(int i = 0; i < 99; ++i)
        {
            file >> myArray[i];
        }
    }
	
	int myArray[SIZE];
	int results;
	int arryNum;
	
	do
	{
	cout<<"Enter a number to find in the array: ";
	cin>>arryNum;
	results = binarySearch(myArray, SIZE, arryNum);
	
	if (results == -1)
		cout<<"The number you entered does not exist in the array. \n";
	else
	{
		cout<<"Found @ literal position(s): "<<results<<endl;
		cout<<"To quit enter 1000"<<endl;
	}
	
 	}while(arryNum != 1000);
 	return 0;
}
int binarySearch(const int array[], int size, int value)
{
	int first = 0,
	    last = size - 1,
	    middle,
	    position = -1;
	bool found = false;
	
	while(!found && first <= last)
	{
		middle = (first + last) / 2;
		if (array[middle] == value)
		{
			found = true;
			position = middle; 
		}
		else if (array[middle] > value)
			last = middle - 1; 
		else
			first = middle + 1; 
	}
	return position;
}
Last edited on
The array on line 15 is thrown away on line 21.

The array on line 23 (with the same name) has nothing to do with the file you read in, and then subsequently threw away.

How would I fix this?
How did you write it in the first place?

Or did you write it in the first place?

The change of indentation and style for just this bit suggests tutor provided boilerplate, with a requirement you add the file reading code.
1
2
3
4
5
6
7
8
9
    if(file.is_open())
    {
        string myArray[99];

        for(int i = 0; i < 99; ++i)
        {
            file >> myArray[i];
        }
    }

What made you choose 'string' as a type?
What made you choose to write 99 instead of SIZE?

Why didn't you make use of the existing array?

All you had to do was put this block of code after int myArray[SIZE];
Hello EarlyProgrammer,

Your use of the if statement is common with new programmers. It does work, but as in this case it is giving you a problem.

I have found this to be a better way of dealing with a file stream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	int myArray[SIZE]{};

	ifstream file("Numbers.txt");
	
	if (!file)
	{
		std::cout << "File \"Numbers.txt\' did not open" << std::endl;
		return 1;
	}

	for (int i = 0; i < SIZE; ++i)
	{
		file >> myArray[i];
	}

You have defined the constant "SIZE", so make use of it.

The for loop works as long as you have 99 numbers in the file. If by chance there is less than 99 numbers the for loop would continue until "i" equals 99 and the test fails leaving you to process a file stream that has failed and looping for no reason. Something you could try, and I have not tested this yet, is:
for (int i = 0; i < SIZE && file >> myArray[i]; ++i); which will eliminate the need for the block. And if you have less than 99 numbers the for loop will end when the file stream reaches end of file or "i" becomes 99 which ever comes first.

Another alternative is: while (i < SIZE && file >> myarray[i++]);. It works the same as the for loop. In future programs you might use this as:
1
2
3
4
5
while (file >> aVariable)
{
    file >> bVariable;
    file >> cVariable;
}

The way this works is as long as the while condition can read something you keep looping. When the read tries to read past end of file the stream fails and so does the while condition.

Hope that helps,

Andy
Hello EarlyProgrammer,

Something that occurred to me while testing your program. This is a case where "i" is better defined outside of the for loop. This way when the for loop ends "i" will have a value of how many numbers have been read. This can be used later in the program to only process the part of the array that is used should there be less than 99 numbers.

Also instead of using "i" a better name would be "count" or "numbersRead". Something that has a better meaning than "i".

BTW using what you started with the for loop would now look like:
for (i = 0; i < SIZE && file >> myArray[i]; ++i);

And I also changed this: int myArray[SIZE]{}, i{};

Hope that helps,

Andy

Edit: typo
Last edited on
Registered users can post here. Sign in or register to post.