Reading from file to an array and computing the grades

This is a discussion on Reading from file to an array and computing the grades within the C++ Programming forums, part of the General Programming Boards category; Given a multiple-choice test of 10 questions with 5 possible choices (A-E). The answers are given in a text file ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8

    Reading from file to an array and computing the grades

    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.

    Code:
    # 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;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Code:
    ifstream fin;
    fin.open("fin.txt");
     
    for( int i = 0; i < 10; ++i)
    {
       fin >> x;
       fin >> y;
    }
    I'm surprised that fin >> x or fin >> y didn't give you an error. It looks like you opened the file right but you are trying to store the arrays wrong.

    Look into the ifstream function getline to correctly get data.
    istream::getline - C++ Reference
    You have to convert the value returned from that to an integer value with the atoi function
    atoi - C++ Reference

    I'll leave you with that for now.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8
    The reason I haven't used the getline function is because that isn't what my teacher uses. He always used the "fin" function. I didn't want to use anything he hasn't taught us. And as for the atoi function, we haven't learned that at all. I can only use what I've already been taught. I really feel that I'm making it harder than it is.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Code:
    string temp[1] = {x};
    This simply assigns the value of x (which, at this point, is undefined) to temp (which is just an array of one elements, that is to say, not really an array at all!). This is obviously non-sensical. What is your intention here?
    >>if (grade[0] = key[0] )
    Also some heads up, but this is assignment, not comparison. Comparison requires two =.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8
    Ok thanks for you help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With Basic Program (computing grades)
    By jsokol7 in forum C++ Programming
    Replies: 18
    Last Post: 06-21-2011, 11:15 AM
  2. Computing the sum of each diagonal in an array
    By Seeshi_suin in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 10:41 PM
  3. help! reading from file and computing average
    By cakestler in forum C Programming
    Replies: 22
    Last Post: 02-13-2009, 03:32 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM

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