Thread: Multiple-Choice Exam Program

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Multiple-Choice Exam Program

    I have to write a multiple choice program and the following code is as far as i've gotten. It's coming back with a value of 0


    You’ve been asked to 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

    Use functional decomposition to solve the problem and code the solution using functions as appropriate. Be sure to use proper formatting and appropriate comments in your code. The output should be neatly formatted, and the error messages should be informative.

    Code:
    #include <iostream>
    #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 Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your program should return 0, since that indicates successful completion of the program (as evidenced by "return 0" in your code). The real question is, what's in your output file.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    The output file simply reads "0". That's what the problem is...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your code says one thing ("exam.dat") while your description says another ("exams.dat").

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Of course it has to be something very simple like that lol. Thanks so much.

    My only problem I have left is the output says:

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

    That last line shouldn't be repeating itself. Thoughts?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by micmagicfly View Post
    That last line shouldn't be repeating itself. Thoughts?
    Don't screw up the eof check? (Remember: eof doesn't happen until after a read has failed; so you need to make sure the read is the last thing that happens before the condition is checked. This is usually done by making the read the actual condition itself, since the read will return true/false for whether it worked or not.)

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    So do I get rid of the eof function..? I'm not sure what i'd use to replace it.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by micmagicfly View Post
    So do I get rid of the eof function..? I'm not sure what i'd use to replace it.
    if you mean this part

    Code:
    do
    {
    inData >> student;
    outData << student<< " ";
    inData >> answers;
    WrongSize (outData, keylength, answers, key);
    
    }
    while (!inData.eof());
    then

    Code:
    while(inData >> student >> answers)
    {
       outData << student<< " ";
       WrongSize (outData, keylength, answers, key);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Organisation in multiple files
    By hilarius in forum C Programming
    Replies: 2
    Last Post: 11-26-2009, 02:20 PM
  2. multiple choice
    By iLike in forum C Programming
    Replies: 6
    Last Post: 10-30-2009, 03:53 PM
  3. Help needed with program - urgent thanks!
    By lildevil in forum C Programming
    Replies: 1
    Last Post: 03-09-2008, 06:45 AM
  4. Replies: 18
    Last Post: 12-05-2003, 12:06 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM