Given a multiple-choice test of 10 questions with 5 possible choices (A-E). The answers are given in a text file a long with the student names. Write a C++ program that does the following:
1.Reads the key answers to an array from the input file. The key is the first line in the file
2.For each student read his/her answers and compare their answers with the key array
3.Stores each the answers of each student in an array of (chars)
4.Sends this array to a function to compute number of correct answers per student
5.Computes % of correct answers for each student
6.Stores the scores in an array of (ints)
7.Displays the list of the students along with their scores (counts and percentages)

My code is not reading the names and grades correctly. I am also unsure of how to calculate the grades of the students. Here is the beginning of my code below.

# include <cmath>
# include <iostream>
# include <iomanip>
# include <cstring>
# include <cctype>
# include <fstream>
# include <string>
using namespace std;
int main()
{
string x;
string temp[1] = {x};
char y = 0;
char key[10] = {y};

string a;
string name[1] = {a};
char b = 0;
char grade[10] = {b};

ifstream fin;
fin.open("fin.txt");
 
for( int i = 0; i < 10; ++i)
{
   fin >> x;
   fin >> y;
 
}
 
while (fin)
{
    for( int j = 0; j < 10; ++ j)
    {
      fin >> a;
      fin >> b;
       
      if (grade[0] = key[0] )
          ;
      if (grade[1] = key[1] )
          ;
      if (grade[2] = key[2] )
          ;
      if (grade[3] = key[3] )
          ;
      if (grade[4] = key[4] )
          ;
      if (grade[5] = key[5] )
          ;
      if (grade[6] = key[6] )
          ;
      if (grade[7] = key[7] )
          ;
      if (grade[8] = key[8] )
          ;
      if (grade[9] = key[9] )
          ;
   
      // Need some type of function to cout their grade
    }
   

   
   
}

system ("pause");
return 0;
}
string x;
string temp[1] = {x};
char y = 0;
char key[10] = {y};

string a;
string name[1] = {a};
char b = 0;
char grade[10] = {b};

This code doesn't make much sense. First off, there is no advantage of declaring an array with a size of one; it is in effect a the same as a single variable. Second, you are assigning to this array a string value that isn't initialized, so you are in effect getting garbage for the assigned value. Third, you are then assigning a single value (again, uninitialized) to a 10-element array, which AFAIK shouldn't compile as an initialization at all.

I suspect that the problem is that you are confusing the mathematical concept of equality with the programming concepts of assignment and initialization. THis seems borne out by the code following it:

for( int j = 0; j < 10; ++ j)
    {
      fin >> a;
      fin >> b;
       
      if (grade[0] = key[0] )
          ;
      if (grade[1] = key[1] )
          ;
// ...

Where you similarly confuse assignment ('=') and comparison ('==').

When you assign (or initialize, as you have it here) a variable with the value of another variable, it does not create any continued connection between them; it is not saying 'this value is the same as this one', but rather, 'take this value on the right and put it in the variable on the left'. Changing the variable on the right later has no effect on the one on the left.

I used the array with size one because I needed to read the name of the key, and the name of the student. My teacher told me to assign a temp string to do that. The file that I am reading has spaces between the name and grades. The first line is "Key A B C...." and the students are "Joe C C C...." I thought by saying it equaled zero meant that I was initializing it. When I changed my code to == instead of =, it gave me the same result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.