C++ Problem I get “nan” as output everytime I run my program

The name of the pictureThe name of the pictureThe name of the pictureClash 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;










share|improve this question







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














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;










share|improve this question







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












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;










share|improve this question







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






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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
















  • 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












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.






share|improve this answer
















  • 1




    Thank you so much, your answer helped alot!
    – Khizar Muhammad
    1 hour ago






  • 1




    No problem, glad it helped.
    – Blaze
    1 hour ago

















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.






share|improve this answer






















  • 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










  • 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


















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“






share|improve this answer










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.

















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer
















    • 1




      Thank you so much, your answer helped alot!
      – Khizar Muhammad
      1 hour ago






    • 1




      No problem, glad it helped.
      – Blaze
      1 hour ago














    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.






    share|improve this answer
















    • 1




      Thank you so much, your answer helped alot!
      – Khizar Muhammad
      1 hour ago






    • 1




      No problem, glad it helped.
      – Blaze
      1 hour ago












    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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












    • 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












    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.






    share|improve this answer






















    • 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










    • 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















    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.






    share|improve this answer






















    • 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










    • 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













    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 30 mins ago

























    answered 1 hour ago









    Bathsheba

    170k26239363




    170k26239363











    • 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










    • 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

















    • 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










    • 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
















    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











    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“






    share|improve this answer










    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.





















      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“






      share|improve this answer










      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.



















        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“






        share|improve this answer










        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“







        share|improve this answer










        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.









        share|improve this answer



        share|improve this answer








        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.




















            Khizar Muhammad is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery