我是C ++和开发人员的新手。坦率地说,我不知道发生了什么。我只是想在一行上显示一个字符串,但该程序给了我一个令人困惑的错误。
我真的很感激任何帮助。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// This program calculates and displays to user
int main()
{
// Constants are state and county taxes.
const float STATE_TAX_RATE = 0.04,
COUNTY_TAX_RATE = 0.02;
// float variables are :
float gross_sales = 0,
net_sales = 0,
county_tax_payment = 0,
state_tax_payment = 0,
total_tax_payment = 0;
// string variable
string month;
// integer variable
int year;
// Get month, year, and sales information from user
cout << "For what month is this? (Please type the name of the month.)\nAnswer: ";
getline(cin, month);
cout << "For what year?\nAnswer: ";
cin >> year;
cout << "How much was total sales at the register?\nAnswer: ";
cin >> gross_sales;
// Calculate the net income
net_sales = (gross_sales)/(1 + STATE_TAX_RATE + COUNTY_TAX_RATE);
// Calculate total taxes paid.
total_tax_payment = (gross_sales - net_sales);
// cout << total_tax_payment; // output test
// Calculate total state taxes paid.
state_tax_payment = (total_tax_payment * (2.0/3.0));
// cout << state_tax_payment; //output test
// Calculate county taxes paid.
county_tax_payment = (total_tax_payment * (1.0/3.0));
//Display the information
cout << "Month: " << month << " " << year << endl;
cout << "--------------------" << endl;
cout << "Total collected:\t $" << fixed << setw(9) << setprecision(2) << right << gross_sales << endl;
cout << "Sales: \t\t\t\t $" << fixed << setw(9) << setprecision(2) << right << net_sales << endl;
cout << "County Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << county_tax_payment << endl;
cout << "State Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << state_tax_payment << endl;
cout << "Total Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << total_tax_payment << endl;
return 0;
}
输出如下:
这个月是几月? (请输入月份名称。)
答案:三月
哪一年?
答案: 2008
注册总销售额是多少?
答案: 26572.89
(lldb)
在“(lldb)”程序刚停止......并且Xcode表示我对“cout&lt;&lt;”月份不明白的事情:“&lt;&lt;&lt;&lt;&lt;&lt;”&lt;&lt;&lt; ;年&lt;&lt; end;“,告诉问题在哪里,然后是很多复杂的调试信息。指示灯为绿色。
再次感谢您的帮助!!!
答案 0 :(得分:1)
由于total_tax_payment
和state_tax_payment = net_sales / state_tax_payment;
未初始化county_tax_payment = net_sales / county_tax_payment;
且state_tax_payment
行可能导致未定义的行为
total_tax_payment
和{{1}} 然后你的程序运行正常。执行结束后可能会退出。所以你可以添加像'getchar()',std :: cin.get()这样的东西来暂停控制台。
答案 1 :(得分:1)
实际问题由Tony D确定。
Xcode中的调试器有一个设置为特定代码行的断点。我只需将它拖出阴沟。对于那些不知道的人来说,代码行左边的绿色箭头是一个断点。将其拖到代码中的底部,将其删除。
我确信我犯了一个全新的错误,因为我是一个,但是吸取了教训。