Need some help with reading a file into an array

I am working on a project that grades a drivers liscense test. It involves two programs. The first being creating a file of 20 random answers. The second program grades the answers using arrays. I am 90% done but I am having problems with reading a file into an array. I cant seem to skip the number of the questions. I will explain more further on.

Program that creates 20 random answers
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	const int numquestions = 20;

	char studanswer[numquestions];

	int numb[numquestions] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

	ofstream outputfile;

	//Stores the Array
	for (int index = 0; index < 20; index++)
	{
		studanswer[index] = 65 + rand()% 4;
	}

	//Opens the file
	outputfile.open("studentanswers.txt");

	//Writes to the file
	for (int index = 0; index < numquestions; index++)
	{
		outputfile << numb[index] << " " << studanswer[index] << endl;
	}

	//Closes file
	outputfile.close();

	cout << "The numbers were saved to a file\n";

	return 0;

}


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

using namespace std;

int main()
{
	const int AnsNumb = 20;

	char CorrectAnswer[AnsNumb];//Array for Correct Answers

	char StudentAnswer[AnsNumb];//Array for random Studen Answers

	int count = 0, total = 0; 


	ifstream inputfile;

	inputfile.open("testanswers.txt");

	while (count < AnsNumb && inputfile >> CorrectAnswer[count])
	{
		count++;
	}

	inputfile.close();

	inputfile.open("studentanswers.txt");

	while (count < AnsNumb && inputfile >> StudentAnswer[count])
	{
		count++;
	}

	inputfile.close();


	for (int index = 0; index < AnsNumb; index++)
	{
		if (StudentAnswer[index] != CorrectAnswer[index])
		{
			total++;

		}
	}



	cout << "Student missed " << total << " questions.\n";

	return 0;


}


Here are the files. They have to be in this format apparently. I can get it to work if the numbers are not included. What I need to do is skip the numbers and spaces and read only the characters of each file.

Correct Answers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 B
2 D
3 A
4 A
5 C
6 A
7 B
8 A
9 C
10 D
11 B
12 C
13 D
14 A
15 D
16 C
17 C
18 B
19 D
20 A


Random Answers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 D
2 B
3 B
4 C
5 C
6 A
7 A
8 C
9 D
10 B
11 A
12 B
13 A
14 C
15 D
16 D
17 D
18 B
19 A
20 A


I would just like to know how I can skip the integers and white spaces and only store characters in the array. Any help would be appreciated. Thank you.
bump?
So are you asking how do you skip the numbers and read in the alpha characters?

I only know of a couple ways to do this:
the easiest thing to do would be just to read in every thing using the ifstreamVariable get() if the character isn't alpha don't store it in the array.

another way to go about is to read in each whole line, and use the find function to find the first "space"(" "). Once it finds it, substr the portion of the line you want into the array.

from as far as I know, it isn't really possible just to "skip" data that is stored in files, you can junk it (option one) or that what you need from the data (in your case the characters)
Thanks for your reply. I was also thinking about possibly doing a 2D array and comparing the second column only. Would this be a valid solution?
Tried the 2D array and it did not work. The program is trying to store integers into a character array and is producing weird ASCII symbols. How would I use the get() function to access only the Alpha Characters?
hm, well the 2D array would work if you wanted to store both the integers and the characters, but what i was thinking for option one was something like:
1
2
3
4
5
6
7
8
9
10
int i = 0;
char grades[20],data;

while (infile){
    data = infile.get();
    if(isalpha(data)){ // making sure the data with in data is alpha character and not a space or a integer
        grades[i] = data;
        i++;
    }
}

of course you would use your variable names, but that is the first option I was talking about
I tried this and it seemed to work however when I tested it by outputting it, it only produces spaces. Here is my modified code:

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

using namespace std;

