1

希望有人可以提供帮助。我能够毫无错误地编译,我没有发现任何语法错误,但是当我运行它时,它崩溃了。在启动时调试段错误。充分披露,这是功课。我不是要求有人编写这个代码,只是看看我的问题和我现有的代码,也许会指出我做了什么让这件事变得如此糟糕?

问:你找到了一份为期五周的令人兴奋的暑期工作。例如,它每小时支付 15.50 美元。假设您为暑期工作收入支付的总税款为 14%。缴纳税款后,您将净收入的 10% 用于购买下一学年的新衣服和其他配饰,1% 用于购买学习用品。买衣服和学习用品后,你用剩余的 25% 购买储蓄债券。对于您购买储蓄债券的每一美元,您的父母会花费 0.50 美元为您购买额外的储蓄债券。编写一个程序,提示用户输入一个小时的工资率和您每周工作的小时数。然后程序输出以下内容:您的暑期工作税前和税后收入。湾。你花在衣服和其他配饰上的钱。C。你花在学习用品上的钱。d。你用来购买储蓄债券的钱。e. 您父母为您购买额外储蓄债券所花费的钱。

代码:

// 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; 
}
4

2 回答 2

3

我收到了这个错误 在此处输入图像描述

cout << "Enter your hourly rate: " << hourlyrate;

您尝试在初始化之前输出变量。这可能是无意的。

下一行是

cin >> hourlyrate

每个变量都是一样的。您应该初始化它们或不输出它们。

于 2013-09-19T00:55:31.713 回答
1

你确定吗:

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;

我认为你想要:

cout << "Enter your hourly rate: "; 
cin >> hourlyrate; 

代替:

cout << "Enter your hourly rate: "<< hourlyrate; 
cin >> hourlyrate; 
于 2013-09-19T01:11:12.677 回答