The total bill doesn't add up correctly, don't get where I went wrong? Total bill is way off.

#include <iostream> 
#include<cmath>
#include <iomanip> 
#include <string>

using namespace std;


    const float Small = 1.20;
    const float Medium = 1.50;
    const float Large = 1.70;
    const float less_inches = 3.75;
    const float more_inches = 4.00;

int main()
{
        int No_Sand, Sand_size;
        float DrinkCost = 0.0, SandCost = 0.0, total = 0.0;
        float No_Drinks;
        char  Drink_size;
        cout << fixed << showpoint << setprecision(2);

        //Display Menu
        cout << "------------Menu-------------" << endl;
        cout << "Drinks " << endl;
        cout << setfill('.');
        cout << left << setw(15) << "   Small" << right << setw(6) << Small << endl;
        cout << left << setw(15) << "   Medium" << right << setw(6) << Medium << endl;
        cout << left << setw(15) << "   Large" << right << setw(6) << Large << endl;
        cout << "Sandwiches " << endl;
        cout << left << setw(15) << "   10 inches" << right << setw(6) << less_inches << endl;
        cout << left << setw(15) << "   12 inches" << right << setw(6) << more_inches << endl;

        //Ask for how many drinks or sandwiches and the size of drink or sandwich 
        cout << "\t How many drinks: "; 
        cin >> No_Drinks;
        cout << "\t   What size(S=Small, M=Medium, L=Large)? ";
        cin >> Drink_size;
        cout << "\t How many sandwiches? ";
        cin >> No_Sand; 
        cout << "\t   What size(12/10 inches)? ";
        cin >> Sand_size; 

        //Calculate Drink Cost
        switch (Drink_size)
        {
        case 'S': case 's': DrinkCost = No_Drinks * 1.20; break; 
        case 'M': case 'm': DrinkCost = No_Drinks * 1.50; break;
        case 'L': case 'l': DrinkCost = No_Drinks * 1.70; break;
        }

        //Calculate Sandwich cost 
        switch(Sand_size)
        {
        case 10: SandCost = No_Sand * 3.75; break;
        case 12: SandCost = No_Sand * 4.00; break;
        }

        //Calculate total
        total = Drink_size + Sand_size;
        cout << "\t Total bill = " << total << endl;


    //end program
    system("pause");
    return 0;
}

Output:

------------Menu-------------
Drinks
   Small.........1.20
   Medium........1.50
   Large.........1.70
Sandwiches
   10 inches.....3.75
   12 inches.....4.00
         How many drinks: 3
           What size(S=Small, M=Medium, L=Large)? M
         How many sandwiches? 3
           What size(12/10 inches)? 10
         Total bill = 87.00
Press any key to continue . . .

Dani AI

Generated

As observed, the program was adding the size variables instead of the computed costs. The line that sets the total uses Drink_size (a char) and Sand_size (an int) instead of DrinkCost and SandCost. In C++ a char is promoted to its integer code in arithmetic (for example 'M' → 77), so 'M' + 10 produces 87 and explains the unexpected printed total that saw.

Quick, minimal fix:

total = DrinkCost + SandCost;

A few practical hardening tips beyond the one-line fix:

  • Use integer types for counts (e.g., int No_Drinks) — counts shouldn't be float.
  • Add default cases in both switch statements to handle invalid input and set a sensible fallback or report an error.
  • Check cin.fail() and clear the stream when input is malformed.
  • For currency, prefer representing values in whole cents (integers) or consistently round when printing to avoid floating-point surprises.
  • While debugging, print DrinkCost and SandCost separately before summing to verify each component.

These changes make the output predictable and make it easier to trace mistakes like the one identified.

Recommended Answers

All 4 Replies

Should be Total bill = 15.75

Line 60: shouldn't you be adding the costs, not the sizes?

didn't I do that?

Oh ok nevermind, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.