Jump to content

Getting A "nan" return from a calculation.

Raderg
Go to solution Solved by 79wjd,

You need to pull the return finalpay in the netpay function out of the else block. 

 

p.s. it's choice not choise 

Hello I'm new to programming and am practicing functions in C++ by making a simple pay predictor. When trying to calculate my net pay it tells me nan? Why does it do this and how do I fix it? Here is my code.

/** Made by Gabriel Rader. This is meant to be a practice of Functions.The "tax" calculated in net pay is a random amount and is fixed.
 So it will not be accurate to your area and does not account for deductions.  **/
#include <cstdio>
#include <iostream>
#include <cmath>

using namespace std;

float gross_pay (float pay, float hours ){
    float finalpay, regpay, otpay, othours;
// pay
if (hours<= 40) {
        finalpay=pay*hours;
return finalpay;
    }
//overtime pay
    else{
        regpay=pay*40;
        othours=hours-40;
        otpay=othours*(pay+(pay*.5));
        finalpay=regpay+otpay;
        return finalpay;
    }
}
float netpay (float pay, float hours) {
    float finalpay, regpay, otpay, othours, tax;
    tax=.07;
// net pay
if (hours<=40){
    finalpay=pay*hours-((pay*hours)*tax);
}
//overtime net pay
else{
   regpay=pay*40;
        othours=hours-40;
        otpay=othours*(pay+(pay*.5));
        finalpay=(regpay+otpay)-((regpay+otpay)*tax);
        return finalpay;
}
}
int main(){
    char choise;
    float hours, pay;
    cout << "Hello welcome to the pay predictor.";
    BEGINING:
    cout <<"\nBefore we begin please let me know your pay and your hours. \n";
    cout << "pay "; cin >> pay;
    cout << "hours "; cin >> hours;
   cout << "Thank you now what would you like to know your gross pay (g) or net pay (n)\n";
   CHOISE:
   cout << "pay? "; cin >> choise;
   if (choise=='n'){
    float npay=netpay(pay,hours);
    cout << "thank you your pay is " <<npay;
    cout << "\nWould you like to go again?\n";
    cout << "'y','n' "; cin >> choise;
    if (choise=='y'){
        goto BEGINING;
    }
else{
    return 0;
}
   }
   else if (choise== 'g'){
    float gpay=gross_pay(pay,hours);
    cout << "thank you your pay is " <<gpay;
    cout << "\nWould you like to go again?\n";
    cout << "'y','n' "; cin >> choise;
    if (choise=='y'){
        goto BEGINING;
    }
else{
    return 0;
}
   }
   else{
    cout << "Your input is invalid please try again. ";
    goto CHOISE;
   }

return 0;
}

and a screen shot of the program 

paypredictorbug.png

Link to comment
Share on other sites

Link to post
Share on other sites

You need to pull the return finalpay in the netpay function out of the else block. 

 

p.s. it's choice not choise 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Raderg said:

Thank you and I corrected "Choice".

 

Something else I noticed, where you have otpay=othours*(pay+(pay*.5));, you could change that to otpay=othours*(pay*1.5); to clean it up a little bit.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

Please rewrite your code to remove all those GOTO's.

GOTO's are frowned upon (putting it politely) in most high-level languages and are never needed.

It leads to spaghetti code that even you yourself will not be able to make sense off in a few months, let alone another poor soul who has to work with your code.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you @Unimportant and @TheKDub I cleaned up my math and I also got rid of the "goto" statements. I put the code in Main in a while Loop to be executed while "Choice2" was 'y'. Then all i did was take the if statements that served the purpose of going back to the beginning blank to restart the code. Thank you again for your feedback I'm trying to teach my self and don't really know the do's and don'ts yet.  

Link to comment
Share on other sites

Link to post
Share on other sites

Really? Nobody here defined NAN despite the OP stating they're a beginner... Well I'm going to assume you don't know what it is so here's a rough definition:

NAN = Not a Number and is stuff like negative or positive infinity and potentially other weirdness I can't remember at the moment. That should explain the theory of why stuff isn't working, I'm too lazy to read through what you're doing to find your error and it seems a the other comments here focus on that anyway. And yes don't use gotos... everything a goto can do an if/for/while/function/whatever can do and it can do it in a much more predictable way.


Also some comments on your dev environment, you seem to be compiling your code within cmd which is fine especially for a beginner but for something like C/C++ I'd strongly suggest using an IDE. My favorite is CLion but it costs money unless you can get an opensource or student license... Mainly an IDE helps with managing multiple files which is something I strongly advise you to look into rather than cramming everything into a single cpp file.


Oh and I've kind of stopped using namespaces after they caused me a bit of a headache in one of my assignments where in combination with a nice series of includes they started conflicting with some of my functions in my classes since they were named the same and it didn't make any sense to name my functions differently. Not using namespaces forces me to write std::cout or glm::rotate() which makes it more obvious where that's from even if it's annoying to type and increases the length of my code... that said I still use namespaces regularly since they're just so convenient until they start being annoying.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I do have an IDE, code blocks, it's free.  using files within the code is going to be my next topic to learn. Thank you for the explanation!

Link to comment
Share on other sites

Link to post
Share on other sites

O haha reading files sucks in c++.. so does the string manipulation, codeblocks also doesn't offer GET POST libraries (dunno what ide's do if I'm being honest). I ended up using sample code for getting a line in a file.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×