I am writing code for a C++ class. I have tried a couple of different ways to write it, but I keep failing. Here is what I have so far and also what the assignment asks us to do. Please help

You have been asked to write a program to grade a multiple-choice exam. The exam has 20 questions, each answered with a letter in the range 'a' through 'f'. The data are stored in a file called exams.txt where the first line is the key, consisting of a string of 20 characters. The remaining lines in the file are exam answers; each consists of a student ID number, a space, and a string of 20 characters. The program should read the key, read each student ID and exam answers, and output the ID number and the number of correct answers to a file called scores.txt. 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 would output the following data in scores.txt

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 formatted neatly, and the error messages should be informative.


Also, I know this is a stupid question, but I am a newbie. When I am doing in data files and out data files, how do I save my data to those files and have them open when I run my program?

#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
 
bool WrongSize (ofstream& outData,int keylength,string answers)
{
    bool valid = false;
    if (answers.length() < keylength)
    {
         outData << "Too few answers!\n";
    }
    else if (answers.length() > keylength )
    {
        outData << "Too many answers!\n";
    }
    else
         valid = true;
    return valid;
}
 
int main()
{
  < snip >
 
  if( WrongSize (outData, keylength, answers) == true )
  {
	for( int count = 0; count < key.length(); count++)
       {
             if( answers[count] == key[count] )
                  grade++;
             else
                  outData << "Invalid answers" ;
}
  }
}

Recommended Answers

All 4 Replies

Following up to mzimmers,

... or your WrongSize() function is mis-named. While it might function completely correctly, it isn't very readable to have WrongSize() return true when the data is the RightSize and false when it's not.

In addition, your logic at lines 33-34 is incorrect. You need a different comparison(s) of answers[count] to determine that the answers are invalid, and you want to output that message only once (not for each invalid character encountered). Since the assignment calls for functional decomposition, consider writing functions called ValidAnswers() and NumberOfCorrectAnswers(). You'll find the program almost writes itself, if you think about the logical breakdown of steps, and in what order to perform those steps.

Cheers, and welcome to DaniWeb!

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.