# Why does the amount is incorrect?

asked Nov 20, 2021
#include <stdio.h>

int
main ()
{
char meal;
char drinks;
char A, B, C, D, S, M, L;
int quantity;
int amount;
char order;

printf ("\tMeal\t\tPackage\t\t\t\tPrice\n");
printf ("\tA   \t\tChicken & Spaghetti\t\t150\n");
printf ("\tB   \t\tHamburger & Fries  \t\t145\n");
printf ("\tC   \t\tCheese Burger      \t\t100\n");
printf ("\tD   \t\tPizza              \t\t80\n\n");
printf ("\tDrinks   \tDescription\t\t\tPrice\n");
printf ("\tS   \t\tSmall Drinks \t\t\t30\n");
printf ("\tM   \t\tMedium Drinks\t\t\t35\n");
printf ("\tL   \t\tLarge Drinks \t\t\t40\n\n");

printf("Enter your meal (A, B, C, D, E) : ");
scanf("%c", &meal);
printf("Enter your drink (S, M, L) : ");
scanf(" %c", &drinks);
printf("Quantity : ");
scanf(" %d", &quantity);

printf("\nMeal          : %c", meal);
printf("\nDrinks        : %c", drinks);
printf("\nQuantity      : %d", quantity);

switch(meal){
case 'A':
A = 150;
switch(drinks){
case 'S':
S = 30;
amount = (A + S) * quantity;
printf("\nOrder Details : Chicken & Spaghetti and Small Drinks");
break;
case 'M':
M = 35;
amount = (A + M) * quantity;
printf("\nOrder Details : Chicken & Spaghetti and Medium Drinks");
break;
case 'L':
L = 40;
amount = (A + L) * quantity;
printf("\nOrder Details : Chicken & Spaghetti and Large Drinks");
break;
}
break;

case 'B':
B = 145;
switch(drinks){
case 'S':
S = 30;
amount = (B + S) * quantity;
printf("\nOrder Details : Hamburger & Fries and Small Drinks");
break;
case 'M':
M = 35;
amount = (B + M) * quantity;
printf("\nOrder Details : Hamburger & Fries and Medium Drinks");
break;
case 'L':
L = 40;
amount = (B + L) * quantity;
printf("\nOrder Details : Hamburger & Fries and Large Drinks");
break;
}
break;

case 'C':
C = 100;
switch(drinks){
case 'S':
S = 30;
amount = (C + S) * quantity;
printf("\nOrder Details : Cheese Burger and Small Drinks");
break;
case 'M':
M = 35;
amount = (C + M) * quantity;
printf("\nOrder Details : Cheese Burger and Medium Drinks");
break;
case 'L':
L = 40;
amount = (C + L) * quantity;
printf("\nOrder Details : Cheese Burger and Large Drinks");
break;
}
break;

case 'D':
D = 80;
switch(drinks){
case 'S':
S = 30;
amount = (D + S) * quantity;
printf("\nOrder Details : Pizza & Fries and Small Drinks");
break;
case 'M':
M = 35;
amount = (D + M) * quantity;
printf("\nOrder Details : Pizza & Fries and Medium Drinks");
break;
case 'L':
L = 40;
amount = (D + L) * quantity;
printf("\nOrder Details : Pizza & Fries and Large Drinks");
break;
}
break;

default:
printf("Invalid Order Details");
}

printf("\nAmount        : %d", amount);

return 0;
}

answered Nov 20, 2021 by (55,660 points)

# Solution For Your Problem

The problem is that your variables A, B, C, D, S, M, L are of type char. A char is an integral type taking value in a range of [-128, 127]. These variables are supposed to represent the price of a certain food.

When you assign A = 150, then the actual value stored in A is -106 as it overflows (128 - 150 = 22 overflow, hence -128 - 22 = -106). Similarly, when you add to chars together the chances are that you cannot store the result on just one signed byte (char). Instead, use a larger type, e.g. int.

# And Some Extra...

