Need Help With Arrays and Files

I need to write a program that can read in a maximum of 100 numbers from a text document. I also have to show how many numbers were in the text document.I was trying to use loops to count how many there were, but it always shows 100. I'm really stumped here and any help would be appreciated!


For example, I have 10 numbers in a text document. How would I get it to only read those 10 numbers and not store anything else in the remainder of the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{	
	int a[100], x;

	ifstream File;
	File.open ("input.txt");

	for(x=0;x<100;x++)
	{
		File>>a[x];
	}
	cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<endl<<a[11]<<endl; //for testing purposes.

	cout<<x<<endl;

	File.close();

	system("PAUSE");
	return 0;
}


By reading this, hopefully someone will better understand what I have to do:
You will need to create a program that reads in at most 100 values from a file (input.txt) and stores them into an array. You will need to keep track of how many values were read in from the file. You will also need to find the highest and lowest values stored in the array and which array element they are stored in. You will need to output the total number of elements, the sum total of all the values, the highest value and which array element it is stored in, and the lowest value and which array element it is stored in to the console and an output file. The output file should be "yourName.txt". You will need to create a program that reads in at most 100 values from a file (input.txt) and stores them into an array. You will need to keep track of how many values were read in from the file. You will also need to find the highest and lowest values stored in the array and which array element they are stored in. You will need to output the total number of elements, the sum total of all the values, the highest value and which array element it is stored in, and the lowest value and which array element it is stored in to the console and an output file. The output file should be "yourName.txt".


I only need help with the underlined portion.
Last edited on
closed account (zb0S216C)
All you have to do is follow these steps:
1) Create an array to store your extracted numbers and initialize all numbers to -1( you will see why later ).
2) Open the file that contains the numbers.
3) Create a for loop that suits your needs.
4) In the loop, extract each number, whilst placing the extracted number into the next slot in the array.
5) When the loop has finished, enter another loop.
6) With the new loop, count how many elements in the array that have the value of -1.

If your file contains negative numbers as well as positive numbers, you could create a small structure like this:
1
2
3
4
5
6
7
struct Number
{
    int Value;
    bool Used; // This is only true if "Value" is holding an extracted value.
};

Number Numbers[ 100 ] = { 0, false };

Using the above code segment will mean your array can hold any positive or negative value.

There are probably other ways, but you could try this to see if it works for you.
Last edited on
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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{	
	int a[100], x = 0; //initialize the number of numbers (x) to 0

	ifstream File;
	File.open ("input.txt");

	// for(x=0;x<100;x++)
	// {
	while( File.good() && x < 100 ) //read only as long as the file is valid to read from, up to 100
		File>>a[x++]; //you read a value, so increase x to refect that
	// }
	cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<endl<<a[11]<<endl; //for testing purposes.

	cout<<x<<endl;

	File.close();

	system("PAUSE");
	return 0;
}


I ran this with the input file
1 2 3 4 5
6 7 8 9 10

and i got (note line 2 is garbage data and will not be the same on your machine)
12345678910
3554224
10
Press any key to continue . . . 


Let me know if this is what you needed, or if you have any questions.
Last edited on
Topic archived. No new replies allowed.