Please help me fix this

First, I can input a phrase with space, ex. water bottle; second, the last total didn't print out right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Welcome to shopper's calculator!\n" << endl;

    float price;
    float pricetotal=0;
    string product;
    int y = 0;

    cout << "Type in what you want to buy(type q to quit): ";
    cin >> product;

    while(product != "q"){
        y++;
        pricetotal = pricetotal + price;

        cout << "Enter the price: $";
        cin >> price;
        cout << y << "----" << product << "--$" << price << "\n\n";
        cout << "Type in what you want to buy(type q to quit): ";
        cin >> product;
        }

    cout << "Your Total is $" << pricetotal << "\n";
    return 0;
}
You sum up pricetotal = pricetotal + price; before you do cin >> price;!

The value of price there is undefined and the variable itself contains garbage. So you're basically meddling in undefined behaviour.

Just move pricetotal = pricetotal + price; after you cin >> price; and things should work. Mind you, I didn't check for other errors, so fix this first.

About the string input.

cin >> product can only take one word of your input. It stops eating up characters as soon as it sees a space. So you must getline(cin, product); instead.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <iomanip>

// using namespace std ; // avoid; just type in std::cout etc.

int main()
{
    std::cout << "Welcome to shopper's calculator!\n" ;

    double price_total = 0 ; // use double as the default floating point type

    std::string product;

    // http://www.cplusplus.com/reference/string/string/getline/
    while( std::cout << "\nType in what you want to buy (type q to quit): " &&
           std::getline( std::cin, product ) && // read in a complete line
           product != "q" && product != "Q" )
    {
        // http://www.cplusplus.com/reference/string/string/empty/
        if( !product.empty() ) // product contains at least one character
        {
            std::cout << "Enter the price: $ ";
            double price ;
            if( std::cin >> price ) // if price was read successfully
            {
                if( price > 0.0 ) price_total += price ;
                else std::cout << "price must be positive\n" ;
            }

            // http://www.cplusplus.com/reference/ios/ios/clear/
            else
            {
                std::cout << "invalid price: ignored.\n" ;
                std::cin.clear() ; // input failed, clear the failed state of std::cin
            }

            // http://www.cplusplus.com/reference/istream/istream/ignore/
            std::cin.ignore( 1000, '\n' ) ; // throw away the rest of this input line
        }
    }

    // http://www.cplusplus.com/reference/ios/fixed/
    // http://www.cplusplus.com/reference/iomanip/setprecision/
    std::cout << "\nYour Total is $ " << std::fixed << std::setprecision(2) << price_total << "\n";

    // there is an implicit return 0 ; at the end of main()
}
thanks a lot! @sasauke & JL Borges
Topic archived. No new replies allowed.