C Multiple Choice exam program

This is a discussion on C Multiple Choice exam program within the C++ Programming forums, part of the General Programming Boards category; Hello, i needed to solve this question: Write a program to grade a multiple choice exam. The exam has 20 ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    C Multiple Choice exam program

    Hello, i needed to solve this question:

    Write a program to grade a multiple choice exam. The exam has 20 questions, each answered with a little in the range of a through f. The data are stored on a file (exams.dat) where the first line is the key consisting of a string of 20 characters. The remaining lines on the files are exam answers, and consist of a student ID number, a space, and a string of 20 characters. The program should read the key, then read each exam and output the ID number and score to file scores.dat. Erroneous input should result in an error message. For example, given the data:

    abcdefabcdefabcdefab
    1234567 abcdefabcdefabcdefab
    9876543 abddefbbbdefcbcdefac
    5554446 abcdefabcdefabcdef
    4445556 abcdefabcdefabcdefabcd
    3332221 abcdefghijklmnopqrst

    The program should output on scores.dat:

    1234567 20
    9876543 15
    5554446 Too few answers
    4445556 Too many answers
    3332221 Invalid answers

    I came across this c++ code but am unsure on how to fully convert it to C. Please help
    Code:
    
    #include <string>#include <fstream>
    
    using namespace std;
    
    void WrongSize (ofstream&, int, string, string);
    
    int main()
    {
    ifstream inData;
    ofstream outData;
    inData.open("exam.dat");
    outData.open("scores.dat");
    
    string key;
    string student;
    string answers;
    int keylength;
    
    inData >> key;
    keylength = key.length();
    
    do
    {
    inData >> student;
    outData << student<< " ";
    inData >> answers;
    WrongSize (outData, keylength, answers, key);
    
    }
    while (!inData.eof());
    
    return 0;
    }
    
    
    void WrongSize (ofstream& outData, int keylength, string answers, string key)
    {
    
    int grade = 0;
    if (answers.length() < keylength)
    {
    outData << "Too few answers";
    }
    else if (answers.length() > keylength )
    {
    outData << "Too many answers";
    }
    else
    {
    bool check=false;
    
    for (int count = 0; count < key.length(); count++)
    {
    if( answers[count] == key[count] )
    grade++;
    else if (answers[count] != 'a' && answers[count] != 'b' && answers[count] != 'c' && answers[count] != 'd' && answers[count] != 'e' && answers[count] != 'f')
    check=true;
    }
    
    if (check==true)
    {
    outData << "Invalid Answer";
    }
    else
    {
    outData << grade;
    }
    }
    outData << endl;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > I came across this c++ code but am unsure on how to fully convert it to C. Please help
    Sure, you use file explorer to locate your .cpp file, you then press the right mouse button to bring up the context menu and then you select the 'delete' option.

    Then you sit down and try and write your OWN code for a change, instead of thinking you can get by through life through google searches and begging others to do your work for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-20-2012, 12:05 AM
  2. Help Needed Fast! Multiple Choice Grading Program
    By namyad in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2010, 10:17 AM
  3. Multiple-Choice Exam Program
    By micmagicfly in forum C++ Programming
    Replies: 7
    Last Post: 03-26-2010, 01:17 PM
  4. multiple choice
    By iLike in forum C Programming
    Replies: 6
    Last Post: 10-30-2009, 03:53 PM
  5. Multiple Choice!!!
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-08-2002, 02:30 PM

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