user3834500
user3834500

Reputation: 13

Having trouble multiplying variables in c++. (c++ doesn't want to multiply them unless hardcoded)

I recently picked up cpp and have been doing a couple very small projects to familiarize myself with the language, and I've run into a dilemma that I can't figure out no matter where I look on the web.

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.875, gross = 0, tax = (gross*salesTax), total = (salesTax + gross); //Variables 

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable

    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax << "% \n\nTotal: $" << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;
}

As you can tell I was building a small cash register type program, however the problem is that whenever run I only get an output that looks something like this.

https://i.sstatic.net/VMdlL.jpg (can't upload images til' 10 points so hopefully imgur is fine :))

Any help would be appreciated (don't mind if my math is off I'm more interested in getting my program to work correctly than I am in getting the math perfect.)

Upvotes: 0

Views: 3101

Answers (4)

roelofs
roelofs

Reputation: 2170

You are calculating all the values depending on gross based on the initial value of 0. Get the gross input, and then calculate tax and total, before outputting.

A few other fixes:

  • 8.75% sales tax is 0.0875
  • You added just the salestax percentage, instead of the calculated value
  • You want to display the final total to 2 decimal places

I've added the fixes below.

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.0875;
    double gross = 0;
    double tax = 0;
    double total = 0; //Variables with initial values

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable
    tax = (gross*salesTax); //calculate tax
    total = (tax + gross); //calculate total

    cout.precision(2);
    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax*100 << "% \n\nTotal: $" << fixed << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;

}

Upvotes: 2

user3064479
user3064479

Reputation: 56

C and C++ execute code line by line, tax and total is initialize by the older value of gross nad salesTax value to correct you program try below code

#include <iostream>
using namespace std;

int main(){
    double salesTax = 0.875, gross = 0, tax = 0, total = 0; //Variables 

    cout << "Welome to StoreMart!" << endl;

    cout << "How much is your total? "; //asks user for an amount of money spent (not including tax).
    cin >> gross; //saves total spent in the gross variable

    tax = (gross*salesTax); //calculating new tax after accepting gross value from user
    total = (salesTax + gross);

    cout << "\n\nThat brings your total with tax to $" << total << "." << endl; //prints the users total with tax.

    cout << "Price: " << gross << "$ \n\nSales Tax: $" << tax << " @" <<
        salesTax << "% \n\nTotal: $" << total << endl; //prints out something similar to the receipts you get at Wal-Mart.

    return 0;
}

Upvotes: 0

M Katz
M Katz

Reputation: 5426

I believe the fundamental problem you are having is that you think that by declaring the variables at the top you are defining the relationship between them for the whole program. But it doesn't work that way. You input the gross from the user around line 10, but up above, you've already assigned the total based on the initial value of gross.

You have to understand that the variables are computed where you declare them. It would be more clear if you used a separate line for each variable.

const double salesTax = 0.875;
double gross = 0; // set to zero now but input later
double tax = 0; // no point setting the tax until we know the gross
and so on...

then later you input the gross

cin >> gross;

then layer you compute the tax, etc. based on that input:

tax = (gross*salesTax); // now we can actually compute the tax because we have the gross
.... and so on

Upvotes: 0

user3854447
user3854447

Reputation: 324

When you program you must always think in the same ways computers "think." Code is followed from the top of the file to the bottom of the file. That is the relationships you encoded at the top of main aren't symbolic - you have not told the computer to update the values of tax and total to reflect the new value of gross that is retrieved from the user. Try placing the tax and total assignment statements AFTER you retrieve the value of gross from the user.

Upvotes: 0

Related Questions