Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Error in code.

 
Post new topic   Reply to topic Printable version
 View previous topic  Bowling! (Calculating the score for a ... Post :: Post Gamestation.  View next topic  
Author Message
Yoink
Novice


Joined: May 23 2006
Posts: 25
Offline

PostPosted: Thu Oct 26, 2006 11:16 pm    Post subject: Error in code. Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Muskrat
Server Help Squatter


Age:36
Joined: Aug 24 2004
Posts: 829
Location: Swamp
Offline

PostPosted: Fri Oct 27, 2006 12:21 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Yoink
Novice


Joined: May 23 2006
Posts: 25
Offline

PostPosted: Fri Oct 27, 2006 12:43 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Muskrat
Server Help Squatter


Age:36
Joined: Aug 24 2004
Posts: 829
Location: Swamp
Offline

PostPosted: Fri Oct 27, 2006 1:00 am    Post subject: Reply to topic Reply with quote

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++.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 679 page(s) served in previous 5 minutes.

phpBB Created this page in 0.498211 seconds : 29 queries executed (91.9%): GZIP compression disabled