C++ error: The right operand of '*' is a garbage value

Viewed 98

I finally completed my code and when I submit it I received a couple errors. It keeps telling me

73:15  Incorrect spacing around >=.
73:31  Incorrect spacing around <=.

for this line, I've tried putting it together and no change

if (quantity >=5 && quantity <=9)

I have checked my entire code multiples times (my program shows dots where spaces are) and I cannot find any extra or unplanned spaces.

The error message The right operand of '*' is a garbage value is emitted in regards to this line:

totalSavings = pricePerDisc * quantity * discount;

Can anyone help me out please?

int main()
{
    //Declare Constant variables
    const double DISC_GOLF_RETAIL = 14.96;
    const double ULTIMATE_RETAIL = 20.96;
    const double DISCOUNT1 = 8;
    const double DISCOUNT2 = .16;
    const double DISCOUNT3 = .24;
    const double DISCOUNT4 = .32;

    //Declare variables
    int quantity;
    double pricePerDisc;
    double totalSavings;
    double afterSavings;
    double total;
    char userInput;
    double discount;
    string discType;
    string disc1 = "Ultimate Disc";
    string disc2 = "Disc-Golf Disc";

    //Display title 
    cout << "Welcome to the Flying-Disc Shop!" << "\n" << endl;

    //Prompt the user for input
    cout << "Enter 'u' for ultimate discs and 'g' for disc golf: ";
    cin >> (userInput);
    cout << endl;

    switch (userInput)
    {
    case 'u':
    case 'U':
        discType = disc1;
        pricePerDisc = ULTIMATE_RETAIL;
        cout << "Enter the number of Ultimate Disc(s): ";
        cin >> (quantity);
        cout << endl;
        break;
    
    case 'g':
    case 'G':
        discType = disc2;
        pricePerDisc = DISC_GOLF_RETAIL;
        cout << "Enter the number of Disc-Golf Disc(s): ";
        cin >> (quantity);
        cout << endl;
        break;
    
    default:
        cout << "Invalid disc type." << endl;
        return 0;
    }
    if (quantity <= 0)
    {
        cout << quantity << " is an invalid number of discs.\n";
        return 0;
    }
    if (quantity >=5 && quantity <=9)
    {
        discount = DISCOUNT1 / 100;
    }
    else if (quantity >=10 && quantity <=19)
    {
        discount = DISCOUNT2;
    }
    else if (quantity >=20 && quantity <=29)
    {
        discount = DISCOUNT3;
    }
    else if (quantity >=30)
    {
        discount = DISCOUNT4;
    }
    totalSavings = pricePerDisc * quantity * discount;
    afterSavings = pricePerDisc - (pricePerDisc * discount);
    total = quantity * pricePerDisc - totalSavings;

    cout << "------------Receipt------------" << endl;
    cout << "     Disc Type: " << discType << endl;
    cout << "      Quantity: " << quantity << endl;
    cout << fixed << setprecision(2);
    cout << "Price per Disc: " << "$ " << setw(12) << afterSavings << endl;
    cout << " Total Savings: " << "$ " << setw(6) << "-" << totalSavings 
        << endl;
    cout << "         Total: " << "$ " << setw(12) << total << endl;
    return 0;
    
}
2 Answers

I like the verbose messages from gcc static analyzer:

