C++ Problem I get “nan†as output everytime I run my program
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I was required to create a program with a function that changes height in feet to height in meters. I made the function and when I cout from the function I get the right value but when I cout it in main I get "nan". I dont understand why the value is not printing. This is my first time using this website so I am sorry if I miss anything.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
int main()
double feet, inches, calcheight;
char ch;
cout << "Enter your height [Use format ft-in]: ";
cin >> feet >> ch >> inches;
calcheight = heightInMeters(feet, inches);
cout << calcheight << endl;
return 0;
c++ function nan
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
6
down vote
favorite
I was required to create a program with a function that changes height in feet to height in meters. I made the function and when I cout from the function I get the right value but when I cout it in main I get "nan". I dont understand why the value is not printing. This is my first time using this website so I am sorry if I miss anything.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
int main()
double feet, inches, calcheight;
char ch;
cout << "Enter your height [Use format ft-in]: ";
cin >> feet >> ch >> inches;
calcheight = heightInMeters(feet, inches);
cout << calcheight << endl;
return 0;
c++ function nan
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I was required to create a program with a function that changes height in feet to height in meters. I made the function and when I cout from the function I get the right value but when I cout it in main I get "nan". I dont understand why the value is not printing. This is my first time using this website so I am sorry if I miss anything.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
int main()
double feet, inches, calcheight;
char ch;
cout << "Enter your height [Use format ft-in]: ";
cin >> feet >> ch >> inches;
calcheight = heightInMeters(feet, inches);
cout << calcheight << endl;
return 0;
c++ function nan
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I was required to create a program with a function that changes height in feet to height in meters. I made the function and when I cout from the function I get the right value but when I cout it in main I get "nan". I dont understand why the value is not printing. This is my first time using this website so I am sorry if I miss anything.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
int main()
double feet, inches, calcheight;
char ch;
cout << "Enter your height [Use format ft-in]: ";
cin >> feet >> ch >> inches;
calcheight = heightInMeters(feet, inches);
cout << calcheight << endl;
return 0;
c++ function nan
c++ function nan
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
Khizar Muhammad
363
363
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Khizar Muhammad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago
add a comment |Â
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
accepted
This function here:
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
isn't returning anything. That's undefined behavior, what you get here
calcheight = heightInMeters(feet, inches);
Is most likely just some invalid rubbish value then. Perhaps instead of this:
cout << heightInMeters << endl;
You wanted this:
return heightInMeters;
Does your compiler issue any warnings for your code? If not, please try to find out if you can set it to give you more warnings. Most compilers usually complain about missing returns.
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
No problem, glad it helped.
– Blaze
1 hour ago
add a comment |Â
up vote
3
down vote
heightInMeters
doesn't have an explicit return
value.
Therefore, since it's not a void
function (or main
), the behaviour of your program is undefined.
Didn't your compiler warn you of that? It's an easy spot for a compiler to make in your case, and the current crop of compilers all warn if you turn the warning level up appropriately.
(Granted, NaN is a peculiar manifestation of that undefined behaviour.)
Finally, note that one foot is exactly 0.3048 meters. Base your conversion metrics from that. Your values introduce unnecessary imprecision.
While that is certainly an issue, I doubt theNaN
return is due to that. More likely hiscin
is reading garbabe to begin with.
– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using adouble
. I'd recode so you only had one constant if I were you.
– Bathsheba
1 hour ago
add a comment |Â
up vote
1
down vote
void f()
double a=0.3;
cout << a << endl;
//
// inside main function
f();
double f()
double a=0.3;
return a;
// inside main function
cout << f() << endl;
Because the return value of your code is not specified, the output is “nan“
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
This function here:
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
isn't returning anything. That's undefined behavior, what you get here
calcheight = heightInMeters(feet, inches);
Is most likely just some invalid rubbish value then. Perhaps instead of this:
cout << heightInMeters << endl;
You wanted this:
return heightInMeters;
Does your compiler issue any warnings for your code? If not, please try to find out if you can set it to give you more warnings. Most compilers usually complain about missing returns.
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
No problem, glad it helped.
– Blaze
1 hour ago
add a comment |Â
up vote
6
down vote
accepted
This function here:
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
isn't returning anything. That's undefined behavior, what you get here
calcheight = heightInMeters(feet, inches);
Is most likely just some invalid rubbish value then. Perhaps instead of this:
cout << heightInMeters << endl;
You wanted this:
return heightInMeters;
Does your compiler issue any warnings for your code? If not, please try to find out if you can set it to give you more warnings. Most compilers usually complain about missing returns.
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
No problem, glad it helped.
– Blaze
1 hour ago
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
This function here:
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
isn't returning anything. That's undefined behavior, what you get here
calcheight = heightInMeters(feet, inches);
Is most likely just some invalid rubbish value then. Perhaps instead of this:
cout << heightInMeters << endl;
You wanted this:
return heightInMeters;
Does your compiler issue any warnings for your code? If not, please try to find out if you can set it to give you more warnings. Most compilers usually complain about missing returns.
This function here:
double heightInMeters(double feet , double inches)
double footToMeter = 0.305;
double inchToMeter = 0.0254;
double heightInMeters = ((footToMeter * feet) + (inchToMeter * inches));
cout << heightInMeters << endl;
isn't returning anything. That's undefined behavior, what you get here
calcheight = heightInMeters(feet, inches);
Is most likely just some invalid rubbish value then. Perhaps instead of this:
cout << heightInMeters << endl;
You wanted this:
return heightInMeters;
Does your compiler issue any warnings for your code? If not, please try to find out if you can set it to give you more warnings. Most compilers usually complain about missing returns.
answered 1 hour ago
Blaze
1,744218
1,744218
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
No problem, glad it helped.
– Blaze
1 hour ago
add a comment |Â
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
No problem, glad it helped.
– Blaze
1 hour ago
1
1
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
Thank you so much, your answer helped alot!
– Khizar Muhammad
1 hour ago
1
1
No problem, glad it helped.
– Blaze
1 hour ago
No problem, glad it helped.
– Blaze
1 hour ago
add a comment |Â
up vote
3
down vote
heightInMeters
doesn't have an explicit return
value.
Therefore, since it's not a void
function (or main
), the behaviour of your program is undefined.
Didn't your compiler warn you of that? It's an easy spot for a compiler to make in your case, and the current crop of compilers all warn if you turn the warning level up appropriately.
(Granted, NaN is a peculiar manifestation of that undefined behaviour.)
Finally, note that one foot is exactly 0.3048 meters. Base your conversion metrics from that. Your values introduce unnecessary imprecision.
While that is certainly an issue, I doubt theNaN
return is due to that. More likely hiscin
is reading garbabe to begin with.
– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using adouble
. I'd recode so you only had one constant if I were you.
– Bathsheba
1 hour ago
add a comment |Â
up vote
3
down vote
heightInMeters
doesn't have an explicit return
value.
Therefore, since it's not a void
function (or main
), the behaviour of your program is undefined.
Didn't your compiler warn you of that? It's an easy spot for a compiler to make in your case, and the current crop of compilers all warn if you turn the warning level up appropriately.
(Granted, NaN is a peculiar manifestation of that undefined behaviour.)
Finally, note that one foot is exactly 0.3048 meters. Base your conversion metrics from that. Your values introduce unnecessary imprecision.
While that is certainly an issue, I doubt theNaN
return is due to that. More likely hiscin
is reading garbabe to begin with.
– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using adouble
. I'd recode so you only had one constant if I were you.
– Bathsheba
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
heightInMeters
doesn't have an explicit return
value.
Therefore, since it's not a void
function (or main
), the behaviour of your program is undefined.
Didn't your compiler warn you of that? It's an easy spot for a compiler to make in your case, and the current crop of compilers all warn if you turn the warning level up appropriately.
(Granted, NaN is a peculiar manifestation of that undefined behaviour.)
Finally, note that one foot is exactly 0.3048 meters. Base your conversion metrics from that. Your values introduce unnecessary imprecision.
heightInMeters
doesn't have an explicit return
value.
Therefore, since it's not a void
function (or main
), the behaviour of your program is undefined.
Didn't your compiler warn you of that? It's an easy spot for a compiler to make in your case, and the current crop of compilers all warn if you turn the warning level up appropriately.
(Granted, NaN is a peculiar manifestation of that undefined behaviour.)
Finally, note that one foot is exactly 0.3048 meters. Base your conversion metrics from that. Your values introduce unnecessary imprecision.
edited 30 mins ago
answered 1 hour ago


