How to properly read char from file into an array?

I need help with properly reading and storing some characters from a text file into my array. I got something going but when I run it, the output shows only the first 10 characters.

The text file I am using is the following...
B
D
D
C
B
C
A
C
C
D
A
B
A
B
C
B
D
B
C
D

But like I said when I run the program. It only displays the first 10 letters from that text file. That is precisely where I am lost and need help. Thanks.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

int main ()
{
	ifstream inFile;
	const int numofQ = 20;
	char CorrectAnswers[numofQ];

	inFile.open("CorrectAnswers.txt");
	
	for (int x = 0; x < numofQ; x++)
	{

		for (char write2line; inFile.get(write2line);) //used to read individual characters
		{
			CorrectAnswers[x] = write2line;
			break;
		}

		cout << CorrectAnswers[x];
	}

	system("pause");
	return 0;
}
Each character in the input file is on a separate line. This inFile.get(write2line) first reads the alphabetic character, the next time it reads the newline character '\n', then the next letter, then the next newline and so on.

In order to skip the newline (or other whitespace), use formatted input inFile >> instead of inFile.get().

1
2
3
4
5
6
7
8
9
10
11
    // first read the file
    for (int x = 0; x < numofQ; x++)
    {
        inFile >> CorrectAnswers[x];
    }

    // then display array contents
    for (int x = 0; x < numofQ; x++)
    {
        cout << CorrectAnswers[x] << '\n';
    }
Topic archived. No new replies allowed.