-7

I have following program and i want the output to be 10.0
what line of code I have to add in function fun so that i have desired output.

#include<bits/stdc++.h>
using namespace std;
float fun(float a)
{
    return a;
}
int main()
{
    float a = 10;
    cout << fun(a);
    return 0;
}

I tried using setprecision() but it is often used with cout. How it can be used when returning the output? I am stuck here.

  • 5
    The value *is* 10.0, which is the same number as 10. Trailing zeros are not printed by default. See [`std::fixed`](https://en.cppreference.com/w/cpp/io/manip/fixed). – molbdnilo Jan 18 '23 at 17:06
  • 3
    You could convert `a` to text inside `fun`, and change `fun` to return `std::string`. Floating-point values are simply values; how they are formatted for output is determined by the conversion to text for the output, not by the value. – Pete Becker Jan 18 '23 at 17:09
  • 2
    Side notes: (1) `#include `: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h, (2) `using namespace std;`: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – wohlstad Jan 18 '23 at 17:09
  • 2
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and then *never* include that header again. See also [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) which is making a bad thing worse. – Jesper Juhl Jan 18 '23 at 17:09
  • 1
    @drescherjm Well, you *can* set `std::fixed` inside the function... – molbdnilo Jan 18 '23 at 17:12
  • you are asking how to make 1234 not be the same number as 0001234 – user253751 Jan 18 '23 at 17:12

1 Answers1

3

Nothing to stop this

float fun(float a)
{
    cout << fixed << setprecision(1);
    return a;
}

But that's stupid code for a stupid puzzle. What does this have to do with real programming?

john
  • 85,011
  • 4
  • 57
  • 81
  • @john Thanks for the answer. Actually, I didn't know how setprecision() can be used in case of returning the output. Also, this is not stupid code. I know it has nothing to do with real programming. I included it so that my problem of converting a float variable having a value like 10 to something like 10.0 (with decimal) is solved. It's just to clarify my question. – user21037758 Jan 19 '23 at 17:56
  • @user21037758 It's a horrible solution, as acknowledged by its author, and you should *not* consider your problem "solved". Nobody should ever use this code, and you need to rethink your requirements because they make no sense. You have a function with a bizarre side effect. Now `fun(3); std::cout << 1.23` is broken and `std::cerr << fun(3)` produces surprising, incorrect results while also changing `cout` as a side effect. What you **need** is for `fun` to return a formatted string. – user229044 Jan 19 '23 at 23:14
  • @user21037758 You might be confusing how a variable is printed, with what value a variable has. The variable `a` has a value of 10.0 always, or put it another way 10.0 and 10 are the same value. `setprecision` does nothing to the variable `a` or anything to do with how `a` is returned from the function. What it does do is change the way `cout` prints floating point values, and because that change is global it still holds after the function has returned, – john Jan 20 '23 at 07:29