Bathsheba
170k26239363
170k26239363
While that is certainly an issue, I doubt theNaN
return is due to that. More likely hiscin
is reading garbabe to begin with.
– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using adouble
. I'd recode so you only had one constant if I were you.
– Bathsheba
1 hour ago
add a comment |Â
While that is certainly an issue, I doubt theNaN
return is due to that. More likely hiscin
is reading garbabe to begin with.
– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using adouble
. I'd recode so you only had one constant if I were you.
– Bathsheba
1 hour ago
While that is certainly an issue, I doubt the
NaN
return is due to that. More likely his cin
is reading garbabe to begin with.– Zinki
1 hour ago
While that is certainly an issue, I doubt the
NaN
return is due to that. More likely his cin
is reading garbabe to begin with.– Zinki
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
@Zinki: It's plausible in a debug build, I guess?
– Bathsheba
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
Thank you, I added a return and it worked! My compiler didnt warn me. I am using code blocks.
– Khizar Muhammad
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using a
double
. I'd recode so you only had one constant if I were you.– Bathsheba
1 hour ago
@KhizarMuhammad: Your last job is to fix the conversion constants; they are inadequate particularly given that you're using a
double
. I'd recode so you only had one constant if I were you.– Bathsheba
1 hour ago
add a comment |Â
up vote
1
down vote
void f()
double a=0.3;
cout << a << endl;
//
// inside main function
f();
double f()
double a=0.3;
return a;
// inside main function
cout << f() << endl;
Because the return value of your code is not specified, the output is “nan“
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
void f()
double a=0.3;
cout << a << endl;
//
// inside main function
f();
double f()
double a=0.3;
return a;
// inside main function
cout << f() << endl;
Because the return value of your code is not specified, the output is “nan“
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
void f()
double a=0.3;
cout << a << endl;
//
// inside main function
f();
double f()
double a=0.3;
return a;
// inside main function
cout << f() << endl;
Because the return value of your code is not specified, the output is “nan“
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
void f()
double a=0.3;
cout << a << endl;
//
// inside main function
f();
double f()
double a=0.3;
return a;
// inside main function
cout << f() << endl;
Because the return value of your code is not specified, the output is “nan“
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 29 mins ago


Pretasoc
563214
563214
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 52 mins ago


Rong Wu
112
112
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Rong Wu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.
Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.
Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.
Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52887686%2fc-problem-i-get-nan-as-output-everytime-i-run-my-program%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Possible duplicate of function keeps returning NaN
– Julien Lopez
56 mins ago