0

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;
    
}
raelyn
  • 17
  • 5
  • 3
    It is sometimes considered good coding style to make all conditions explicit. A kind of an executable comment. – Erik Kaplun Sep 13 '22 at 06:51
  • 2
    Also, quantity between 1 and 4 might get through as well so the statement might be to avoid that. – Standard_101 Sep 13 '22 at 06:52
  • 1
    @Maaz hit that hard. That's the actual problem (which I admit I did not see). There is a window where `discount` will never be initialized, and your compiler is telling you as much. All of those conditions can strike false, and when that happens `discount` is uninitialized. You can address that by simply initializing `discount` to zero in the first place. – WhozCraig Sep 13 '22 at 06:57
  • 2
    @Maaz No, it doesn't. – molbdnilo Sep 13 '22 at 07:12
  • “I cannot find any extra or unplanned spaces.” — Maybe look for *missing* spaces. – Konrad Rudolph Sep 13 '22 at 07:24
  • @molbdnilo It's actually [indeterminate](https://en.cppreference.com/w/cpp/language/default_initialization). Thank you. – Standard_101 Sep 13 '22 at 07:29
  • @KonradRudolph I compared my other lines regarding if..else.. that have not received an error and they are spaced the same – raelyn Sep 13 '22 at 07:58
  • To debug these types of issues, you can simply comment out or delete parts of the code until the error disappears. At some point you are left with a minimal piece of code where you are either able to figure it out yourself or where you can simply ask. Your code snippet is a lot longer than it needs to be. Refer to the help pages about creating a [example]. – asynts Sep 13 '22 at 07:59
  • what compiler are you using ? Imho "The right operand of '*' is a garbage value" is a super poor error message – 463035818_is_not_an_ai Sep 13 '22 at 08:05
  • @raelyn Most of the conditions in your `if` statements are missing spaces around the operators. I don’t really know what else to tell you. – Konrad Rudolph Sep 13 '22 at 08:08
  • @463035818_is_not_a_number visual studio, the code works fine I just get a big portion off my grade according to this style checker – raelyn Sep 13 '22 at 08:14
  • @KonradRudolph I have tried adding them already and I don't get the error on every line just the one. – raelyn Sep 13 '22 at 08:16
  • @raelyn "works fine" is a dangerous thing with C++. Your code formally has undefined behaviour when `quantity` is between 1 and 4. It's valid for the resulting program to *print no output* or *format your hard drive* with particular input – Caleth Sep 13 '22 at 08:34
  • For the spacing messages: it's telling you the line and column numbers where it expects different spacing: `if (quantity >= 5 && quantity <= 9)` – Caleth Sep 13 '22 at 08:37
  • I'd also ask the instructor which linter, and what settings, they are using. It's quite plausible you can lint your next assignment before submitting it. – Caleth Sep 13 '22 at 08:40
  • @Caleth originally I did have the spacing that way but changed it b/c of the error. I did reach out to my instructor regarding the linter and settings after making the changes and putting it back in the original spacing it now works, thank you! – raelyn Sep 13 '22 at 18:23

2 Answers2

1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Jesus … you actually *like* that mess?! Looks like somebody heard that people like the Rust diagnostic output, didn’t understand it, and implemented what they thought people wanted: spaghetti output. – Konrad Rudolph Sep 13 '22 at 07:28
  • so that's actually not supposed to be between 1-4. It's was meant to be less than or equal to 0 that way zero and negative numbers would result to that output. No discounts applied on quantity 1-4 – raelyn Sep 13 '22 at 07:52
  • 1
    @raelyn a discount of 0 is wildly different to an uninitialized discount – Caleth Sep 13 '22 at 08:23
  • @KonradRudolph If it had omitted the string constructions then it would be ok – Caleth Sep 13 '22 at 08:26
-2

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!

IncPink
  • 34
  • 4
  • 2
    It’s not true that `switch` isn’t recommended, nor that the compiler can't look “inside” it. This is also not the reason for the warning. Rather, the code might fail to initialise `discount` (which is not touched inside the `switch`). Furthermore, your “solution” isn’t good: it silences a very valid warning rather than fixing the issue. – Konrad Rudolph Sep 13 '22 at 07:22
  • I simply pasted this code to my IDE an it works, lol. I only have warning about that those variables are not initialized, so I initialized them and there are no more warning in "totalSaving" line and "else if (quantity >= 30)". For example, my IDE are using the worst scenario, when none of that variables are initialized, so IDE keep reminding me about that case. Maybe, I just simply don't understand the question, hmmm. – IncPink Sep 13 '22 at 07:27
  • 1
    How did you verify that it works, i.e. that it produces the correct result? The warning only tells you that there’s an issue with the code, not what the correct solution is. Warnings are just that. You need to do the actual work of *understanding* the underlying issue with the code, instead of blindly applying band-aids to silence the warning. – Konrad Rudolph Sep 13 '22 at 07:30
  • Yes that actually did fix it, thank you! now if I could just figure out what's going on with this spacing – raelyn Sep 13 '22 at 08:01
  • 2
    @raelyn As I said in my previous comment, don’t just blindly apply changes until a warning goes away. You need to understand what is *causing* the warning to fix the logical errors in your code. – Konrad Rudolph Sep 13 '22 at 08:09
  • @KonradRudolph I'm fairly new to C++ but was the reason not because it wasn't initialized? Genuinely asking to get a better understanding. – raelyn Sep 13 '22 at 08:19
  • 3
    @raelyn Sure, the warning's gone now because the value is now initialized. But do you understand why the uninitialized value was a problem? And whether the initial value is correct? Or is it just a band-aid to make the compiler happy? (Said differently: does it also make the *user* of the program happy?) – j6t Sep 13 '22 at 08:28
  • 1
    @raelyn is 0 the right discount whenever you have missed a case, or is 0 only the right discount when quantity is between 1 and 4? – Caleth Sep 13 '22 at 08:29