标题:我是菜鸟。。想了两天都不明白为什么这个程序是死循环
取消只看楼主
cheerjam
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-10-6
结帖率:100%
已结贴  问题点数:20 回复次数:0 
我是菜鸟。。想了两天都不明白为什么这个程序是死循环
就是一个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里一直是只有一个跳动的光标。

谢谢大侠赐教,菜鸟感激不尽。
搜索更多相关主题的帖子: include 用户名 天都 
2009-11-12 12:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-292045-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.166270 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved