如何让我的答案显示为小数?

时间:2014-06-26 21:06:15

标签: c++

我已经阅读了我的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;
}

我遇到的问题是我的答案都没有小数,因为你可以看到我试图整合&#34; setprecision&#34;但它没有奏效。

2 个答案:

答案 0 :(得分:5)

int替换为double

int r;
int a;
int d;
int val;

答案 1 :(得分:3)

std::set_precision仅在应用于浮点类型时执行您的预期,例如floatdouble。我怀疑如果您使用其中一种类型而不是int s

,大多数问题都会得到解决