the code compiles but when i try running the .exe it closes. It looks liek for the 2 seconds its up it works, but then it closes itself. What am i doing wrong? heres the code:

#include <iostream>

using namespace std;

int main()
  {
     // sets variables to be set in the program later on
     double hours_worked, hourly_pay, gross_pay, ss_wh, wh_tax, net_pay, gross_pay_total, ss_wh_total, wh_tax_total, net_pay_total, checks;

     cout << "\nInput hours worked(0 to quit):";
     cin >> hours_worked;


     // Loop to calculate all info
     while (hours_worked > 0)
        {


           // Inputs hourly rate per hour
           cout << "\nInput pay rate per an hour  :";
           cin >> hourly_pay;

          if (hours_worked > 40)
            gross_pay = (hours_worked * hourly_pay) + (hours_worked - 40) * (hourly_pay * 1.5);
          if (hours_worked = 0) 
            cout << "\nTOTALS\n";
            cout << "Total Payroll:        " << gross_pay_total;
            cout << "\nNumber of Check:    " << checks;
            cout << "\nAverage Paycheck:   " << net_pay_total;
            cout << "\n\nTotal SS Withheld:" << ss_wh_total;
            cout << "\nTotal Witholding:   " << wh_tax_total; 


           // Does all calc for taxes and net worth
           if (hours_worked < 40, hours_worked > 0)
           gross_pay = hours_worked * hourly_pay;
           ss_wh = gross_pay * .08;
           wh_tax = gross_pay * .10;
           net_pay = gross_pay - ss_wh - wh_tax;

           // counters
           checks++;
           gross_pay_total = gross_pay_total + gross_pay;
           ss_wh_total = ss_wh_total + wh_tax;
           wh_tax_total = wh_tax_total + wh_tax;
           net_pay_total = net_pay_total + net_pay;    

           // Prints summary info
           cout << "\nGross pay       :" << gross_pay; 
           cout << "\nSS Witheld      :" << ss_wh;
           cout << "\nWitholding tax  :" << wh_tax;           
           cout << "\nNet Pay         :" << net_pay;

        }

        return 0;
  }

I just read the info about students posting requests for answers to their HW questions. This IS a homework problem, but i have legit. tried it and just CANNOT for the life of me figure out where a mistake is. Also for some reason when it didnt end before displaying the answer, it wouldnt accept a 0 to quit the program, im not sure how to add that, any ideas? Cause the while is there, then the 2 if's for the pay differences for overtime etc, any ideas would be truely awesome. thanks!

Recommended Answers

All 14 Replies

If you want your program to run more than once (the while loop), you should put

cout << "\nInput hours worked(0 to quit):";
cin >> hours_worked;

inside the loop:

do
{
   cout << "\nInput hours worked(0 to quit):";
   cin >> hours_worked;
..........................
}
while (hours_worked > 0);

If you have more than one instructions to execute after an if statement, you have to put them inside curly braces:

if (hours_worked < 40, hours_worked > 0)
{
   gross_pay = hours_worked * hourly_pay;
   ss_wh = gross_pay * .08;
   wh_tax = gross_pay * .10;
   net_pay = gross_pay - ss_wh - wh_tax;
}

If you have to test more than one condition, theif statement should be writen as:

if (hours_worked < 40 || hours_worked > 0)

(|| = OR)
Also,

if (hours_worked = 0)

assigns 0 to hours_worked; to test the equality, use ==.
And use code tags.

commented: very helpful +1

frrossk did a great job helping you! Please put the following lines near the end of your code, just above return 0; to make the console display wait:

cin.get();  // trap return
cin.get();  // wait till key pressed

there are a couple of other things to be worked on, but you will get it right.

frrossk did a great job helping you! Please put the following lines near the end of your code, just above return 0; to make the console display wait:

cin.gets();  // trap return
cin.gets();  // wait till key pressed

Yes, I know I was missing somethig :cheesy: Thx, vegaseat :D

Yes, I know I was missing somethig :cheesy: Thx, vegaseat :D

please use cin.get();
not
cin.gets();
my early morning booboo!

Also using if (doublevalue == 0) can give unwanted results since doubles notoriously approximate.

Make sure your totals start at zero!

To break out of the loop properly put this if statement right after entering hours_worked:

if (hours_worked <= 0)
    { 
      cout << "\nTOTALS\n";
      cout << "Total Payroll: " << gross_pay_total;
      cout << "\nNumber of Check: " << checks;
      cout << "\nAverage Paycheck: " << net_pay_total;
      cout << "\n\nTotal SS Withheld:" << ss_wh_total;
      cout << "\nTotal Witholding: " << wh_tax_total;
      
      break;  // done, break out of loop now
    }

