C++ Segfault No compile errors can't find cause
Hoping someone can help. I'm able to compile with no error, I'm not
finding any syntax errors but when I run this, it crashes. Debug segfaults
on launch. Full disclosure, this is homework. I'm not asking for someone
to code this, just look at my problem and my existing code and maybe point
me toward what I did that broke this so badly?
Question: You found an exciting summer job for five weeks. It pays, say,
$15.50 per hour. Suppose that the total tax you pay on your summer job
income is 14%. After paying the taxes you spend 10 % of your net income to
buy new clothes and other accessories for the next school year and 1% to
buy school supplies. After buying clothes and school supplies, you use 25%
of the remaining money to buy savings bonds. For each dollar you spend to
buy savings bonds, your parents spend $0.50 to buy additional savings
bonds for you. Write a program that prompts the user to enter the pay rate
for an hour and the number of hours you worked each week. The program then
outputs the following: a. Your income before and after taxes from your
summer job. b. The money you spend on clothes and other accessories. c.
The money you spend on school supplies. d. The money you spend to buy
savings bonds. e. The money your parents spend to buy additional savings
bonds for you.
Code:
// Libraries defined
#include <iomanip>
#include <iostream>
using namespace std;
//Main function
int main ()
{
//Input variables
double hourlyrate;
double hweek1;
double hweek2;
double hweek3;
double hweek4;
double hweek5;
//Output variables
double beforetax;
double netincome;
double clothmoney;
double suppliesmoney;
double moneyonbonds;
double additionalbonds;
double remain;
//This statement takes care of the decimal places
cout << fixed << showpoint << setprecision(2);
//Input from user
cout << "Enter your hourly rate: " << hourlyrate;
cin >> hourlyrate;
cout << "Week 1: " << hweek1;
cin >> hweek1;
cout << "Week 2: " << hweek2;
cin >> hweek2;
cout << "Week 3: " << hweek3;
cin >> hweek3;
cout << "Week 4: " << hweek4;
cin >> hweek4;
cout << "Week 5: " << hweek5;
cin >> hweek5;
//Mathematics
beforetax = hourlyrate * (hweek1 + hweek2 + hweek3 + hweek4+
hweek5) ;
netincome = beforetax - beforetax * 0.14;
clothmoney = netincome * 0.1;
suppliesmoney = netincome * 0.01;
remain = netincome - clothmoney - suppliesmoney;
moneyonbonds = remain * 0.25;
additionalbonds = static_cast<double>(moneyonbonds) * .50;
//Output to user
cout << endl << "Income before tax = $" << beforetax << endl
<< "Net income = $" << netincome << endl << "Money for
clothes/accesories = $"
<< clothmoney << endl << "Money for supplies = $"<< suppliesmoney << endl
<< "Money for saving bonds = $" << moneyonbonds << endl
<< "Additional saving bonds money = $" << additionalbonds;
return 0;
}
No comments:
Post a Comment