Server Help

Non-Subspace Related Coding - Error in code.

Yoink - Thu Oct 26, 2006 11:16 pm
Post subject: Error in code.
This file reads an input file for a key, and by string comparison, checks student answers.

Edit: Yes, I realise this should be broken down into functions. Whether it's bad methodology or not, my initial goal was to have a working program, rather than have it broken down appropriately. I'll worry about that afterwards.

Here is the input file (named exams.txt). The first line is the key, the following lines are student ID numbers followed by a set of characters (which must be equal to the key in length, and must only contain chars a-f).

Code: Show/Hide
abcdefabcdefabcdefab
1234567 abcdefabcdefabcdefab
9876543 abddefbbbdefcbcdefac
5554446 abcdefabcdefabcdef
4445556 abcdefabcdefabcdefabcd
3332221 abcdefghijklmnopqrst


In my compiler (DevC++) it must be in the same directory as the .cpp file. Anyway, one of the problems that I see is that my counting variable, n, should be set back to zero before or after each while loop. Unfortunately, when I do that (say at the very end of the primary while block), it doesn't compile correctly unless a datatype (int) is declared before it, even though the variable has been declared earlier and has the necessary scope.

There are probably other problems too.

Here are the results I'm getting:

Code: Show/Hide

1234567 20
9876543 15
5554446 Too few answers.
4445556 Too many answers.



The last line is where the insertion marker is. Any help would be appreciated.

Code: Show/Hide

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
    ifstream key;
    ofstream results;
   
    string keyAnswer;
    string stuAnswer;   
   
    int studentID = 9999999;   
    int n = 0;   
    int numCorrect = 0;   
       
    key.open("exams.txt");
    results.open("results.txt");

    key >> keyAnswer;
    key >> studentID;
    key >> stuAnswer;
           
    while ( !key.eof() )
    {
        // checking for invalid input values (letters other than a-f)
        while ( n < keyAnswer.length() )
        {
            string typecheck = stuAnswer.substr(n, 1);
            if ( typecheck > "f" || typecheck < "a" )
            {
                 results << studentID << " ";
                 results << "Invalid answers." << endl;                                 
            }
            n++;   
        }
         
        n = 0;   
       
        // checking for answers with a lack of input values
        if ( stuAnswer.length() < keyAnswer.length() )
        {
             results << studentID << " ";
             results << "Too few answers." << endl;
        }
       
        // checking for answers with too many input values
        else if ( stuAnswer.length() > keyAnswer.length() )
        {
             results << studentID << " ";         
             results << "Too many answers." << endl;             
        }
       
        // the correct number of input values, but how many of them are correct?
        else if ( stuAnswer.length() == keyAnswer.length() )
        {
             while ( n < keyAnswer.length() )
             {
                   string stuCheck = stuAnswer.substr(n, 1);
                   string keyCheck = keyAnswer.substr(n, 1);               
                   
                   if ( stuCheck == keyCheck )
                         numCorrect++;
                                       
                   n++;
             }
   
             results << studentID << " ";         
             results << numCorrect << endl;
             numCorrect = 0;         
        }
       
        key >> studentID;
        key >> stuAnswer;
    }   

    key.close();
    results.close();
       
    cout << "Answers checked.  Please check output file for results.\n";
    system("pause");
    return 0;
}


PS. I guess I want to look at some bot core code (C++). While I'm going to dig around for Mervbot, I'd appreciate it if someone were to post me links to the source, or some other core that I should look at first, if that's the case.
Muskrat - Fri Oct 27, 2006 12:21 am
Post subject:
after you read your last 2 entries, key.eof will be true, cutting off that last go. I modified it to work right and got:
Code: Show/Hide
1234567 20
9876543 15
5554446 Too few answers.
4445556 Too many answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 Invalid answers.
3332221 6


You might want to make that part break after the first Invalid answer or something. Also, why not use for loops? using 1 counter for a few loops will get crazy in any program bigger than this.
Yoink - Fri Oct 27, 2006 12:43 am
Post subject:
That's one of the things I tried. It wasn't compiling correctly. Am I missing a library?

Well, it was compiling fine, but it wasn't running / functioning properly.
Muskrat - Fri Oct 27, 2006 1:00 am
Post subject:
What? your major problem is the organization of your mail while loop and data entry.

You do:
Code: Show/Hide

key >> in;
while(!key.oef()){

   ........
   .................
   .........


   key >> in;//on the last data entry, the instream stops at the end
}


try something like:
Code: Show/Hide

while(true){
   key >> in;

        ............
        ...............
        ..........

        if(key.eof)
             break;
}


I personally find VC++ much easier to use than dev-C++.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group