please use cin.get();
not
cin.gets();
my early morning booboo!

Also using if (doublevalue == 0) can give unwanted results since doubles notoriously approximate.

Make sure your totals start at zero!

To break out of the loop properly put this if statement right after entering hours_worked:

if (hours_worked <= 0)
    { 
      cout << "\nTOTALS\n";
      cout << "Total Payroll: " << gross_pay_total;
      cout << "\nNumber of Check: " << checks;
      cout << "\nAverage Paycheck: " << net_pay_total;
      cout << "\n\nTotal SS Withheld:" << ss_wh_total;
      cout << "\nTotal Witholding: " << wh_tax_total;
      
      break;  // done, break out of loop now
    }

Thank you all ever so much for your help. It is all greatly appreciated. I will sit down, go over the code i wrote already, go over your proposed changes etc and hopefully get a working program. If not -- ill post ya what i get going on if i cant figure it out myself. You guys are good, but theres no better way to learn by sitting freaking out a program on your own for a while.

Test your program well. For instance put in 40 hours worked at 1 Dollar an hour, check the numbers. Put in 50 hours worked at 1 Dollar an hour. This should uncover a couple of minor mistakes.
Usually time and a half is paid for overtime above 40 hours. Check your Average Paycheck and Total SS Withheld results.

ok....i have started over with my program and added in suggestions that have been posted here. I tried a few others things also but im getting 2-3 error messages when i try to compile the program. Could one of you guys take a walk through the program code and see if theres anything obvious? Ive spent about 2 hours trying to figure out where the mistake might be. lol. Heres the code please help me asap. Its due soon......but its ok. I just need to get it done and its getting to me i cant the simple stuff done.

#include <iostream>


using namespace std;


int main()
{


double hours_worked, hourly_pay, gross_pay, ss_wh, wh_tax, net_pay, ss_wh_total, pay_total, wh_total, wh_tax_total, gross_pay_total, net_pay_total, checks;


do
{
cout << "\nInput hours worked(o to quit):";
cin >> hours_worked;


if (hours_worked < 40, hours_worked > 0)
{
gross_pay = hours_worked * hourly_pay;
ss_wh = gross_pay * .08;
wh_tax = gross_pay *.1;
net_pay = gross_pay - ss_wh - wh_tax;
cout << "\nGross Pay: " << gross_pay;
cout << "\nSS Witheld: " << ss_wh;
cout << "\nWithholding Tax: " << wh_tax;
cout << "\nNet Pay: " << net_pay;
}
if (hours_worked > 40)
{
gross_pay = (hours_worked * hourly_pay) + (hours_worked - 40) * (hourly_pay * 1.5);
ss_wh = gross_pay * .08;
wh_tax = gross_pay *.1;
net_pay = gross_pay - ss_wh - wh_tax;
cout << "\nGross Pay: " << gross_pay;
cout << "\nSS Witheld: " << ss_wh;
cout << "\nWithholding Tax: " << wh_tax;
cout << "\nNet Pay: " << net_pay;
}
if (hours_worked == 0)
{
cout << "\nTotal Payroll: " << gross_pay_total;
cout << "\nNumber of checks: " << checks;
cout << "\nAverage Paycheck: " << net_pay_total;
cout << "\nTotal SS Witheld: " << ss_wh_total;
cout << "\nTotal Witholding: " << wh_tax_total;


break;
}


while (hours_worked > 0);


return 0;


}



}

Hope you guys can help me. Thanks everyone, you guys have been awesome so far.

for starters you don't even ask the user to input the amount they get paid an hour. so you can't really compute anything without...

for starters you don't even ask the user to input the amount they get paid an hour. so you can't really compute anything without...

good point and i JUST caught that. lol. My bad, silly post. Ive got it working....i guess i just need sit down a lil longer before giving in huh? One question though, i set thje variables as doubles because it was told to, how do i make the output display with two decimal places, e.g 20.00 instead of 20? Thanks all.

good point and i JUST caught that. lol. My bad, silly post. Ive got it working....i guess i just need sit down a lil longer before giving in huh? One question though, i set thje variables as doubles because it was told to, how do i make the output display with two decimal places, e.g 20.00 instead of 20? Thanks all.

