How to fill an array with integers from a .txt file

For one of my assignments in school, I was asked to pass integer values from a .txt file into an array for evaluation, but I'm confused on how to put integer values from a .txt file to an array. Could I have help?

Just in case the values from the .txt file is necessary:

woohoo.txt = 76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129
149 176 200 87 35 157 189
Try using a loop to read from the file each integer and place it into an array.
closed account (DEUX92yv)
Zhuge is right. To read from the file, use fstream.

A brief example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream> // This is the library for the filestream functions
using namespace std;

int main()
{
   int yourarray[26];  // Consider using dynamic allocation
   ifstream yourstream ( filename );  // Declares infile stream; in your case, use "woohoo.txt"
                                      // (with quotation marks)
   /* Optional error checking:
   if ( ! yourstream.is_open() ) {
      return 1;
   }   */

   for (int i = 0; i < 26; i++) { // This loop will be a bit different if you dynamically allocate
      yourstream >> yourarray[i];
   }

   yourstream.close();

   return 0;
}


In your case, you know the number of integers you're reading in, so dynamic allocation isn't necessary, but it's a good idea to learn in case you don't know the .txt file's contents.

For the ifstream object, "yourstream" can be any name you choose - make it something descriptive. It just has to be the same name throughout your code. The name you choose doesn't affect anything.

Last edited on
I got that far, but after that, I am trying to display how many numbers in the array are between 0 and 24 with variable "first", but intellisense tells me that the "first" variable that I am trying manipulate in the while loop is not defined, although it's clearly defined there.




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

using namespace std;

void displayScores(int scores[]);

int main()
{
	ifstream scoreFile;
	scoreFile.open("Ch8_ex4_Info.txt");
	int scoreArray[26];


	for (int i=0; i <= 25; i++)
	{
		scoreFile >> scoreArray[i];
	}

	displayScores(scoreArray);


}
void displayScores(int scoreArray[])
{

int i=0;
int first;

while (i <= 25)
{
	
	if (scoreArray[i] >= 0 && scoreArray[i] <= 24)
	{
		first++;	
	}
	cout << first;
	i++;
}
cout << first << endl;
		

}
closed account (DEUX92yv)
That's because you define "first" as an int, but never initialize it. So in the while loop, whenever you increment "first," you're adding 1 to some random integer. In line 28, add "= 0" and it should work perfectly.

Two other suggestions:
1) Move cout << first; (line 37) to inside of the preceding if loop; this way, "first" will only be printed whenever a number is between 0 and 24, instead of printing a huge line of 0s and 1s, in which "first" is printed every time the loop iterates. Additional possibility in this suggestion: print "first" before entering the loop, to show the user its initial value.
2) Change the while loop (lines 30-39) to a for loop! Because you know how many iterations you want, and you increment "i" regularly, a while loop isn't necessary here. It only saves you one line of code (gets rid of the "i++" in line 38), but it's good practice to use for loops when you know the desired number of iterations.
Last edited on
Topic archived. No new replies allowed.