我已經閱讀了我的C++書籍的前50頁左右,並與 知識,我已經嘗試創建一個程序, 從一個計算一個圓的兩個測量,這就是我已經想出了什麼 :如何讓我的答案顯示爲小數?
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
string op;
int r;
int a;
int d;
int val;
cout << "This program aims to calculate two measurements of a cicle from one measurement" << endl;
cout << "Enter the measurement you wish to input from a choice of r, d or a" << endl;
cin >> op;
cout << "Please enter the value of this measurement" << endl;
cin >> val;
if (op == "r") {
a = val * val * 3.14;
d = val * 2;
r = val;
}
if (op == "d") {
a = val/2 * val/2 * 3.14;
d = val;
r = val/2;
}
if (op == "a"){
a = val;
d = (sqrt(val/3.14)) * 2;
r = (sqrt(val/3.14));
}
cout << "The area is " << endl;
cout << std::setprecision(3) << a << endl;
cout << "The diameter is " << endl;
cout << std::setprecision(3) << d << endl;
cout << "The radius is " << endl;
cout << std::setprecision(3) << r << endl;
}
我的問題是,我的答案都出來沒有小數,你可以看到我試圖納入「setprecision」,但它沒有奏效。
請嘗試發佈的[最少的代碼(HTTP:// stackoverflow.com/help/mcve)需要重現錯誤! – Csq
您正在使用'int's。你爲什麼期望得到小數? –
另外,打開你的警告,並閱讀它們 –