[英]How can i get an input of 3 double variables with cin in C++?
我必須將 3 個雙變量作為輸入並找到它們的平均值。 如果我輸入 integer 作為輸入(例如 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.
if you input 3 its working. but if you input 3,2 its not working.
, 浮點數是按編程標准用 . 分隔的.
, 不,
. 例如,您必須編寫3.2
。 這也是英語國家的標准。
你的代碼是正確的。 我認為您在輸入 5,3 中寫道。 當您使用符號時,您的程序運行不正確。 你不應該使用系統(“暫停”)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.