Ask coding questions

← Back to all posts
Code won't total
moonlitdeath (0)

I'm trying to run a code that is supposed to come out with a total bill after a price and item amount input and can't figure out why the code isnt working. Any ideas on what i should try?

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int quantity; // contains the amount of items purchased
float itemPrice; // contains the price of each item
float totalBill; // contains the total bill.

cout << setprecision(2) << fixed << showpoint;	// formatted output 
cout << "Please input the number of items bought" << endl;

// Fill in the input statement to bring in the quantity.
    cin >> quantity;

// Fill in the prompt to ask for the price.
    cout << "Please input the total cost of the items" << endl;

// Fill in the input statement to bring in the price of each item.
    cin >> itemPrice;
            
// Fill in the assignment statement to determine the total bill.
    cout << "The total bill is $";

// Fill in the output statement to print total bill,
// with a label to the screen.
    cout << totalBill;
    
    totalBill = quantity * itemPrice;

return 0;

}

Comments
hotnewtop
malvoliothegood (851)

It's a question of the order of operations.
This:

cout << totalBill;
totalBill = quantity * itemPrice;

needs to be this:

totalBill = quantity * itemPrice;
cout << "The total bill is: " << totalBill;