【问题标题】:What is happening when I declare an int, double, and char; I input a double for an int and an int for a double? It seems as it breaks things apart当我声明一个 int、double 和 char 时发生了什么;我为 int 输入一个 double,为 double 输入一个 int?似乎它把事情分开了
【发布时间】:2021-06-04 05:31:50
【问题描述】:

输入:5.7 4 b

输出:5 0.7 4

发生此输出的原因是什么? 我还注意到,当我使用与其声明的数据类型不匹配的输入时,我无法输入最后一个变量“char”。

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int main()
{
    int whole;
    double fractional;
    char letter;

    cin >> whole >> fractional >> letter;
    cout << whole << " " << fractional << " " << letter << endl;
return 0;
}

【问题讨论】:

    标签: char integer double


    【解决方案1】:

    首先 cin 需要一个整数,所以它只读取第一个值的整数部分并留下其余部分。你的输入相当于这个 5 0.7 4 b,所以 b 永远不会被读取。

    【讨论】:

    • 谢谢@daskalgg。我不难理解的是,从代码中我们要求三个不同的条目 cin
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2020-05-09
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多