C++ 华氏摄氏度转换器问题
目的是输入华氏或摄氏温度转换输出,若未能正确输入则重新输入。测试时发现正确格式输入正常输出,但一旦输入单个字母则会无限循环,为何会发生这种情况,求解答!
#include<iostream>
using namespace std;
int main()
{
const unsigned short ADD_SUBTRACT=32;
const double RATIO=9.0/5.0;
double tempin,tempout;
char typein, typeout;
std::cout<<"请以【xx.x C】或【xx.x F】的格式输入温度"<<"\n\n";
do
{
std::cin>>tempin>>typein;
std::cin.ignore(100,'\n');
std::cout<<"\n";
if(typein=='C'||typein=='c')
{
tempout=tempin*RATIO+ADD_SUBTRACT;
typeout='F';
typein='C';
break;
}
else if(typein=='F'||typein=='f')
{
tempout=(tempin-ADD_SUBTRACT)/RATIO;
typeout='C';
typein='F';
break;
}
else
{
typeout='E';
std::cout<<"格式错误,请重新输入!"<<"\n\n";
}
}while(typeout=='E');
std::cout<<tempin<<" "<<typein
<<" = "<<tempout<<" "<<typeout<<"\n\n";
system("pause");
return 0;
} C++ 的,我帮你顶
页:
[1]