Marriott81
Marriott81

Reputation: 275

Adding not multiplying

I am teaching myself C++, starting on the basics have written this:

// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string mystr;
    int price = 0;
    int quantity = 0;
    int total = price * quantity;

    cout << "------------------------------------------------------" << '/n';
    cout << "-----------------Welcome to the shop!-----------------" << '/n';
    cout << "------------------------------------------------------" << '/n';
    cout << "Enter price of item" << '/n';
    getline(cin, mystr);
    stringstream(mystr) >> price;
    cout << "How Many do you want?" << '/n';
    getline(cin, mystr);
    stringstream(mystr) >> quantity;
    cout << "you want this many: " << quantity << '/n';
    cout << "at this price: " << price << '/n';
    cout << "this would cost you: " << total << " pounds" << '/n';

    if (total >= 20)
    {
        cout << "here is a discount of " << total / 20 << '/n';
    }
    else if (total >= 10)
    {
        cout << "here is a discount of " << total / 10 << '/n';
    }
    else
    {

        cout << "sorry no discount" << '/n';
    };
}

My only problem is - its adding the total price instead of multiplying. I feel I am missing something very obvious but after an hour I cant seem to find out what it is, I have tried declaring total further down the code which has not worked, I have also tried putting the total in brackets still nothing.

What am I missing?

-- as an example -- 10 units at the cost of 10 each, should come out at 100, not 20 as it does on my code

not adding

Upvotes: 0

Views: 122

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

It's not doing anything with the total price, since it'll always be 0.

int total = price * quantity;

The result of the multiplication is performed and "saved" at this point, and doesn't change later even if price and quantity do.

You should put this line after the lines where you truly set the values of price and quantity.

As for your question about "adding not multiplying", with the fix in place as above the value output is correct, so you must be doing something wrong that we can't see. Check that you're running this code, and not some other piece of code.

Also, you've consistently written /n, whereas it should be \n (which further suggests that your screenshot is not from running this code). In fact, the two before your prompt for input should be endl, to ensure that the prompt is flushed to the console.

Upvotes: 4

cnd
cnd

Reputation: 33784

auto total = [&]() { return price * quantity; };

and then use total()

http://coliru.stacked-crooked.com/a/27ba0fa6978ec9e1

Upvotes: 1

rcs
rcs

Reputation: 7227

you are calculating the total at the wrong place. Initially, you calculate total = price * quantity, with price = 0 and quantity = 0, and this will assign 0 to total. Then after inputting the quantity and price, you do not recalculate the total, so it gives you the wrong result.

My suggestion is to put total = price * quantity; after stringstream(mystr) >> quantity;

Upvotes: 2

Related Questions