【问题标题】:How can i get an input of 3 double variables with cin in C++?如何在 C++ 中使用 cin 获得 3 个双变量的输入?
【发布时间】:2020-04-12 12:03:23
【问题描述】:

我必须将 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;
}

【问题讨论】:

  • 对我也有用:https://ideone.com/QUJaQb
  • 您是否将语言环境设置为使用逗号小数而不是点?
  • 您确定这是您正在运行的代码吗?也许您最初编写代码以获取 int 变量,而您仍在运行旧的二进制文件。
  • 如何编写这段代码来获取双变量?我是 C++ 的新手。

标签: c++ input output double cin


【解决方案1】:

您的程序按预期运行。至少相应地计算平均值,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。这也是英语国家的标准。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2021-04-23
      • 2021-04-11
      • 1970-01-01
      • 2013-02-19
      • 2021-12-25
      • 2022-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多