注册 登录
编程论坛 C++教室

我是菜鸟。。想了两天都不明白为什么这个程序是死循环

cheerjam 发布于 2009-11-12 12:05, 585 次点击
就是一个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里一直是只有一个跳动的光标。

谢谢大侠赐教,菜鸟感激不尽。
3 回复
#2
l01w292009-11-12 13:38
在你的machrange函数里的while循环后多了一个分号...
#3
fuqingjun2009-11-12 22:40
回复 2楼 l01w29
哈哈, 你真细心,顶你
#4
flyingcloude2009-11-13 12:32
对于这样的程序,如果一时发现不了错误的地方,单步调试是最好的方法。
1