Search code examples
c++syntax-errorclang-tidy

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


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;
    
}

Solution

  • 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!