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

自学c++时候出现的问题,请帮下忙了。。

hulihong 发布于 2016-03-20 01:12, 5311 次点击
#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""输入错误!"
3 回复
#2
天使梦魔2016-03-20 10:10
while(typeOut!='0');

在do{下面添加typeIn=0;用来清除之前的buffer
#3
hulihong2016-03-20 17:38
回复 2楼 天使梦魔
还是不行,按照这样改。输入正确时就会进入死循环
#4
hjx11202016-03-21 00:02
程序代码:
#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;
}
1