943,525 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8986
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 4th, 2005
0

Simple C++ program terminate prematurely

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster
tones1986 is offline Offline
179 posts
since Feb 2005
Feb 4th, 2005
1

Re: Simple C++ program terminate prematurely

If you want your program to run more than once (the while loop), you should put
C++ Syntax (Toggle Plain Text)
  1. cout << "\nInput hours worked(0 to quit):";
  2. cin >> hours_worked;
inside the loop:
C++ Syntax (Toggle Plain Text)
  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,
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Feb 4th, 2005
1

Re: Simple C++ program terminate prematurely

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 4th, 2005
0

Re: Simple C++ program terminate prematurely

Quote 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
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Feb 4th, 2005
0

Re: Simple C++ program terminate prematurely

Quote 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]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 4th, 2005
0

Re: Simple C++ program terminate prematurely

Quote 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tones1986 is offline Offline
179 posts
since Feb 2005
Feb 5th, 2005
0

Re: Simple C++ program terminate prematurely

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 10th, 2005
0

Re: Simple C++ program terminate prematurely

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tones1986 is offline Offline
179 posts
since Feb 2005
Feb 10th, 2005
0

Re: Simple C++ program terminate prematurely

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...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bombe is offline Offline
14 posts
since Feb 2005
Feb 10th, 2005
0

Re: Simple C++ program terminate prematurely

Quote 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tones1986 is offline Offline
179 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String operatios
Next Thread in C++ Forum Timeline: classes in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC