Simple C++ program terminate prematurely

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Simple C++ program terminate prematurely

 
0
  #1
Feb 4th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Simple C++ program terminate prematurely

 
1
  #2
Feb 4th, 2005
If you want your program to run more than once (the while loop), you should put
  1. cout << "\nInput hours worked(0 to quit):";
  2. cin >> hours_worked;
inside the loop:
  1. do
  2. {
  3. cout << "\nInput hours worked(0 to quit):";
  4. cin >> hours_worked;
  5. ..........................
  6. }
  7. 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,
  1. if (hours_worked = 0)
assigns 0 to hours_worked; to test the equality, use ==.
And use code tags.
Last edited by frrossk; Feb 4th, 2005 at 3:16 am. Reason: missing something
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Simple C++ program terminate prematurely

 
1
  #3
Feb 4th, 2005
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:
[php]
cin.get(); // trap return
cin.get(); // wait till key pressed
[/php]
there are a couple of other things to be worked on, but you will get it right.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Simple C++ program terminate prematurely

 
0
  #4
Feb 4th, 2005
Originally Posted by vegaseat
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:
[php]
cin.gets(); // trap return
cin.gets(); // wait till key pressed
[/php]
Yes, I know I was missing somethig :cheesy: Thx, vegaseat
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Simple C++ program terminate prematurely

 
0
  #5
Feb 4th, 2005
Originally Posted by frrossk
Yes, I know I was missing somethig :cheesy: Thx, vegaseat
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:
[php] 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
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Simple C++ program terminate prematurely

 
0
  #6
Feb 4th, 2005
Originally Posted by vegaseat
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:
[php] 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
}
[/php]

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Simple C++ program terminate prematurely

 
0
  #7
Feb 5th, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Simple C++ program terminate prematurely

 
0
  #8
Feb 10th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 14
Reputation: bombe is an unknown quantity at this point 
Solved Threads: 1
bombe bombe is offline Offline
Newbie Poster

Re: Simple C++ program terminate prematurely

 
0
  #9
Feb 10th, 2005
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Simple C++ program terminate prematurely

 
0
  #10
Feb 10th, 2005
Originally Posted by bombe
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC