C++ Segfault No 编译错误找不到原因

标签 c++

希望有人能提供帮助。我可以毫无错误地编译,我没有发现任何语法错误,但是当我运行它时,它崩溃了。在启动时调试段错误。全面披露,这是作业。我不是要找人来编写这个代码,只是看看我的问题和我现有的代码,也许会指出我所做的严重破坏了这个代码?

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

代码:

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

最佳答案

我收到这个错误 enter image description here

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

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

下一行是

cin >> hourlyrate

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

关于C++ Segfault No 编译错误找不到原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884518/

相关文章:

c++ - 当用户在 tableView 中使用键盘箭头更改行时如何发出信号?

c++ - 如何使用从 C++ 中的 vector 读取的值启动二叉树?

c++ - 为什么 const double reference 可以分配给 int 而不是 non const ?

c++ - 使用指针作为返回类型实现模板化接口(interface)的函数 -> 冲突返回类型错误

c++ - 在Qt/C++中通过光标选择一个单词并获取它的位置

c++ - 您必须从较早版本的 BDS [$(BDS)\include\atl\*.*] 复制 ATL 文件才能使用 ATL?

c++ - 检查文件格式正确性的最佳方法

c++ - 我可以使用 tie 将一对元素放入 vector 中吗?

c++ - 防止使用 glibc 头文件

c++ - extern 数组出现 undefined reference 错误,但适用于 int