| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4945 人关注过本帖
标题:自学c++时候出现的问题,请帮下忙了。。
只看楼主 加入收藏
hulihong
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-5-10
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
自学c++时候出现的问题,请帮下忙了。。
#include <iostream>
int main()
{   
   
    const unsigned short ADD_SUBTRACT = 32;
    const double RATIO = 9.0 / 5.0;
   
    double tempIn,tempOut;
    char typeIn,typeOut;
 do{
         std::cout<<"请输入**.* C或**.* F\n";
         std::cin>>tempIn>>typeIn;
         std::cin.ignore(100,10);
         std::cout<<"\n";

         switch(typeIn)
         {
         case 'C':
         case 'c':
             tempOut=tempIn*RATIO+ADD_SUBTRACT;
             typeOut='F';
             typeIn='C';

             break;        
         case 'F':
         case 'f':
             tempOut=(tempIn-ADD_SUBTRACT)/RATIO;
             typeOut='C';
             typeIn='F';
             break;

         default:
             typeOut='0';
              break;
         }
         if(typeOut!='0')
         {
             std::cout<<tempIn<<typeIn<<'='<<tempOut<<typeOut<<"\n\n";
         }
         else
         {
            
             std::cout<<"输入错误!"<<"\n";
            
            
         }     
}while(typeOut=='0');        
    return 0;
}

这是一个华氏度与摄氏度互换的程序。想利用do while函数,输入出错后再次进行输入。结果出现死循环,一直重复 "请输入**.* C或**.* F""输入错误!"
搜索更多相关主题的帖子: include double 
2016-03-20 01:12
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
收藏
得分:5 
while(typeOut!='0');

在do{下面添加typeIn=0;用来清除之前的buffer
2016-03-20 10:10
hulihong
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-5-10
收藏
得分:0 
回复 2楼 天使梦魔
还是不行,按照这样改。输入正确时就会进入死循环
2016-03-20 17:38
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:5 
程序代码:
#include <iostream>
#include <cctype>
int main()
{  

  

    const unsigned short ADD_SUBTRACT = 32;
    const double RATIO = 9.0 / 5.0;
  

    double tempIn,tempOut;
    char typeIn,typeOut;
     do{
         std::cout<<"请输入**.* C或**.* F\n";
         std::cin>>tempIn>>typeIn;
         std::cin.ignore(100,10);

         std::cout<<"\n";

         switch(typeIn)
         {
         case 'C':
         case 'c':
             tempOut=tempIn*RATIO+ADD_SUBTRACT;
             typeOut='F';
             typeIn='C';
             std::cout<<tempIn<<typeIn<<'='<<tempOut<<typeOut<<"\n\n";

             break;       

         case 'F':
         case 'f':
             tempOut=(tempIn-ADD_SUBTRACT)/RATIO;
             typeOut='C';
             typeIn='F';
             std::cout<<tempIn<<typeIn<<'='<<tempOut<<typeOut<<"\n\n";
             break;

         default:
            // typeOut='0';
            //  break;
            std::cout<<"输入错误!"<<"\n";
         }
       

    }while(typeOut != 'q' && tempIn != isalpha(tempIn));   

       

    return 0;
}
2016-03-21 00:02
快速回复:自学c++时候出现的问题,请帮下忙了。。
数据加载中...
 
   



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

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