char inFile to Array

I am developing a program that takes 20 multiple choice answers from a student's test and compares it to an array consisting of the correct answers. I want to take the values as a char and use if else statements to determine the students score (out of 20) by comparing to correct answer. I need help taking the values from an input text file and loading them into an array. Is there a relatively simple way to accomplish this?

The input file looks something like this:

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


Hello WakelessFoil,

As a start you may find this helpful. http://www.cplusplus.com/forum/beginner/273685/

Andy
If you search these forums, you'll find that this question has been asked and answered previously.
Hello WakelessFoil,

I am developing a program that takes 20 multiple choice answers from a student's test and compares it to an array consisting of the correct answers.

I am developing an answer, but without any other input I do not know what to put in the answer.

If this is a school project or assignment. Start by posting the directions so everyone will know what yo need to do. This may also avoid any answers that would be more advanced than what you know for now.

If you took any time to plan the program. Anything that you have may be useful followed by any code you have written.

That is a place to start. Having something to work with will produce better responses.

This may also be helpful: http://www.catb.org/~esr/faqs/smart-questions.html

I have not compared these links, but they look about the same.
https://d.cxcore.net/Eric%20S%20Raymond/How%20To%20Ask%20Questions%20The%20Smart%20Way.pdf

I will do what I can, but you need to provide more information and something to work with.

Andy
So you are probably gonna laugh at my attempt to make this program. I am a beginner but it took 700 lines for me to make it work. I do not understand the structure of functions very well or how to use them. I am sure it would make this script much shorter.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>
#include <fstream>

using namespace std;


int main()
{
    //declare variables
    string D1name, D2name, D3name;
    string passfail;

    const int number_of_questions = 20;
    char corans[21]={'-', 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
    char D1ans[40];
    char D2ans[40];
    char D3ans[40];
    int D1score, D2score, D3score;

    //initialize scores to 0
    D1score = 0;
    D2score = 0;
    D3score = 0;

    //declare and open input files
    fstream inFile;  //declare input files


    //program title
    cout << "-----------------------------------------" << endl;
    cout << "Driver's License Exam Grading Application" << endl;
    cout << "-----------------------------------------" << endl << endl;

    //table
    cout << "Name                               Answers                      Correct  Incorrect  Pass/Fail" << endl;
    cout << "---------------------------------------------------------------------------------------------" << endl;

    ///load answers in from file
    ///extra input values take up the return character between answers
    inFile.open("driver1.txt");
    getline(inFile, D1name);
    inFile.get(D1ans[1]);
    inFile.get(D1ans[2]);
    inFile.get(D1ans[3]);
    inFile.get(D1ans[4]);
    inFile.get(D1ans[5]);
    inFile.get(D1ans[6]);
    inFile.get(D1ans[7]);
    inFile.get(D1ans[8]);
    inFile.get(D1ans[9]);
    inFile.get(D1ans[10]);
    inFile.get(D1ans[11]);
    inFile.get(D1ans[12]);
    inFile.get(D1ans[13]);
    inFile.get(D1ans[14]);
    inFile.get(D1ans[15]);
    inFile.get(D1ans[16]);
    inFile.get(D1ans[17]);
    inFile.get(D1ans[18]);
    inFile.get(D1ans[19]);
    inFile.get(D1ans[20]);
    inFile.get(D1ans[21]);
    inFile.get(D1ans[22]);
    inFile.get(D1ans[23]);
    inFile.get(D1ans[24]);
    inFile.get(D1ans[25]);
    inFile.get(D1ans[26]);
    inFile.get(D1ans[27]);
    inFile.get(D1ans[28]);
    inFile.get(D1ans[29]);
    inFile.get(D1ans[30]);
    inFile.get(D1ans[31]);
    inFile.get(D1ans[32]);
    inFile.get(D1ans[33]);
    inFile.get(D1ans[34]);
    inFile.get(D1ans[35]);
    inFile.get(D1ans[36]);
    inFile.get(D1ans[37]);
    inFile.get(D1ans[38]);
    inFile.get(D1ans[39]);
    inFile.get(D1ans[40]);

///check driver1's answers
if (D1ans[1] == corans[1])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[3] == corans[2])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[5] == corans[3])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[7] == corans[4])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[9] == corans[5])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[11] == corans[6])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[13] == corans[7])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[15] == corans[8])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[17] == corans[9])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[19] == corans[10])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[21] == corans[11])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[23] == corans[12])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[25] == corans[13])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[27] == corans[14])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[29] == corans[15])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[31] == corans[16])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[33] == corans[17])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[35] == corans[18])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[37] == corans[19])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }
if (D1ans[39] == corans[20])
    {
        D1score = D1score + 1;
    }
    else
    {
        D1score = D1score + 0;
    }

    if (D1score > 15)
    {
        passfail = "PASSED";
    }
    else
    {
        passfail = "FAILED";
    }
    cout << D1name << "        " << D1ans[1] << " " << D1ans[3] << " " << D1ans[5] << " " << D1ans[7] << " " << D1ans[9]
                               << " " << D1ans[11] << " " << D1ans[13] << " " << D1ans[15] << " " << D1ans[17] << " " << D1ans[19]
                               << " " << D1ans[21] << " " << D1ans[23] << " " << D1ans[25] << " " << D1ans[27] << " " << D1ans[29]
                               << " " << D1ans[31] << " " << D1ans[33] << " " << D1ans[35] << " " << D1ans[37] << " " << D1ans[39]
                               << "            " << D1score << "          " << number_of_questions - D1score << "     " << passfail << endl;

