Sum of loop output in float

Hello I need help on how to compute the loop output in float

1
2
3
4
5
6
7
8
9
10
11
12
13
  float num=1.5,sum;

do
{
cout<<num;
cout<<endl;
num+=1.5;


}while(num<=9.0);

sum+=num;
cout<<"Sum: "<<sum;

The output of my code is:
1.5
3
4.5
6
7.5
9
Sum: 10.5
The output I'm looking for is this:
1.5
3.0
4.5
6.0
7.5
9.0
Sum: 31.5
sum+=num;
Try putting this before the loop starts.

Then try putting it inside the loop.
Think about whether it should go before or after the adjustment of num.
Ohh it works when it's in the loop how? But another problem is how can I make the 3, 6, and 9 into 3.0, 6.0 and 9.0
> Ohh it works when it's in the loop how?
If only there was something you could do to the s/w.

Maybe, just maybe
1
2
3
num+=1.5;
sum+=num;
cout << "DEBUG: num=" << num << ", sum=" << sum << endl;

When you've understood, you can delete the debug line.

> But another problem is how can I make the 3, 6, and 9 into 3.0, 6.0 and 9.0
https://en.cppreference.com/w/cpp/io/manip/setprecision
But setprecision doesn't add .0 to the whole numbers.
Keep reading.
iomanip has lots of things for you to try.
Ohh sh*t I got it, thanks man!
Topic archived. No new replies allowed.