<source>: In function 'int main()':
<source>:79:18: warning: use of uninitialized value 'discount' [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
   79 |     totalSavings = pricePerDisc * quantity * discount;
      |     ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'int main()': events 1-4
    |
    |    4 | int main()
    |      |     ^~~~
    |      |     |
    |      |     (1) entry to 'main'
    |......
    |   21 |     double discount;
    |      |            ~~~~~~~~
    |      |            |
    |      |            (2) region created on stack here
    |      |            (3) capacity: 8 bytes
    |   22 |     string discType;
    |   23 |     string disc1 = "Ultimate Disc";
    |      |                    ~~~~~~~~~~~~~~~
    |      |                    |
    |      |                    (4) calling 'std::__cxx11::basic_string<char>::basic_string<>' from 'main'
    |
    +--> 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 5-8
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:634:7:
           |  634 |       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
           |      |       ^~~~~~~~~~~~
           |      |       |
           |      |       (5) entry to 'std::__cxx11::basic_string<char>::basic_string<>'
           |......
           |  638 |         if (__s == 0)
           |      |         ~~
           |      |         |
           |      |         (6) following 'false' branch (when '__s' is non-NULL)...
           |......
           |  641 |         const _CharT* __end = __s + traits_type::length(__s);
           |      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                                                        |
           |      |                                                        (7) ...to here
           |      |                                                        (8) calling 'std::char_traits<char>::length' from 'std::__cxx11::basic_string<char>::basic_string<>'
           |
           +--> 'static constexpr std::size_t std::char_traits<char>::length(const char_type*)': events 9-11
                  |
                  |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/char_traits.h:383:7:
                  |  383 |       length(const char_type* __s)
                  |      |       ^~~~~~
                  |      |       |
                  |      |       (9) entry to 'std::char_traits<char>::length'
                  |......
                  |  386 |         if (std::__is_constant_evaluated())
                  |      |         ~~
                  |      |         |
                  |      |         (10) following 'false' branch...
                  |......
                  |  389 |         return __builtin_strlen(__s);
                  |      |                ~~~~~~~~~~~~~~~~~~~~~
                  |      |                                |
                  |      |                                (11) ...to here
                  |
           <------+
           |
         'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 12-13
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:641:56:
           |  641 |         const _CharT* __end = __s + traits_type::length(__s);
           |      |                                     ~~~~~~~~~~~~~~~~~~~^~~~~
           |      |                                                        |
           |      |                                                        (12) returning to 'std::__cxx11::basic_string<char>::basic_string<>' from 'std::char_traits<char>::length'
           |  642 |         _M_construct(__s, __end, forward_iterator_tag());
           |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                     |
           |      |                     (13) calling 'std::__cxx11::basic_string<char>::_M_construct<const char*>' from 'std::__cxx11::basic_string<char>::basic_string<>'
           |
           +--> 'void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 14-15
                  |
                  |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.tcc:217:7:
                  |  217 |       basic_string<_CharT, _Traits, _Alloc>::
                  |      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  |      |       |
                  |      |       (14) entry to 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
                  |......
                  |  248 |       }
                  |      |       ~
                  |      |       |
                  |      |       (15) calling 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
                  |
                  +--> 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag)::_Guard::~_Guard() [with _InIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 16-18
                         |
                         |  238 |           ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
                         |      |           ^           ~~                                        ~
                         |      |           |           |                                         |
                         |      |           |           |                                         (18) ...to here
                         |      |           |           (17) following 'false' branch...
                         |      |           (16) entry to 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard'
                         |
                  <------+
                  |
                'void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': event 19
                  |
                  |  248 |       }
                  |      |       ^
                  |      |       |
                  |      |       (19) returning to 'std::__cxx11::basic_string<char>::_M_construct<const char*>' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard'
                  |
           <------+
           |
         'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': event 20
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:642:21:
           |  642 |         _M_construct(__s, __end, forward_iterator_tag());
           |      |         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                     |
           |      |                     (20) returning to 'std::__cxx11::basic_string<char>::basic_string<>' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
           |
    <------+
    |
  'int main()': events 21-22
    |
    |<source>:23:20:
    |   23 |     string disc1 = "Ultimate Disc";
    |      |                    ^~~~~~~~~~~~~~~
    |      |                    |
    |      |                    (21) returning to 'main' from 'std::__cxx11::basic_string<char>::basic_string<>'
    |   24 |     string disc2 = "Disc-Golf Disc";
    |      |                    ~~~~~~~~~~~~~~~~
    |      |                    |
    |      |                    (22) calling 'std::__cxx11::basic_string<char>::basic_string<>' from 'main'
    |
    +--> 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 23-26
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:634:7:
           |  634 |       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
           |      |       ^~~~~~~~~~~~
           |      |       |
           |      |       (23) entry to 'std::__cxx11::basic_string<char>::basic_string<>'
           |......
           |  638 |         if (__s == 0)
           |      |         ~~
           |      |         |
           |      |         (24) following 'false' branch (when '__s' is non-NULL)...
           |......
           |  641 |         const _CharT* __end = __s + traits_type::length(__s);
           |      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                                                        |
           |      |                                                        (25) ...to here
           |      |                                                        (26) calling 'std::char_traits<char>::length' from 'std::__cxx11::basic_string<char>::basic_string<>'
           |
           +--> 'static constexpr std::size_t std::char_traits<char>::length(const char_type*)': events 27-29
                  |
                  |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/char_traits.h:383:7:
                  |  383 |       length(const char_type* __s)
                  |      |       ^~~~~~
                  |      |       |
                  |      |       (27) entry to 'std::char_traits<char>::length'
                  |......
                  |  386 |         if (std::__is_constant_evaluated())
                  |      |         ~~
                  |      |         |
                  |      |         (28) following 'false' branch...
                  |......
                  |  389 |         return __builtin_strlen(__s);
                  |      |                ~~~~~~~~~~~~~~~~~~~~~
                  |      |                                |
                  |      |                                (29) ...to here
                  |
           <------+
           |
         'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 30-31
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:641:56:
           |  641 |         const _CharT* __end = __s + traits_type::length(__s);
           |      |                                     ~~~~~~~~~~~~~~~~~~~^~~~~
           |      |                                                        |
           |      |                                                        (30) returning to 'std::__cxx11::basic_string<char>::basic_string<>' from 'std::char_traits<char>::length'
           |  642 |         _M_construct(__s, __end, forward_iterator_tag());
           |      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                     |
           |      |                     (31) calling 'std::__cxx11::basic_string<char>::_M_construct<const char*>' from 'std::__cxx11::basic_string<char>::basic_string<>'
           |
           +--> 'void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 32-33
                  |
                  |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.tcc:217:7:
                  |  217 |       basic_string<_CharT, _Traits, _Alloc>::
                  |      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  |      |       |
                  |      |       (32) entry to 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
                  |......
                  |  248 |       }
                  |      |       ~
                  |      |       |
                  |      |       (33) calling 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
                  |
                  +--> 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag)::_Guard::~_Guard() [with _InIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': events 34-36
                         |
                         |  238 |           ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }
                         |      |           ^           ~~                                        ~
                         |      |           |           |                                         |
                         |      |           |           |                                         (36) ...to here
                         |      |           |           (35) following 'false' branch...
                         |      |           (34) entry to 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard'
                         |
                  <------+
                  |
                'void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_construct(_InIterator, _InIterator, std::forward_iterator_tag) [with _FwdIterator = const char*; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': event 37
                  |
                  |  248 |       }
                  |      |       ^
                  |      |       |
                  |      |       (37) returning to 'std::__cxx11::basic_string<char>::_M_construct<const char*>' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>(const char*, const char*, std::forward_iterator_tag)::_Guard::~_Guard'
                  |
           <------+
           |
         'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with <template-parameter-2-1> = std::allocator<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]': event 38
           |
           |/opt/compiler-explorer/gcc-trunk-20220913/include/c++/13.0.0/bits/basic_string.h:642:21:
           |  642 |         _M_construct(__s, __end, forward_iterator_tag());
           |      |         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           |      |                     |
           |      |                     (38) returning to 'std::__cxx11::basic_string<char>::basic_string<>' from 'std::__cxx11::basic_string<char>::_M_construct<const char*>'
           |
    <------+
    |
  'int main()': events 39-44
    |
    |<source>:24:20:
    |   24 |     string disc2 = "Disc-Golf Disc";
    |      |                    ^~~~~~~~~~~~~~~~
    |      |                    |
    |      |                    (39) returning to 'main' from 'std::__cxx11::basic_string<char>::basic_string<>'
    |......
    |   58 |     if (quantity <= 0)
    |      |     ~~              
    |      |     |
    |      |     (40) following 'false' branch...
    |......
    |   63 |     if (quantity >=5 && quantity <=9)
    |      |         ~~~~~~~~~~~~
    |      |                  |
    |      |                  (41) ...to here
    |......
    |   75 |     else if (quantity >=30)
    |      |          ~~         
    |      |          |
    |      |          (42) following 'false' branch...
    |......
    |   79 |     totalSavings = pricePerDisc * quantity * discount;
    |      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |              |
    |      |                  |              (43) ...to here
    |      |                  (44) use of uninitialized value 'discount' here
    |
    if (quantity <= 0)
    {
        cout << quantity << " is an invalid number of discs.\n";
        return 0;
    }
    if (quantity >=5 && quantity <=9)

discount is uninitialized when quantity is between 1 and 4. Add another if or change the existing ones to cover that range or initialize the variable on declaration, etc.

If you are asking why is there are The right operand of '*' is a garbage value error, I think it is because program thinks that pricePerDisc/quentity/discount are not initialized.

It happens because you are using switch(case) that is unrecommended in C++. Compiler/IDE not looking inside of switch(case) (you have initialization of previous variables in it) and this is why he keeps thinking, that you don't initialize any of pricePerDisc/quentity/discount variables (when you are not initializing variables there are just garbage in it).

If you want to silence this warning/error - simply initialize some default numbers in your declaration. For example:

int quantity = 0;
double pricePerDisc = 0.0;
double totalSavings = 0.0;
double afterSavings;
double total;
char userInput;
double discount = 0.0;

Hope it helps!

Related