OK another prob. I changed the whole thing around and got this thing 90% working. I have to run through the thing twice, entering the values of 20 then 10 and then 50 then 10(for hours then pay respectively) the second time it prints out the pay and stuff the figures are wrong(i have an example out put thign here in front of me) ... do i need to set the figures to 0 so that they dont add into one another?? im not sure. Also the loop seems to work fine, but when you choose 0 ...it should quit the loop and print a summary of the TOTAL payroll, number of checks(notice my counter), ave paycheck, total SS, and total WH but for some reason i cant get that stuff to print after ive exited the loop. Any help would be wonderful help!!!

#include <iostream>

using namespace std;

int main()
  {
  
  float hours_worked, hourly_rate, gross_pay, ss_wh, ss_withheld, wh_tax, net_pay, checks, total_payroll, ave_paycheck, total_ss_wh, total_wh;
  
  cout << "Input number of hours worked:(0 to quit)";
  cin >> hours_worked;
  
while (hours_worked > 0)
   {
    
    if (hours_worked > 40)
        
        { 
        cout << "input hourly rate of pay:";
        cin >> hourly_rate;
        gross_pay = (hours_worked * hourly_rate) + ((hours_worked - 40) * (hourly_rate * 1.5));
        ss_wh = gross_pay * .08;
        wh_tax = gross_pay *.1;
        net_pay = gross_pay - ss_wh - wh_tax;
        checks++;
        total_payroll = total_payroll + gross_pay;
        total_ss_wh = total_ss_wh + ss_wh;
        total_wh = total_wh + wh_tax;
        }
        
    if (hours_worked > 0 , hours_worked < 40)
        
        {
        cout << "input hourly rate of pay:";
        cin >> hourly_rate;
        gross_pay = (hours_worked * hourly_rate);
        ss_wh = gross_pay * .08;
        wh_tax = gross_pay *.1;
        net_pay = gross_pay - ss_wh - wh_tax;
        checks++;
        total_payroll = total_payroll + gross_pay;
        total_ss_wh = total_ss_wh + ss_wh;
        total_wh = total_wh + wh_tax;
        }
    
    ave_paycheck = total_payroll / checks;

    cout << "\nGross pay:      " << gross_pay;
    cout << "\nSS Withheld:    " << ss_wh;
    cout << "\nWithholding Tax:" << wh_tax;
    cout << "\nNet Pay:        " << net_pay;
    
    cout << "\n\nInput hours worked:(0 to quit)";
    cin >> hours_worked;
    
    }
    
    if (hours_worked == 0)
        cout << "TOTALS";
        cout << "\n\nTotal SS Withheld:" << total_ss_wh;
        cout << "\nTotal Witholding:   " << total_wh;
    
    return 0;
    }

So yea i have edited a few things since the above post. But i still get a really weird out put. If you copy the above code and try these numbers:

Hours: 20
Pay: 10

should get:
200, 16, 20, 164 as the 4 printed out puts.

hours: 50
pay: 10

should get:
550, 44, 55, 451 as the 4 printed out puts (get weird ones, off by a hundred to twenty.)

No choose 0(zero) as hours worked.

The out put should say:
Total payroll : 750
checks: 2
ave paycheck: 375

total ss wh: 60:
total wh: 75

HELP!!!!

Where did i go wrong. You have the code that i used above...

I added these lines as output to replace:

OLD:

if (hours_worked == 0)
        cout << "TOTALS";
        cout << "\n\nTotal SS Withheld:" << total_ss_wh;
        cout << "\nTotal Witholding:   " << total_wh;

with this code:

cout << "\n\nTOTALS";
        
        cout << "\nTotal Payroll:      " << total_payroll;
        cout << "\nNumber of checks:   " << checks;
        cout << "\nAverage Paycheck:   " << ave_paycheck;
        cout << "\n\nTotal SS Withheld:" << total_ss_wh;
        cout << "\nTotal Witholding:   " << total_wh;
        
    
    cin.get();  
    cin.get();

Instead of

if (hours_worked > 0 , hours_worked < 40)

you need

if (hours_worked <= 40)

because you reach this statement only if hours_worked > 0 (tested in the while loop)
The weird values you get is because you use uninitializsed variables; that means they can have any possible value:

float check;//not initialized; will have a random value
...
if (hours_worked > 40)
{
  ...
  check++;//chech will have the same random value + 1
}

You have to initialise them after you are declaring them:

float check=0, total_payroll=0,...

Instead of

if (hours_worked > 0 , hours_worked < 40)

you need

if (hours_worked <= 40)

because you reach this statement only if hours_worked > 0 (tested in the while loop)
The weird values you get is because you use uninitializsed variables; that means they can have any possible value:

float check;//not initialized; will have a random value
...
if (hours_worked > 40)
{
  ...
  check++;//chech will have the same random value + 1
}

