0

I'm new to C++ and development in general. Frankly, I have no idea what is going on. I'm just trying to display a string on one line, but the program is giving me a confusing error.

I would really appreciate any help.

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

The output looks like this:

For what month is this? (Please type the name of the month.)

Answer: March

For what year?

Answer: 2008

How much was total sales at the register?

Answer: 26572.89

(lldb)

At "(lldb)" The program just stops... and Xcode indicates something I don't understand on "cout << "Month: " << month << " " << year << end;", telling where an issue is, then a lot of complex debugging info. The indicator is green colored.

Thanks again for any help!!!

10
  • 2
    Perhaps a typo: endl instead of end (note the last letter)? Commented Sep 9, 2015 at 1:25
  • Another problem is that for state_tax_payment = net_sales / state_tax_payment;, you are dividing a non-initialized variable state_tax_payment. Commented Sep 9, 2015 at 1:26
  • Thanks! I'll correct and edit that now. Commented Sep 9, 2015 at 1:32
  • Ok, I initialized the variables at the beginning of the program, and corrected the typo... The build succeeds, but the same error persists. I'll update my code to reflect changes. Commented Sep 9, 2015 at 1:47
  • 1
    1/3 is zero (remainder one). You don't want integer division. Perhaps you want 1.0/3.0 and 2.0/3.0? Commented Sep 9, 2015 at 2:12

2 Answers 2

1

Because state_tax_payment and total_tax_payment are not initialize state_tax_payment = net_sales / state_tax_payment; and county_tax_payment = net_sales / county_tax_payment; lines can be result in undefined behavior

  1. Initialize the all float variables
  2. Assign some value to state_tax_payment and total_tax_payment
  3. Correct the type mention by ' Thomas Matthews'.

Then your program works fine . May be it exit after execution finish. so you can add something like 'getchar()' , std::cin.get() to pause the console.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks you, the corrections are made, but I'm still getting the same error.
month std::__1::string "March" total_tax_payment float 13673.4287 13673.4287 state_tax_payment float 0 0 year int 2008 2008 county_tax_payment float 0 0
^That's in the debug window
Did you assign some value to county_tax_payment
I'm not sure... I thought the following statement did that: county_tax_payment = total_tax_payment * (1/3);
|
1

The actual problem was identified by Tony D.

The debugger in Xcode had a breakpoint set to the particular line of code. I simply had to drag it out of the gutter. For those who don't know, that the green arrow on the left in of the lines of code is a breakpoint. Drag it to the bottom, out of the code, to remove it.

I'm sure I made a total newbie mistake, since I am one, but lesson learned.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.