|
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
Yoink Novice
Joined: May 23 2006 Posts: 25 Offline
|
Posted: 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).
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:
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.
#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 |
|
|
Muskrat Server Help Squatter
Age:37 Joined: Aug 24 2004 Posts: 829 Location: Swamp Offline
|
|
Back to top |
|
|
Yoink Novice
Joined: May 23 2006 Posts: 25 Offline
|
Posted: 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. |
|
Back to top |
|
|
Muskrat Server Help Squatter
Age:37 Joined: Aug 24 2004 Posts: 829 Location: Swamp Offline
|
Posted: 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:
key >> in;
while(!key.oef()){
........
.................
.........
key >> in;//on the last data entry, the instream stops at the end
}
|
try something like:
while(true){
key >> in;
............
...............
..........
if(key.eof)
break;
} |
I personally find VC++ much easier to use than dev-C++. |
|
Back to top |
|
|
|
|
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
|
Software by php BB © php BB Group Server Load: 1264 page(s) served in previous 5 minutes.
|