| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 585 人关注过本帖
标题:我是菜鸟。。想了两天都不明白为什么这个程序是死循环
只看楼主 加入收藏
cheerjam
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-10-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
我是菜鸟。。想了两天都不明白为什么这个程序是死循环
就是一个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
l01w29
Rank: 2
等 级:论坛游民
帖 子:4
专家分:27
注 册:2009-11-7
收藏
得分:20 
在你的machrange函数里的while循环后多了一个分号...
2009-11-12 13:38
fuqingjun
Rank: 2
来 自:山东
等 级:论坛游民
帖 子:48
专家分:80
注 册:2009-11-2
收藏
得分:0 
回复 2楼 l01w29
哈哈, 你真细心,顶你

我是猪猪,我很想进步,寻志同道合者革命途中并肩行路!
2009-11-12 22:40
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:0 
对于这样的程序,如果一时发现不了错误的地方,单步调试是最好的方法。

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-11-13 12:32
快速回复:我是菜鸟。。想了两天都不明白为什么这个程序是死循环
数据加载中...
 
   



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

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