C++ 错误:“*”的右操作数是垃圾值 [重复]

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

提问人:raelyn 提问时间:9/13/2022 最后编辑:raelyn 更新时间:9/13/2022 访问量:327

问:

我终于完成了我的代码,当我提交它时,我收到了几个错误。它一直在告诉我

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

对于这一行,我试着把它放在一起,没有变化

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

我已经多次检查了我的整个代码(我的程序在空格所在的位置显示点),但我找不到任何额外或计划外的空间。

针对以下行发出错误消息:The right operand of '*' is a garbage value

totalSavings = pricePerDisc * quantity * discount;

谁能帮我?

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;
    
}
C++ 语法错误 clang-tidy

评论

3赞 Erik Kaplun 9/13/2022
有时认为将所有条件都明确为良好的编码风格。一种可执行的注释。
2赞 Standard_101 9/13/2022
此外,1 到 4 之间的数量也可能通过,因此该语句可能是为了避免这种情况。
1赞 WhozCraig 9/13/2022
@Maaz受到的打击很大。这就是实际的问题(我承认我没有看到)。有一个窗口永远不会初始化,你的编译器会告诉你很多。所有这些条件都可能为 false,并且当这种情况发生时是未初始化的。您可以通过首先简单地初始化为零来解决这个问题。discountdiscountdiscount
2赞 molbdnilo 9/13/2022
@Maaz 不,它没有。
0赞 Konrad Rudolph 9/13/2022
“我找不到任何额外或计划外的空间”——也许寻找缺失的空间。

答:

-2赞 IncPink 9/13/2022 #1

如果你问为什么会有错误,我认为这是因为程序认为 // 没有初始化。The right operand of '*' is a garbage valuepricePerDiscquentitydiscount

发生这种情况是因为您正在使用 C++ 中不推荐使用的东西。编译器/IDE 没有查看内部(你在其中初始化了以前的变量),这就是为什么他一直认为,你没有初始化任何 // 变量(当你不初始化变量时,里面只有垃圾)。switch(case)switch(case)pricePerDiscquentitydiscount

如果要使此警告/错误静音 - 只需在声明中初始化一些默认数字即可。例如:

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

希望对您有所帮助!

评论

2赞 Konrad Rudolph 9/13/2022
不推荐,也不是说编译器不能查看它的“内部”。这也不是警告的原因。相反,代码可能无法初始化(在 中未触及)。此外,你的“解决方案”并不好:它使一个非常有效的警告沉默,而不是解决问题。switchdiscountswitch
1赞 Konrad Rudolph 9/13/2022
您如何验证它是否有效,即它产生正确的结果?警告只告诉你代码有问题,而不是正确的解决方案是什么。警告就是这样。您需要实际了解代码的潜在问题,而不是盲目地使用创可贴来消除警告。
2赞 Konrad Rudolph 9/13/2022
@raelyn 正如我在之前的评论中所说,不要盲目地应用更改,直到警告消失。您需要了解导致警告的原因,以修复代码中的逻辑错误。
3赞 j6t 9/13/2022
@raelyn 当然,警告现在已经消失了,因为该值现在已经初始化。但是你明白为什么未初始化的值是一个问题吗?初始值是否正确?还是只是让编译器开心的创可贴?(换一种说法:它是否也让程序的用户感到高兴?
1赞 Caleth 9/13/2022
@raelyn 0 是正确的折扣,还是数量在 1 到 4 之间时才是正确的折扣?
1赞 KamilCuk 9/13/2022 #2

我喜欢静态分析器的详细消息:gcc

<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当介于 1 和 4 之间时未初始化。添加另一个或更改现有变量以覆盖该范围或在声明时初始化变量等。quantityif

评论

1赞 Konrad Rudolph 9/13/2022
耶稣。。。你真的喜欢那个烂摊子?!看起来有人听说人们喜欢 Rust 诊断输出,但不理解它,并实现了他们认为人们想要的东西:意大利面条输出。
0赞 raelyn 9/13/2022
所以这实际上不应该在 1-4 之间。它应该小于或等于 0,这样零和负数将导致该输出。数量 1-4 不适用折扣
1赞 Caleth 9/13/2022
@raelyn 0 的折扣与未初始化的折扣有很大不同
0赞 Caleth 9/13/2022
@KonradRudolph 如果它省略了字符串结构,那就没问题了