我是菜鸟。。想了两天都不明白为什么这个程序是死循环
就是一个grade作业的程序流入文件里第一行是标准答案,接下来的几行都是用户名和他们的答案。
要求输出文件里第一行是标准答案,以下几行是用户名和对的个数。
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
string keystr;
string answerstr;
ifstream infile;
ofstream outfile;
int ID;
void machlength();
void machrange();
void machanswer();
int main ()
{
outfile.open("score.dat");
infile.open("exams.dat");
if (!infile)
{ cout<<"connot open the file"<<endl;
return 1;
}
infile>>keystr;
outfile<<keystr<<endl;
infile>>ID>>answerstr;
while (infile)
{
outfile<<ID<<" ";
machlength();
infile>>ID>>answerstr;
}
return 0 ;
}
void machlength()
{
int len;
len=answerstr.length();
if (len>20)
outfile<<"Too many answers"<<endl;
else if (len<20)
outfile<<"Too few answers"<<endl;
else
machrange();
return;
}
void machrange()
{
string myanswer;
int countwrong=0;
int count=0;
while (count<20);
{
myanswer=keystr.substr(count,1);
if (!(myanswer=="a"||myanswer=="b"||myanswer=="c"||myanswer=="d"||myanswer=="e"||myanswer=="f"))
{
outfile<<"Invalid answer";
countwrong++;
}
count++;
}
if (countwrong==0)
machanswer();
return;
}
void machanswer()
{
string key;
string answer;
int rightanswer=0;
int count=0;
while (count<20)
{
key=keystr.substr(count,1);
answer=answerstr.substr(count,1);
if (key==answer)
rightanswer++;
count++;
}
outfile<<rightanswer;
return;
}
做出来exe里一直是只有一个跳动的光标。
谢谢大侠赐教,菜鸟感激不尽。