Whats my error in C++

Hello,
So for my assignment I had to create a program that would find the sum and then average of a list of numbers. I managed to do that part fine, but I was also meant to use double data type for my variables which I think I may have done right too? But for some reason in my output for my answer Im still getting a whole number, when I should be gettimg a decimal number. Does anyone see a mistake im making?


#include <iostream>

using namespace std;

int main()
{
float average;
int const FIVE = 5;
Double a = 24.,
Double b = 28.,
Double c = 32.,
Double d = 37.,
Double e = 55.,
Double sum = a + b + c + d + e;
Double average = (Double sum / FIVE);

cout << endl << endl;
cout << "The average is " << average;
cout << endl << endl;

return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This is my first time posting here, so thanks for the tip I really appreciate the help!

double should have a lowercase d.

You are dividing a double by a int. I think the default is to use integer division?

Ok, so i’ve fixed the uppercase issue, but im not sure I understand what you mean on integer division. Do you mean I have an issue with my formula?

I think double / int => int

So you should use a double for 5

Could be, but I thought promotions went the other way. It’s been a minute since I wrote C++ code though. Frankly I’m surprised the code compiles at all given the repeat declarations of sum and average.

Recommend removing the duplicate declarations. Making the FIVE constant double precision certainly won’t hurt too.

jrm

Yeah, you are right about promotion going the other way. I’m also confused about this compiling.

These lines are super wonky. Makes me thing there is some hidden macro definition of Double? But as written this should fail to compile for at least 4 different reasons.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.