|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目的是输入华氏或摄氏温度转换输出,若未能正确输入则重新输入。
测试时发现正确格式输入正常输出,但一旦输入单个字母则会无限循环,为何会发生这种情况,求解答!
- #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;
- }
复制代码 |
|