C ++必须输入两次

智慧

我正在使用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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

:w之后必须两次ctrl + C

来自分类Dev

目标C-两次释放会导致崩溃?

来自分类Dev

消息框出现两次C#

来自分类Dev

C ++ Google测试运行两次

来自分类Dev

对结构列表两次排序C ++

来自分类Dev

C ++两次删除指针

来自分类Dev

从C ++ JNI NDK函数调用Java方法两次

来自分类Dev

必须在C ++中的getline之后按两次Enter键吗?

来自分类Dev

值在输出中显示两次(C编程)

来自分类Dev

用C语言对链表进行两次排序

来自分类Dev

两次阅读C ++ ifstream吗?

来自分类Dev

C ++两次声明变量

来自分类Dev

C ++:通过地图进行两次迭代

来自分类Dev

C释放数组和元素失败,并释放两次

来自分类Dev

C:字符串错误打印两次

来自分类Dev

C#和反射不能连续两次工作

来自分类Dev

检索C中两次迭代之间花费的时间

来自分类Dev

C#Picturebox.Image必须先加载两次,然后才能在代码中使用

来自分类Dev

在C ++中,构造函数两次调用过吗?

来自分类Dev

两次使用时,C#params参数选择

来自分类Dev

Weblogic 12c-两次部署耳朵

来自分类Dev

Extern在C ++中使用了两次

来自分类Dev

为什么在这里对c ++输入文件流进行了两次检查?

来自分类Dev

必须输入两次值才能使用C ++

来自分类Dev

C ++-两次DrawText()

来自分类Dev

C ++输出被打印两次

来自分类Dev

C#Picturebox.Image必须先加载两次,然后才能在代码中使用

来自分类Dev

相同的输入两次不输入两次

来自分类Dev

必须单击两次按钮才能触发c ++