0

Okay, so I know that you guys aren't my own personal programmers but I was wondering if you could help me out a bit here. I work as a teachers assistant while I'm going to school. I know a bit of C++ and was trying to make a basic program to check answers with.

The first part of the program works fine to test the answers against the key, however the second part is what I'm having issues with. I'm trying to find how many students answered what question so I don't have to count them by hand, but I cannot seem to get this down.

What I really need is to be able to print them out like so:

question A B C D E

1 1 12 3 5 7

and so on.

I know I'm probably going to need a nested for loop, but I've been banging my head on this all night. Any help would be appreciated.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void main(){

int numstud=0, numcorrect, i;
string key, id, responses;
ifstream keyfin, studfin;
keyfin.open("answers.dat");
studfin.open("tests.dat");

keyfin >> key;
studfin >> id >> responses;

cout << setw(5) << "Student-Id's" << setw(20) << "# Correct" << endl << endl;

while(!studfin.eof())
{
    numstud++;
    numcorrect=0;
    for(i=0; i<20; i++){

        if(responses[i] == key[i])
        {
            numcorrect++;
        }
    }

    cout << id << setw(20) <<numcorrect << endl;
    studfin >> id >> responses;

}








studfin >> id >> responses;
int col[20][5]={0}, j=0;
char ch;
studfin >> id >> responses;
cout << "Question" << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw        (5) << "D" << setw(5) << "E";

while (!studfin.eof())
{
    for(i=0; i<responses.length(); i++)
    {
        col[i++][responses[i-'a'] ++];

    }


}
}
Skathix
  • 278
  • 1
  • 3
  • 14
  • 1
    `void main` should be `int main` and [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – chris Apr 16 '14 at 23:43
  • Would you mind being more specific as to why? – Skathix Apr 16 '14 at 23:45
  • `col[i++][responses[i-'a'] ++]` <- possibly undefined behavior – user657267 Apr 16 '14 at 23:45
  • How is your data stored? – yizzlez Apr 16 '14 at 23:45
  • Data is stored in a .txt file, sorry haven't gotten around to the ofstream statement, was just testing beforehand. Also I came to Stack when I was ready to throw the monitor, so the second half of the code isn't complete, as I deleted most of what I had there. – Skathix Apr 16 '14 at 23:48
  • `Okay, so I know that you guys aren't my own personal programmers but I was wondering if you could help me out a bit here` I can't be sure but are you saying "I know you don't do X, but can you do X just this one time?" Because I don't see a question here beyond that. – Lightness Races in Orbit Apr 16 '14 at 23:49
  • Indeed, that is basically what I'm asking, for someone to help me complete this program. I apologize that was supposed to be clear, thank you for pointing this out. – Skathix Apr 16 '14 at 23:50
  • @Skathix, The language says the return type of `main` must be `int`. Nothing more to it than that. As for the other point, the link goes into great specifics. – chris Apr 16 '14 at 23:53

1 Answers1

0

You have the general idea as far as implementing an histogram of the responses. However there are a few things wrong with the line col[i++][responses[i-'a'] ++].

  • First assuming responses are lower case, you probably intended responses[i]-'a' rather than responses[i-'a'], to convert the letter grade to a 0,1,2,3,4 index.
  • Then, it's the content of the table you want to increment, not row and column indices. That is col[i][responses[i]-'a']++.

As a side note, you don't need a second while (!studfin.eof()) loop to update the histogram. You can keep updating it as you are checking answers against the key (and you should not use a second loop unless you reset the input stream that has reached end-of-file by the end of the first loop).

SleuthEye
  • 14,379
  • 2
  • 32
  • 61