我如何在C ++中使用cin输入3个双变量?

问题描述 投票:0回答:2

我必须将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;
}
c++ input output double cin
2个回答
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。这就是英语国家所采用的标准。


0
投票

您的代码是核心。我认为您在输入5,3中写过。当您使用sign时,您的程序运行不正确。您不应该使用system(“ pause”)。

© www.soinside.com 2019 - 2024. All rights reserved.