n1; 转到终点; } else如果(x==2) { cout>n1; 转到终点; } 结束:返回(n1); } int getOperator() { char op{}; cout>op; 返回(op); } 无效打印应答(双n1,字符op,双n2) { 如果(op=='+') cout,c++,visual-c++,C++,Visual C++" /> n1; 转到终点; } else如果(x==2) { cout>n1; 转到终点; } 结束:返回(n1); } int getOperator() { char op{}; cout>op; 返回(op); } 无效打印应答(双n1,字符op,双n2) { 如果(op=='+') cout,c++,visual-c++,C++,Visual C++" />

C++;警告C4244';返回';:从';双倍';至';int';,Visual Studio 2015可能丢失数据 新到C++,遵循教程并尝试扩展它。我的程序已编译,但第23行出现错误“警告C4244:‘返回’:从'double'转换为'int',可能丢失数据”: end:返回(n1)编译时,这意味着我的计算器结果不正确,忽略小数位并将所有内容视为整数。在我的代码中,除了我特别使用的1之外,我找不到任何int的实例。根本不应该转换双打。谁能看一下,告诉我哪里出了问题?我尝试将当前的int转换为double,并使用各种其他格式样式,但都无济于事 #include "stdafx.h" #include <iostream> #include <cstdint> using namespace std; int getNumber(int16_t x) { double n1{}; if (x == 1) { cout << "First number: "; cin >> n1; goto end; } else if (x == 2) { cout << "Second number: "; cin >> n1; goto end; } end: return(n1); } int getOperator() { char op{}; cout << "Operator (+, -, *, /): "; cin >> op; return(op); } void printAnswer(double n1, char op, double n2) { if (op == '+') cout << n1 << " + " << n2 << " = " << (n1 + n2) << '\n'; else if (op == '-') cout << n1 << " - " << n2 << " = " << (n1 - n2) << '\n'; else if (op == '*') cout << n1 << " * " << n2 << " = " << (n1 * n2) << '\n'; else if (op == '/') cout << n1 << " / " << n2 << " = " << (n1 / n2) << '\n'; } int main() { cout << "Please enter two numbers and an operator to perform a calculation." << endl; double n1(getNumber(1)); char op(getOperator()); double n2(getNumber(2)); printAnswer(n1, op, n2); return(0); } #包括“stdafx.h” #包括 #包括 使用名称空间std; int getNumber(int16_t x) { 双n1{}; 如果(x==1) { cout>n1; 转到终点; } else如果(x==2) { cout>n1; 转到终点; } 结束:返回(n1); } int getOperator() { char op{}; cout>op; 返回(op); } 无效打印应答(双n1,字符op,双n2) { 如果(op=='+') cout

C++;警告C4244';返回';:从';双倍';至';int';,Visual Studio 2015可能丢失数据 新到C++,遵循教程并尝试扩展它。我的程序已编译,但第23行出现错误“警告C4244:‘返回’:从'double'转换为'int',可能丢失数据”: end:返回(n1)编译时,这意味着我的计算器结果不正确,忽略小数位并将所有内容视为整数。在我的代码中,除了我特别使用的1之外,我找不到任何int的实例。根本不应该转换双打。谁能看一下,告诉我哪里出了问题?我尝试将当前的int转换为double,并使用各种其他格式样式,但都无济于事 #include "stdafx.h" #include <iostream> #include <cstdint> using namespace std; int getNumber(int16_t x) { double n1{}; if (x == 1) { cout << "First number: "; cin >> n1; goto end; } else if (x == 2) { cout << "Second number: "; cin >> n1; goto end; } end: return(n1); } int getOperator() { char op{}; cout << "Operator (+, -, *, /): "; cin >> op; return(op); } void printAnswer(double n1, char op, double n2) { if (op == '+') cout << n1 << " + " << n2 << " = " << (n1 + n2) << '\n'; else if (op == '-') cout << n1 << " - " << n2 << " = " << (n1 - n2) << '\n'; else if (op == '*') cout << n1 << " * " << n2 << " = " << (n1 * n2) << '\n'; else if (op == '/') cout << n1 << " / " << n2 << " = " << (n1 / n2) << '\n'; } int main() { cout << "Please enter two numbers and an operator to perform a calculation." << endl; double n1(getNumber(1)); char op(getOperator()); double n2(getNumber(2)); printAnswer(n1, op, n2); return(0); } #包括“stdafx.h” #包括 #包括 使用名称空间std; int getNumber(int16_t x) { 双n1{}; 如果(x==1) { cout>n1; 转到终点; } else如果(x==2) { cout>n1; 转到终点; } 结束:返回(n1); } int getOperator() { char op{}; cout>op; 返回(op); } 无效打印应答(双n1,字符op,双n2) { 如果(op=='+') cout,c++,visual-c++,C++,Visual C++,警告消息非常清楚。在您定义的获取数字的函数中,返回类型为int int getNumber(int16_t x) // ^^^ 而实际上是将double类型传递给return语句 要消除警告,请修复函数返回类型: double getNumber(int16_t x) // ^^^^^^ 作为旁注:请不要使用goto,尤其是在它完全多余的情况下。您的所有代码路径都直接指向标签end:。您的getNumber函数返回一个int,但您给它一个double。将double分配

警告消息非常清楚。在您定义的获取数字的函数中,返回类型为
int

    int getNumber(int16_t x)
 // ^^^
而实际上是将
double
类型传递给return语句

要消除警告,请修复函数返回类型:

    double getNumber(int16_t x)
 // ^^^^^^


作为旁注:请不要使用
goto
,尤其是在它完全多余的情况下。您的所有代码路径都直接指向标签
end:

您的
getNumber
函数返回一个
int
,但您给它一个
double
。将
double
分配给
 int
?您可能会丢失数据。如果您将
45.23
分配给
int
,您只会得到
45
0.23
将丢失。而且,
goto
完全不需要。
goto
的意义是什么?