如何在 C++ 中使用 cin 获取 3 个双变量的输入?
How can i get an input of 3 double variables with cin in C++?
我必须将 3 个双精度变量作为输入并求出它们的均值。如果我输入一个整数作为输入(例如 5)程序工作。但如果我输入一个小数(例如 5.3),它不会接受其他 2 个输入并关闭。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
double y1,y2,y3,ort;
cout<<"1. input : \n";
cin>>y1;
cout<<"2. input : \n";
cin>>y2;
cout<<"3. input : \n";
cin>>y3;
ort=(y1+y2+y3)/3;
cout<<"Value : "<< ort << "\n" ;
system("pause");
return 0;
}
您的程序按预期运行。至少平均值是相应计算的,pause
调用对我不起作用。
1. input :
1.3
2. input :
2.3
3. input :
3.3
Value : 2.3
sh: 1: pause: not found
Press <RETURN> to close this window...
可能您设置的语言环境有误。
您可以尝试添加:
#include <locale.h>
setlocale(LC_ALL,"C")
正如我刚刚阅读您的评论 if you input 3 its working. but if you input 3,2 its not working.
,浮点数按照编程标准用 .
分隔,而不是 ,
。例如。你必须写3.2
。这也是英语国家的标准。
您的代码是正确的。
我认为您在输入 5,3 中写道。当您使用 sign 时,您的程序 运行 不正确。
你不应该使用 system("pause").
我必须将 3 个双精度变量作为输入并求出它们的均值。如果我输入一个整数作为输入(例如 5)程序工作。但如果我输入一个小数(例如 5.3),它不会接受其他 2 个输入并关闭。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
double y1,y2,y3,ort;
cout<<"1. input : \n";
cin>>y1;
cout<<"2. input : \n";
cin>>y2;
cout<<"3. input : \n";
cin>>y3;
ort=(y1+y2+y3)/3;
cout<<"Value : "<< ort << "\n" ;
system("pause");
return 0;
}
您的程序按预期运行。至少平均值是相应计算的,pause
调用对我不起作用。
1. input :
1.3
2. input :
2.3
3. input :
3.3
Value : 2.3
sh: 1: pause: not found
Press <RETURN> to close this window...
可能您设置的语言环境有误。
您可以尝试添加:
#include <locale.h>
setlocale(LC_ALL,"C")
正如我刚刚阅读您的评论 if you input 3 its working. but if you input 3,2 its not working.
,浮点数按照编程标准用 .
分隔,而不是 ,
。例如。你必须写3.2
。这也是英语国家的标准。
您的代码是正确的。 我认为您在输入 5,3 中写道。当您使用 sign 时,您的程序 运行 不正确。 你不应该使用 system("pause").