///do the same for the other two drivers
    inFile.close();
    inFile.open("driver2.txt");
    getline(inFile, D2name);
    inFile.get(D2ans[1]);
    ///.......
return 0;
}


The input files look just like this:

John Smith
B
D
A
A
C
A
B
A
C
D
B
C
C
C
C
C
C
C
C
C
Last edited on
@WakelessFoil,
If you find yourself writing essentially the same thing 40 times ... it's time to learn about for-loops.
http://www.cplusplus.com/doc/tutorial/control/

Your character arrays might be more easily handled as strings as well:
string corans="BDAACABACDBCDADCCBDA";
You can still address them the same way.

You don't actually need to store the prospective driver's answers in order to score them - just compare them one by one with the model answer. Which, of course, requires you to use for-loops.
Last edited on
For your starter, consider:

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

int main()
{
	const std::string correct {"BDAACABACDBCDADCCBDA"};
	const std::string fnames[] {"driver1.txt", "driver2.txt", "driver3.txt"};

	for (const auto& fn : fnames) {
		std::ifstream ifs(fn);

		if (!ifs.is_open())
			std::cout << "Cannot open file " << fn << '\n';
		else {
			std::string name;
			size_t corr {}, bad {};

			std::getline(ifs, name);

			for (unsigned char got {}, grade {}; got < correct.size() && (ifs >> grade); ++got)
				grade == correct[got] ? ++corr : ++bad;

			std::cout << name << "  " << corr << " correct  " << bad << " incorrect  " << std::fixed << std::setprecision(2) << corr / (corr + bad + 0.0) * 100 << "%\n";
		}
	}
}


which for my test files displays:


driver 1  6 correct  14 incorrect  30.00%
driver 2  14 correct  6 incorrect  70.00%
driver 3  20 correct  0 incorrect  100.00%

Last edited on
I need help taking the values from an input text file and loading them into an array.


Using a .txt file exactly as you specified this is the way to do what you ask. You can extend all this to do what you have to do in the future. There are many posibilities including adding the questions to the same file ... and so it goes 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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>

// SEE: https://www.cplusplus.com/doc/tutorial/files/ // <---

using namespace std;

int main ()
{
    ifstream myfile ("q_and_a.txt");
    
    if (!myfile.is_open())
    {
        cout << "Unable to open file";
        return -9;
    }
    char Answer[50]; // ALLOW PLENTY OF SPACE
    char answer;
    
    // READ FROM FILE AND PUT IN ARRAY
    int count{0};
    while ( myfile >> answer )
    {
        Answer[count] = answer;
        count++;
    }
    int NO_OF_ANSWERS = count;
    myfile.close();
    
    // DISPLAY ANSWERS
    for(int ans_no = 0; ans_no < NO_OF_ANSWERS; ans_no++)
    {
        cout << "Answer No: " << ans_no << " Answer: " << Answer[ans_no] << '\n';
    }
    
    return 0;
}
Hello WakelessFoil,

Looking over your code I noticed the are some big parts the need worked on and some little things.

As a start:
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
#include <iostream>
#include <string>  // <--- Needed for "std::string" and "getline()" along with some of the "cout" statements.

#include <fstream>

using namespace std;


