-2

I tried to calculate the sum of two floats 6.5 and 7.5 in C++. I expected the result to be a float with decimals like 14.000000. But it gave an answer 14, and integer without decimals.

Can someone explain what is happening?

#include <iostream.h>
int main ()
{     
  float number1, number2;
  sum,average;
   cout<<"Enter 2 numbers";
   cout<<"number1=";
   cin>>number1;
   cout<<"number2=";
  cin>>"number2;
  sum = numberl+number2;
  average=sum/2;
  cout << "sum=" <<sum ;
   cout << "Average = " << average;
  return 0;
 }

The output is:

Enter 2 numbers: 6.5 7.5
Sum=14
Average=7
Christophe
  • 68,716
  • 7
  • 72
  • 138
Tech Geek
  • 21
  • 2
  • 4
  • Try `#include /* ... */ std::cout << std::fixed << std::setprecision(2) << your_value << '\n';` – Bob__ Jan 18 '20 at 09:29
  • You need to provide details, read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – anastaciu Jan 18 '20 at 09:29
  • also choose either C or C++ (not both) and post your code here – phuclv Jan 18 '20 at 09:49
  • See e.g. https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout – Bob__ Jan 18 '20 at 10:06
  • 3
    It looks like you are using an ancient compiler. `iostream.h` was removed decades ago. In such a case it's important to know which compiler you are using. Is this the good old Turbo C++ from 1996? Such old compilers behave very differently as "modern" compilers from after 1998. – Thomas Sablik Jan 18 '20 at 10:20
  • 1
    When you enter program text into a Stack Overflow question or answer, copy and paste the **exact** code. Do not retype it. The code in the question has at least three problems that look like retyping errors: `sum,average;` is missing a keyword. `cin>>"number;` has a quote mark it should not. And `sum = numberl+number2;` has an `l` instead of a `1`. Mistakes like this impede people from reproducing your problem or understanding your question. – Eric Postpischil Jan 18 '20 at 14:17

3 Answers3

3
float number1, number2;
  sum,average;

That's strange moment. Seems like second line is declaration of two variables with type int. It actually shouldn't work, but according to your text seems like it does. Also you are using some very old compiler.

The fix is simple: either add float:

float number1, number2;
float sum, average;

or replace semicolon by comma:

float number1, number2, sum, average;

Also I'd like to say that it's better to use double then float.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128
  • Indeed, old C (and C++?) compilers assumed `int` when the type was left out. Nice observation... ;-) – Scheff's Cat Jan 18 '20 at 13:26
  • @Scheff, I know about default int, but I thought it worked only for functions arguments and return type. Did it really worked so that any word in code like `qqq;` would declare a new integer variable? Actually I remembered that I've played with declaring a function pointer starting from `(*`... Interesting. And it was never a part of C++ as I know, only for C. – Qwertiy Jan 18 '20 at 13:30
  • Now, where you ask, I became uncertain. Actually, I never relied on this neither in C nor in C++. I found this answer to [SO: Omitting the datatype (e.g. “unsigned” instead of “unsigned int”)](https://stackoverflow.com/a/4315841/7478597), which mentions the default `int` as well. – Scheff's Cat Jan 18 '20 at 13:35
  • @Scheff, I played with it when was writing a program that should compile both in C and Pascal. For C `(*` was declaring a pointer and for pascal it was beginning of the comment. And it definitely was placed in the very beginning of the file. – Qwertiy Jan 18 '20 at 13:37
  • 1
    Found an even better answer: [SO: Why does/did C allow implicit function and typeless variable declarations?](https://stackoverflow.com/a/11835427/7478597) – Scheff's Cat Jan 18 '20 at 13:39
  • As I recall, declarations could be typeless but not completely declaration-keyword-less. You had to have `static` or `extern` or something; the compiler would not just recognize `foo;` as a declaration. – Eric Postpischil Jan 18 '20 at 14:14
1

I think that this code doesn't compile nor on old compilers nor on new ones, since there is an invalid double quote in this line:

cin>>"number2;

Since there are typos, it is not clear if this is the original code, but this line:

sum,average;

should lead to a compilation error since you use undefined variables. The K&>R grammar for old C compilers allowed a function to be declared without type, which meant it was int. But that was for functions only. Variables had to have at least one type specifier.

Assuming that there was a typo and that the last character of the previous line was a , and not a ; the variables would be float, which is your expectation.

Finally, with the typos corrected, it appears that it is only a formatting issue. The default formating for a floating point in C++ is to use an integer format if the value has no meaningful decimals.

You can force a decimal representation:

  • with cout<<showpoint<<sum<<endl;
  • with cout<<fixed<<sum<<endl;.
  • or wwith scientific presentation, but it would then be displayed as 1.4e+01
Christophe
  • 68,716
  • 7
  • 72
  • 138
0

The output “14” is not a floating-point number or an integer type. It is character text. It does not indicate the value used with cout << sum had any particular type.

The reason that adding 6.5 and 7.5 gave an integer result is because the mathematical result is 14, and 14 is an integer. Floating-point types are capable of representing integers. When you output this value with cout << sum, the default formatting produces the text “14”.

To instruct the stream to show a decimal point and trailing zeros for floating-point outputs, insert cout.setf(ios_base::showpoint); into your code.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312