我已經在我的C++程序中編寫了下面的代碼,但接近尾聲時,我需要計算x1/SumOfIntegers
的值。我是一個初學者,我非常感謝任何能夠幫助我找出答案的小數結果的人。我一直使用2作爲我所有的整數輸入,所以x1 = 2
和SumOfIntegers = 10
。因此x1/SumOfIntegers
應該等於.2
,但我始終得到1
作爲輸出。有人可以幫幫我嗎?如何從兩個整數輸入產生一個十進制結果?
#include <iostream>
#include "graphics.h"
#define _USE_MATH_DEFINES
#include "math.h"
using namespace std;
int main()
{
double x1;
double x2;
double x3;
double x4;
double x5;
double SumOfIntegers;
const double Radius = 250;
double CircumferenceOfCircle;
double x1PercentOfTotal;
cout <<
"You will be prompted to enter five integers for a pie chart \n";
cout << "Enter integer 1: ";
cin >> x1;
cout << "Enter integer 2: ";
cin >> x2;
cout << "Enter integer 3: ";
cin >> x3;
cout << "Enter integer 4: ";
cin >> x4;
cout << "Enter integer 5: ";
cin >> x5;
cout << "Sum of integers: " << x1 + x2 + x3 + x4 + x5 << endl;
cin >> SumOfIntegers;
cout << "Circumference of Circle: " << 2 * (M_PI) * Radius << endl;
cin >> CircumferenceOfCircle;
cout << "x1 Percentage of Total " << (double)(x1)/
(double)(SumOfIntegers) << endl;
cin >> x1PercentOfTotal;
return 0;
}
謝謝!你能解釋爲什麼刪除cin >> SumOfIntegers解決了我的問題嗎?我是一個初學者,這是我寫的第一個程序。 – princessofsly5
'cin >> SumOfIntegers' _overwrites_存儲的值與您在鍵盤中輸入的任意數字。 :) – sarnold