int main()
{
    constexpr int MAXSIZE{ 21 };
    const int number_of_questions = 20;  // <--- Could just use MAXSIZE.

    //declare variables
    string D1name, D2name, D3name;
    string passfail;

    char corans[MAXSIZE] = { '-', 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
    char D1ans[MAXSIZE]{'-'};
    char D2ans[MAXSIZE]{ '-' };
    char D3ans[MAXSIZE]{ '-' };
    int D1score{}, D2score{}, D3score{};

    //initialize scores to 0  // <--- Not needed if you initialize the variables when defined.
    //D1score = 0;
    //D2score = 0;
    //D3score = 0;

    //declare and open input files
    fstream inFile;  //declare input files


    //program title
    cout << "-----------------------------------------\n";
    cout << "Driver's License Exam Grading Application\n";
    cout << "-----------------------------------------\n\n";

    //table
    cout << "Name                               Answers                      Correct  Incorrect  Pass/Fail" << endl;
    cout << "---------------------------------------------------------------------------------------------" << endl;

    ///load answers in from file
    ///extra input values take up the return character between answers
    inFile.open("driver1.txt");

    getline(inFile, D1name);

You are good to this point with what I have noted in the code.

You have defined "std::string"s and used "getline()" both of which need the header file <string> to work properly.

The string D1name, D2name, D3name; may work because of a header that is included that you do not know about, but this does not mean that it will work properly.

Lines 19 - 21 are initialized with the first element being "-" and all the other elements are set to "\0". This eliminates the garbage that the array would initially hold. Also the "\0" can be used later in the program.

I do have a question. If correct answers only has 20 letters and the file only has 20 lines to read, What is the extra space for? Your arrays only need to hold 21 user answers.

Line 22 is OK, but you could use an array especially if the number of files increases in the future.

After this lastchance's suggestions of using a "std::string is good. The string can be accessed just like your "char" arrays with less work and more advantages.

Next would lines 42 - 81 in your last code. This cries for a for loop. Which you could do in as little as 2 lines with a for loop. Even a while loop could be done in 2 lines.

All the if statements after the first 1 are wrong. With your arrays starting at 1 instead of (0) zero, which is fine, the first if statement makes the correct comparison. All the rest are off because the correct answers array and what is read from the file are set up to match. Comparing if (D1ans[3] == corans[2]) is more likely not to match and evaluate to "false" than to be "true".

And in the next if statement if (D1ans[5] == corans[3]). Now you are off by 2.

These if/else statements scream for a for loop.

Also the else part is not needed because in the last output statement you figure the incorrect answers based on the correct amount.

That alone would shorten the if statements. Also D1score = D1score + 0; really does not do anything and the other way D1score = D1score + 1; can easily be written as D1score++;. It still just adds 1 without all the extra work. Using the "++" or "--" as a prefix or suffix the more preferred way of using this shorthand as is the others "+=", "-=", "*=" and "/=". These are the most used although there are others.

In addition to lastchance's link This is a good one https://www.learncpp.com/cpp-tutorial/57-for-statements/

Some where around line 268 the "cout" prints all the odd indices of your array and includes elements that should not be printed because they were never used and you may or may not know what they contain.

You could print the name followed by a short for loop to print the users answers the whatever is left.

Andy
@WakelessFoil
I didn’t see it at the time, you changed it, but you need an additional line before the while statement line to read the person’s name in. That can be in 2 parts, first and family name, both as strings or using getline as a single string (containing the existing space between them).

In terms of arrays it changes things slightly in that the name and corresponding results can be tied together using the count variable to act as an index for parallel arrays. You would read in first to name_array[x] then read into a 2d array result_array[x][i] where i goes from 0 to 20
Last edited on
And this is what that looks like:
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
#include <iostream>
#include <fstream>
#include <string>

// SEE: https://www.cplusplus.com/doc/tutorial/files/ // <---

using namespace std;

int main ()
{
    const int NO_OF_STUDENTS = 50;  // ALLOW FOR PLENTY OF NAMES
    const int NO_OF_ANSWERS = 20;
    
    ifstream myfile ("q_and_a.txt");
    
    if (!myfile.is_open())
    {
        cout << "Unable to open file";
        return -9;
    }
    
    string Name[NO_OF_STUDENTS];
    string Last_Name[NO_OF_STUDENTS];
    
    char Answer[NO_OF_STUDENTS][NO_OF_ANSWERS]; // 2D ARRAY
    
    
    // READ FROM FILE AND PUT IN ARRAYS
    int count{0};
    int ans_no{0};
    
    // https://www.cplusplus.com/reference/string/string/getline
    while( getline( myfile, Name[count]) )
    {
        ans_no = 0;
        while ( myfile >> Answer[count][ans_no] && count < NO_OF_ANSWERS)
        {
            ans_no++;
        }
        count++;
    }
    myfile.close();
    
    
    // DISPLAY STUDENTS & THEIR ANSWERS
    for(int i = 0; i < count; i++)
    {
        cout << Name[i] << '\n';
        for(int ans_no = 0; ans_no < NO_OF_ANSWERS; ans_no++)
        {
            cout << "Answer No: " << ans_no << " Answer: " << Answer[i][ans_no] << '\n';
        }
    }
    
    return 0;
}




John Smith
Answer No: 0 Answer: A
Answer No: 1 Answer: D
Answer No: 2 Answer: B
Answer No: 3 Answer: C
Answer No: 4 Answer: C
Answer No: 5 Answer: D
Answer No: 6 Answer: A
Answer No: 7 Answer: D
Answer No: 8 Answer: B
Answer No: 9 Answer: D
Answer No: 10 Answer: B
Answer No: 11 Answer: C
Answer No: 12 Answer: C
Answer No: 13 Answer: D
Answer No: 14 Answer: A
Answer No: 15 Answer: D
Answer No: 16 Answer: B
Answer No: 17 Answer: C
Answer No: 18 Answer: A
Answer No: 19 Answer: B
Program ended with exit code: 0
Topic archived. No new replies allowed.