You have to initialise them after you are declaring them:

float check=0, total_payroll=0,...

Awesome!!! Thank you ever so much. Now the only other problem that i am still getting is that when i am using the over time pay -- the figures come out incorrect. According to my assing i need to have something like:

If hours worked was more than 40, then is it 40 tims the hourly rate plus the extra hours over 40 time 1 1/2 times the hourly rate. The statement i used:

gross_pay = (hours_worked * hourly_rate) + ((hours_worked - 40) * (hourly_rate * 1.5));

Seems correct does it not....

But if you try using the code that is now correct by the looks of it:

#include <iostream>

using namespace std;

int main()
  {

  double hours_worked = 0, hourly_rate = 0, gross_pay = 0, ss_wh = 0, ss_withheld = 0, wh_tax = 0, net_pay = 0, total_payroll = 0, ave_paycheck = 0, total_ss_wh = 0, total_wh = 0;
  float checks = 0;

  cout << "PAYROLE CALCULATOR\n\n";
  cout << "Input number of hours worked (0 to quit):";
  cin >> hours_worked;

while (hours_worked > 0)
   {

    if (hours_worked <= 40)

        {
        cout << "input hourly rate of pay:";
        cin >> hourly_rate;
        gross_pay = (hours_worked * hourly_rate);
        ss_wh = gross_pay * .08;
        wh_tax = gross_pay *.1;
        net_pay = gross_pay - ss_wh - wh_tax;
        checks++;
        total_payroll += gross_pay;
        total_ss_wh += ss_wh;
        total_wh += wh_tax;
        }

     if (hours_worked > 40)    

        { 
        cout << "Input hourly rate of pay:";
        cin >> hourly_rate;
        gross_pay = (hours_worked * hourly_rate) + ((hours_worked - 40) * (hourly_rate * 1.5));
        ss_wh = gross_pay * .08;
        wh_tax = gross_pay *.1;
        net_pay = gross_pay - ss_wh - wh_tax;
        checks++;
        total_payroll += gross_pay;
        total_ss_wh += ss_wh;
        total_wh += wh_tax;
        }

    ave_paycheck = total_payroll / checks;

    cout << "\nGross pay:      " << gross_pay;
    cout << "\nSS Withheld:    " << ss_wh;
    cout << "\nWithholding Tax:" << wh_tax;
    cout << "\nNet Pay:        " << net_pay;

    cout << "\n\nInput hours worked (0 to quit):";
    cin >> hours_worked;

    }

    cout << "\n\nTOTALS";
    cout << "\n\nTotal Payroll:      " << total_payroll;
    cout << "\nNumber of checks:   " << checks;
    cout << "\nAverage Paycheck:   " << ave_paycheck;
    cout << "\n\nTotal SS Withheld:" << total_ss_wh;
    cout << "\nTotal Witholding:   " << total_wh;


    cin.get();  
    cin.get();
    return 0;

    }

You should get:

PAYROLL CALCULATOR 

Enter hours worked (0 to quit): [B]20[/B]
Enter pay rate: [B]10[/B]
Gross Pay:        200.00
SS Withheld:       16.00
Withholding Tax:   20.00
Net Pay:          164.00   

Enter hours worked (0 to quit): 50
Enter pay rate: [B]10[/B]
Gross Pay:        550.00
SS Withheld:       44.00
Withholding Tax:   55.00
Net Pay:          451.00 

Enter hours worked (0 to quit): 0

TOTALS

Total Payroll:      750.00
Number of Checks:        2
Average Paycheck:   375.00 

Total SS withheld:   60.00
Total Withholding:   75.00 

But when i try doing this...the over time values are off slighty still ---- Do i need to empty a value or reassign that value to zero maybe? The values im getting are:

650
52
65
533

not quite sure what its/im doing wrong. But once again thank you ever so much for your help guys. Your all getting me to learn this stuff faster than either mt TA or my teacher could ever have done(seriously!). Thanks all, hope you can help me figure out this last part ive been struggling with since the beginning. (the wierd numbers i got before were like 1.3eNMAM and stuff, and it was for the totals area, ive always been getting numbers that are a little off for the overtime pay -- so im guessing its the over time equation im using, although ive asked some people and they say it seems correct.

(last thing, these values are dollar values and they need to be with 2 decimal places....eg. 12.00 instead of 12. But when they are printed they do not have decimal places, so i need to caste them or something? -- and if so to what type?)

When you write your "if" statement you must tell the computer what you want in the statement by using brackets. { "What you want if statement to consist of" }

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.