nan

im making a full calculator but why is the output is always followed by
nan
ex:
9+9=18nan
What calculator? What output? What programming language?
small calculator haha not full. i mean it always printing out the answer followed by "nan",of course C++
9+9
output:
18nan
Last edited on
nan means "not a number". It also implies there is an error in your code.
@Chervil
here's some code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double addition(double first_argument,double second_argument);
int main()
{
    system("color 40");
double a;
double b;
cout<<"Enter two numbers: ";
cin>>a>>b;
cout<<addition(a,b);


}
double addition(double first_argument,double second_argument)
{

    cout<<first_argument + second_argument;

}
Last edited on
*face palm*
What is your addition function supposed to return? What does it currently return?
The function addition() should return a value of type double

that's what this line means:
 
double addition(double first_argument,double second_argument);


because you told the compiler that it will return a value, it trusts you will write code which honours that declaration. Placing blind trust in you, it dutifully tries here to output whatever value was returned:
 
cout<<addition(a,b);


But because there is no return statement in the function, (and I hope the compiler protested and told you so), there is nothing there, so it can only grab at the block of memory where the result should have been located, and output it, as though it was a value of type double.

my compiler gives these messages - don't ignore such messages:
In function 'double addition(double, double)':
[Warning] no return statement in function returning non-void [-Wreturn-type]


If you don't receive such messages when compiling, try changing the compiler options (MinGW GCC) to;
-std=c++11 -Wall -Wextra -pedantic
@Smac89 it should be
 18 
but its returning
18nan

The output of
18
is coming from the cout statements on line 16 in the addition function. There is no return statement in the addition function sending a value back.

The
nan
is coming from line 9 in the main function as it tries to cout a value that hasn't been returned from the addition function.
@Chervil nice explanation
because you told the compiler that it will return a value

first, so i understand now,if i use a function with non-void it will automatically tell the compiler that it will return a value.


second,i hope so too
third,how about the error in functions "too few argument" or "too many arguments"
how about the error in functions "too few argument" or "too many arguments"

I didn't get those messages when I compiled the code.
Was it some other code which gave that error?
@Chervil
yeah it was some other code.
by the way thank you
Topic archived. No new replies allowed.