You could restructure your code in a way that data and logic is separated. You could create functions that deal with processing various food requests or displaying them. You could create a data structure that holds the code, the name and the price of a specific food item.

If you do this, your program is easily extendable. If you want to add a new meal or drink, you just add it to the right array and voila, everything works. No need to touch anything else, no extra switch-cases, no extra logic needed.

See an example for this below:

```#include <ctype.h>
#include <stdio.h>

#define CODE_LENGTH 8
#define NAME_LENGTH 24
#define PRICE_LEN 8

#define ElementCount(array) sizeof(array) / sizeof(array[0])

typedef struct
{
char code;
const char * name;
int price;
} Food;

// columns must have exactly 3 elements
void PrintHeader(const char ** columns)
{
printf("%-*s%-*s%*s\n", CODE_LENGTH, columns[0], NAME_LENGTH, columns[1], PRICE_LEN, columns[2]);
}

void PrintFood(const Food * foods, size_t numFoods)
{
for (size_t i = 0; i < numFoods; i++)
printf("%-*c%-*s%*d\n", CODE_LENGTH, foods[i].code, NAME_LENGTH, foods[i].name, PRICE_LEN, foods[i].price);
}

const Food * Find(const Food * foods, size_t numFoods, char code)
{
for (size_t i = 0; i < numFoods; i++)
{
const Food * food = &foods[i];
if (food->code == code)
return food;
}
return NULL;
}

int main()
{
//
// Setting up the menu
//
const char * mealHeader[] = { "Meal", "Package", "Price" };
Food meals[] =
{
{ 'A', "Chicken & Spaghetti", 150 },
{ 'B', "Hamburger & Fries", 145 },
{ 'C', "Cheese Burger", 100 },
{ 'D', "Pizza", 80 }
};

const char * drinkHeader[] = { "Drinks", "Description", "Price" };
Food drinks[] =
{
{ 'S', "Small Drinks", 30 },
{ 'M', "Medium Drinks", 35 },
{ 'L', "Large Drinks", 40 }
};

//
// Printing the menu
//
PrintFood(meals, ElementCount(meals));
printf("\n");
PrintFood(drinks, ElementCount(drinks));
printf("\n");

//
// Asking the user to choose something from the menu
//
char meal;
char drink;
int quantity;
printf("Enter your meal (A, B, C, D, E) : ");
scanf("%c", &meal);
printf("Enter your drink (S, M, L) : ");
scanf(" %c", &drink);
printf("Quantity : ");
scanf(" %d", &quantity);
printf("\n");
printf("--------------------------------------------")
printf("\n");

//
// Processing the user's choice
//
const Food * orderedMeal = Find(meals, ElementCount(meals), toupper(meal));
if (!orderedMeal)
{
printf("Invalid meal code: %c\n", meal);
return -1;
}

const Food * orderedDrink = Find(drinks, ElementCount(drinks), toupper(drink));
if (!orderedDrink)
{
printf("Invalid drink code: %c\n", drink);
return -1;
}

int total = (orderedMeal->price + orderedDrink->price) * quantity;
printf("Meal         : %c\n", meal);
printf("Drink        : %c\n", drink);
printf("Quantity     : %d\n", quantity);
printf("Order details: %s and %s\n", orderedMeal->name, orderedDrink->name);
printf("Amount       : %d", total);
return 0;
}```
commented Nov 21, 2021 by (940 points)
thank you very much, I'm sorry I don't understand the syntax you had used yet, but I will try to learn this. Thank you!
commented Nov 21, 2021 by (940 points)
also, i tried to look into your first suggestion, so I added an int a, b, c, d, s, m, l; and replace the uppercase char types in my switch cases. But thanks to you that you gave me a better solution and suggestion
commented Nov 21, 2021 by (55,660 points)
"I don't understand the syntax you had used"

Please, look at how formatting works for printf(): https://www.cplusplus.com/reference/cstdio/printf/

If this is not what you meant, I'm happy to clarify if you have any specific questions.