学校の宿題のプログラムがうまくいかなくて困っています。
Posted: 2010年4月07日(水) 07:24
初めまして。大学でC++を習い始めたばかりです。
課題で、exams.datというファイルの中にある以下のデータを読み取り。
abcdefabcdefabcdefab
1234567
abcdefabcdefabcdefab
9876543
abddefbbbdefcbcdefac
5554446
abcdefabcdefabcdef
4445556
abcdefabcdefabcdefabcd
3332221
abcdefghijklmnopqrst
一番上の列を模範解答として読み込み。
残りは生徒のIDナンバーと生徒の解答として読み込みます。生徒の数はわからないことになってます。
例
abcdefabcdefabcdefab ←模範解答
1234567 ←IDナンバー
abcdefabcdefabcdefab ←1234567の解答
9876543 ←IDナンバー
abddefbbbdefcbcdefac ←9876543の解答
5554446 ←IDナンバー
abcdefabcdefabcdef ←5554446の解答
4445556 ←IDナンバー
abcdefabcdefabcdefabcd ←3332221の解答
3332221 ←IDナンバー
abcdefghijklmnopqrst ←3332221の解答
その後、scores.datというファイルの中に。
生徒の解答と模範解答を照合して
もし正解ならスコアを一点ずつ加算し、最終的にはIDとスコアを表示。
もし生徒の解答数が少なすぎる(20個に満たない)場合、IDとTwo few answersと表示。
もし生徒の解答数が多すぎる(20個より多い)場合、IDとToo many answersと表示。
もし生徒の解答にa,b,c,d,e,f以外のものがひとつでも含まれる場合。IDとInvalid answersと表示。
結果的にはこのように表示されます。
1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answer
さて僕のプログラムなのですが、ファイルの中身の結果がこのようになってしまいます。
1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answer
3332221 5
僕が思うに、データの読み込みの部分のループがうまく行われていないのだと思うのですが……
以下が僕のプログラムです。
#include<fstream>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
void readInput(string&, int [/url], string [/url], int&); // read keys, ids,
//and
// answers
void gradeExam(string, int [/url], string [/url], int); // grade each
// student's
// answer
int main()
{
string key; // the key of the exam
string answers[20]; // answers from students
int id[20]; // ids of students
ofstream outFile; // keeps the exam result
int stc;
readInput(key, id, answers, stc);
gradeExam(key, id, answers, stc);
return 0;
}
// read data from file
void readInput( /* out */ string& key,
/* out */ int id[/url],
/* out */ string ans[/url], int& stc)
{
ifstream inFile; // contains the exam data to be processed
// Ask for the input file name
// Open input file and read input data into key, id, and ans
inFile.open("exams.dat");
int loopcount = 0;
inFile >> key;
inFile >> id[loopcount];
while(inFile)
{
inFile >> ans[loopcount];
loopcount++;
inFile >> id[loopcount];
}
stc = loopcount;
inFile.close();
}
// Process each student's answer and write the grade to a file
// Check if the length of student's answer is the same as the key length
// Check if each answer lies between 'a' and 'f'
void gradeExam( /* in */ string key,
/* in */ int id[/url],
/* in */ string ans[/url], int stc)
{
ofstream outFile; // output file to contain the grade report
int posit = 1;
int score = 0;
int count = 0;
// Ask for the output file name and open output file
outFile.open("scores.dat");
outFile << left;
// grade each answer and write the result to the file
while(count < stc)
{
if(ans[count].length() < 20)
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Too few answers" << endl;
else if(ans[count].length() > 20)
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Too many answers" << endl;
else
{
posit = 1;
score = 0;
while(posit < 21)
{
if(key.substr(posit, 1) == ans[count].substr(posit, 1))
score += 1;
else if(ans[count].substr(posit, 1) != "a"
&& ans[count].substr(posit, 1) != "b"
&& ans[count].substr(posit, 1) != "c"
&& ans[count].substr(posit, 1) != "d"
&& ans[count].substr(posit, 1) != "e"
&& ans[count].substr(posit, 1) != "f")
{
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Invalid answer" << endl;
break;
}
posit++;
}
outFile << fixed << setw(10) << id[count]
<< setw(10) << score << endl;
}
count++;
}
outFile.close();
}
mainの部分は最初から先生が書いてありました。
どう直せばよろしいのでしょうか?
よろしくお願いします。
課題で、exams.datというファイルの中にある以下のデータを読み取り。
abcdefabcdefabcdefab
1234567
abcdefabcdefabcdefab
9876543
abddefbbbdefcbcdefac
5554446
abcdefabcdefabcdef
4445556
abcdefabcdefabcdefabcd
3332221
abcdefghijklmnopqrst
一番上の列を模範解答として読み込み。
残りは生徒のIDナンバーと生徒の解答として読み込みます。生徒の数はわからないことになってます。
例
abcdefabcdefabcdefab ←模範解答
1234567 ←IDナンバー
abcdefabcdefabcdefab ←1234567の解答
9876543 ←IDナンバー
abddefbbbdefcbcdefac ←9876543の解答
5554446 ←IDナンバー
abcdefabcdefabcdef ←5554446の解答
4445556 ←IDナンバー
abcdefabcdefabcdefabcd ←3332221の解答
3332221 ←IDナンバー
abcdefghijklmnopqrst ←3332221の解答
その後、scores.datというファイルの中に。
生徒の解答と模範解答を照合して
もし正解ならスコアを一点ずつ加算し、最終的にはIDとスコアを表示。
もし生徒の解答数が少なすぎる(20個に満たない)場合、IDとTwo few answersと表示。
もし生徒の解答数が多すぎる(20個より多い)場合、IDとToo many answersと表示。
もし生徒の解答にa,b,c,d,e,f以外のものがひとつでも含まれる場合。IDとInvalid answersと表示。
結果的にはこのように表示されます。
1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answer
さて僕のプログラムなのですが、ファイルの中身の結果がこのようになってしまいます。
1234567 20
9876543 15
5554446 Too few answers
4445556 Too many answers
3332221 Invalid answer
3332221 5
僕が思うに、データの読み込みの部分のループがうまく行われていないのだと思うのですが……
以下が僕のプログラムです。
#include<fstream>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
void readInput(string&, int [/url], string [/url], int&); // read keys, ids,
//and
// answers
void gradeExam(string, int [/url], string [/url], int); // grade each
// student's
// answer
int main()
{
string key; // the key of the exam
string answers[20]; // answers from students
int id[20]; // ids of students
ofstream outFile; // keeps the exam result
int stc;
readInput(key, id, answers, stc);
gradeExam(key, id, answers, stc);
return 0;
}
// read data from file
void readInput( /* out */ string& key,
/* out */ int id[/url],
/* out */ string ans[/url], int& stc)
{
ifstream inFile; // contains the exam data to be processed
// Ask for the input file name
// Open input file and read input data into key, id, and ans
inFile.open("exams.dat");
int loopcount = 0;
inFile >> key;
inFile >> id[loopcount];
while(inFile)
{
inFile >> ans[loopcount];
loopcount++;
inFile >> id[loopcount];
}
stc = loopcount;
inFile.close();
}
// Process each student's answer and write the grade to a file
// Check if the length of student's answer is the same as the key length
// Check if each answer lies between 'a' and 'f'
void gradeExam( /* in */ string key,
/* in */ int id[/url],
/* in */ string ans[/url], int stc)
{
ofstream outFile; // output file to contain the grade report
int posit = 1;
int score = 0;
int count = 0;
// Ask for the output file name and open output file
outFile.open("scores.dat");
outFile << left;
// grade each answer and write the result to the file
while(count < stc)
{
if(ans[count].length() < 20)
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Too few answers" << endl;
else if(ans[count].length() > 20)
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Too many answers" << endl;
else
{
posit = 1;
score = 0;
while(posit < 21)
{
if(key.substr(posit, 1) == ans[count].substr(posit, 1))
score += 1;
else if(ans[count].substr(posit, 1) != "a"
&& ans[count].substr(posit, 1) != "b"
&& ans[count].substr(posit, 1) != "c"
&& ans[count].substr(posit, 1) != "d"
&& ans[count].substr(posit, 1) != "e"
&& ans[count].substr(posit, 1) != "f")
{
outFile << fixed << setw(10) << id[count]
<< setw(10) << "Invalid answer" << endl;
break;
}
posit++;
}
outFile << fixed << setw(10) << id[count]
<< setw(10) << score << endl;
}
count++;
}
outFile.close();
}
mainの部分は最初から先生が書いてありました。
どう直せばよろしいのでしょうか?
よろしくお願いします。