我正在使用Visual Studio Professional2013。运行以下简单代码时:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain()
{
int choice;
double fahr, cel;
cout << "Please choose 1 for Fahrenheit, or 2 for Celsius conversion: ";
if (cin >> choice) {
while (cin >> choice){
if (choice == 1)
{
cout << "Enter Fahrenheit degrees to be converted: ";
cin >> fahr;
cel = (fahr - 32.0) / 1.8;
cout << fahr << " degrees Fahrenheit is " << cel << " degrees Celsius" << endl;
cout << "Please type a character followed by Enter, to end the program";
int stop; cin >> stop;
return 0;
}
else if (choice == 2){
cout << "Enter Celsius degrees to be converted: ";
cin >> cel;
fahr = 9.0 / 5.0 * cel + 32.0;
cout << cel << " degrees Celcius is " << fahr << " degrees Fahrenheit" << endl;
cout << "Please type a character followed by Enter, to end the program";
int stop; cin >> stop;
return 0;
}
else {
cout << "Not a valid option" << endl
<< "Please run program again...";
int stop; cin >> stop;
return 0;
}
}
}
return 0;
}
程序的输出是这样的:
Please choose 1 for Fahrenheit, or 2 for Celsius conversion: 1
1
Enter Fahrenheit degrees to be converted: 32
32 degrees Fahrenheit is 0 degrees Celsius
我必须输入两次选择,程序的其余部分才能运行。在上面的示例中,我必须将要转换的温度放置两次,然后将其转换一次。第一次放入时,它会移到新的一行。
为什么有任何线索?
编辑以包含代码和输出
if (cin >> choice) {
while (cin >> choice){
这要求输入两次。
摆脱掉,if
然后做while
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句