int main()
{
	const int AnsNumb = 20;

	char CorrectAnswer[AnsNumb];//Array for Correct Answers

	char StudentAnswer[AnsNumb];//Array for random Studen Answers

	char data;

	int count = 0;

	bool AnswersEqual = true;


	ifstream inputfile;

	inputfile.open("testanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			CorrectAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();

	
	

	inputfile.open("studentanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			StudentAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();

	for(int index = 0; index < AnsNumb; index++)
	{
		cout << CorrectAnswer[index] << endl;
	}




	return 0;
}



Compiles fine but doesnt give me the Characters. I dont know if I might be doing something wrong. Thanks for your help though.
Hi, first post here and I am a newbie, but I kind of did this for my school HW last night.

I think you are looking for a variation of this.

1
2
3
4
5
6
7
8
while(!file.eof()) { // till the end of the file
	file.seekg(2L, ios::beg); // starting at the second character
	for(int offset = 2L; offset <= 10; offset += 2) { // after every loop, jump two characters
		getline(file, line, ' '); // getline till a space
		x = atoi(line.c_str()); // I needed to convert my string to a number
		total5 += x; // then add
	}
}


there was more to this, but figured this would be enough to help a little

EDIT: at work, but thought of helping a bit more.

maybe something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
inputfile.seekg(0L, ios::beg);
for( int i = 0; !inputfile.eof(); ++i) {
     if(i > 9){
          inputfile.seekg(3L, ios::cur);
          getline(inputfile, line, '\n');
          studentarray[i] = line;
     }
     else {
          inputfile.seekg(2L, ios::cur);
          getline(inputfile, line, '\n');
          studentarray[i] = line;
     }
}


I'm still at work and have no way of checking if it works, but I hope it does.
Last edited on
wh33lybrdy

try changing your display loop to:
1
2
3
4
	for(int index = 0; index < AnsNumb; index++)
	{
		cout <<StudentAnswer[index] << endl; // you had CorrectAnswer[index], remember the loop above stores the letters in the StudentAnswer array
	}


if it still doesn't work, let me know, and I will look farther into it for you ^_^
Yeah I realized after I posted that it was the wrong array. Fixed it but its still the same problem. Project isnt due until midnight tommorrow so I'll ask my professor about it in class tomorrow and update for everyone else who may have trouble. Thanks
lol so, I didn't understand why your code wasn't working, so I went over it a couple times.

The issue is:

in your second program you are using count for two different loops, all you need to do is assign the value of 0 to count right before you use it in the second loop.
So for your second program on line 40 or something just write:
 
count = 0;


the code is working, your just assigning values to the wrong spot of the array, then when you loop to view the data, you are looking in the indexes (0,1,2,3,4,etc), but you assigned the values in indexes(21,22,23,24,etc)(because yo didn't reset count)

try doing that, let me know what happens
Tried it again with what you suggested. Still just getting a blank output for 20 elements.
message me, or post the code you have, I ran what you have above what some changes (like including the cstdlib), and it worked.
Let me see what you currently have.
this is what I just ran:

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

using namespace std;

int main()
{
	const int AnsNumb = 20;

	char CorrectAnswer[AnsNumb];//Array for Correct Answers

	char StudentAnswer[AnsNumb];//Array for random Studen Answers

	char data;

	int count = 0;

	bool AnswersEqual = true;


	ifstream inputfile;

	inputfile.open("testanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			CorrectAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();

	
	count = 0;

	inputfile.open("studentanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			StudentAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();

	for(int index = 0; index < AnsNumb; index++)
	{
		cout << CorrectAnswer[index] << endl;
	}




	return 0;
}



Uses these files as input:

Correct Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 B
2 D
3 A
4 A
5 C
6 A
7 B
8 A
9 C
10 D
11 B
12 C
13 D
14 A
15 D
16 C
17 C
18 B
19 D
20 A



Randomly Generated Answers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 D
2 B
3 B
4 C
5 C
6 A
7 A
8 C
9 D
10 B
11 A
12 B
13 A
14 C
15 D
16 D
17 D
18 B
19 A
20 A
I just ran your code, I just added cstdlib, but other then that I made no changes:
this is what I ran
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
	const int AnsNumb = 20;

	char CorrectAnswer[AnsNumb];//Array for Correct Answers

	char StudentAnswer[AnsNumb];//Array for random Studen Answers

	char data;

	int count = 0;

	bool AnswersEqual = true;


	ifstream inputfile;

	inputfile.open("testanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			CorrectAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();


	count = 0;

	inputfile.open("studentanswers.txt");

	while (inputfile)
	{
		data = inputfile.get();
		if(isalpha(data))
		{
			StudentAnswer[count] = data;
			count++;
		}
	}

	inputfile.close();

	for(int index = 0; index < AnsNumb; index++)
	{
		cout << CorrectAnswer[index] << endl;
	}

	return 0;
}

my output was

B
D
A
A
C
A
B
A
C
D
B
C
D
A
D
C
C
B
D
A

Process returned 0 (0x0)   execution time : 0.018 s
Press any key to continue.


:-/ I'm not getting no empty spaces.

I'm not sure if you tried this already and I'm not sure what compiler you are using.
but I would make sure the compiler is building your new code, sometimes restarting it works.

I can't see a reason for you to still be getting empty spaces for output, unless your compiler is running your last successful build and isn't recompiling.

also try using a different compiler

I guess try include <cstdlib> (or <cstdlib.h>) (depending on your compiler)
I think I see the problem. Im using a MAC so its a UNIX compiler. Dont know if that has anything to do with it but I did run your exact code and still got spaces.
Ah, I have a friend that programs on a mac as well and we occasionally run into compiler issues. But for code like you have above usually doesn't cause many problems.

Well, if you wanted you could try installing codeblocks (ide/complier) that usually does the trick for him.But never (100%)

Good Luck, and sorry about you still getting spaces
No problem. I will just discuss it with the professor tomorrow as I'm sure others will have the same problem. Thanks for really trying to figure this out with me. I'll let you know what fixes it.
Fixed..

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>

using namespace std;

void createfile();

int main()
{
	createfile();
	const int AnsNumb = 20;
	string line;
	string CorrectAnswer[AnsNumb];//Array for Correct Answers
	string StudentAnswer[AnsNumb];//Array for random Studen Answers
	int count = 0, total = 0; 

	ifstream inputfile;
	inputfile.open("testanswers.txt");

	inputfile.seekg(2L, ios::beg);
	for( int i = 0; i < AnsNumb; ++i) {
		if(i > 9){
			getline(inputfile, line, '\n');
			CorrectAnswer[i] = line;
			inputfile.seekg(3L, ios::cur);
		}
		else {
			getline(inputfile, line, '\n');
			CorrectAnswer[i] = line;
			inputfile.seekg(2L, ios::cur);
		}	
}

	inputfile.close();
	inputfile.open("studentanswers.txt");

	inputfile.seekg(2L, ios::beg);
	for( int i = 0; i < AnsNumb; ++i) {
		if(i > 9){
			getline(inputfile, line, '\n');
			StudentAnswer[i] = line;
			inputfile.seekg(3L, ios::cur);
		}
		else {
			getline(inputfile, line, '\n');
			StudentAnswer[i] = line;
			inputfile.seekg(2L, ios::cur);
		}	
}

	inputfile.close();

	for (int index = 0; index < AnsNumb; index++)
	{
		if (StudentAnswer[index] != CorrectAnswer[index])
		{
			total++;
		}
	}

	cout << "Student missed " << total << " questions.\n";

	cin.ignore();
	return 0;
}

void createfile()  {
	const int numquestions = 20;
	char studanswer[numquestions];
	srand((int)time(0));
	int numb[numquestions] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

	ofstream outputfile;

	//Stores the Array
	for (int index = 0; index < 20; index++)
	{
		studanswer[index] = 65 + rand()% 4;
	}

	//Opens the file
	outputfile.open("studentanswers.txt");

	//Writes to the file
	for (int index = 0; index < numquestions; index++)
	{
		outputfile << numb[index] << " " << studanswer[index] << endl;
	}

	//Closes file
	outputfile.close();

	cout << "The numbers were saved to a file\n";

	cin.ignore();
	return;
}


I added <ctime> to make the student answers truely random. double and triple checked to make sure this worked.
Last edited on
Topic archived. No new replies allowed.