-4

The title speaks of the problem.

#include<stdio.h>
#include<conio.h>
main(){
char order, coffee, size, affirm;
float price;
clrscr();
printf("Hello, welcome to C Coffee Shop. How may I help you?\n");
printf(">Buy = B\n>Nothing = N\n"); /* Choose action */
scanf("%c",&order);
if(order == 'B'){ /* Decided to buy coffee */
  printf("What coffee would you want for today?\n"); /* Decide coffee type */
  printf(">Espresso = E\n>Americano = A\n>Latte = L\n");
  scanf("%c",&coffee);
  printf("How large would your drink be?\n"); /* Decide coffee size */
  printf(">Petite = P\n>Regular = R\n>Tall = T\n");
  scanf("%c",&size);
  if((coffee == 'E') && (size == 'P')){ /* Petite Espresso */
    price = 35;
    }
  else if((coffee == 'E') && (size == 'R')){ /* Regular Espresso */
    price = 50;
    }
  else if((coffee == 'E') && (size == 'T')){ /* Tall Espresso */
    price = 75;
    }
  else if((coffee == 'A') && (size == 'P')){ /* Petite Americano */ 
    price = 45;
    }
  else if((coffee == 'A') && (size == 'R')){ /*Regular Americano */
    price = 65;
    }
  else if((coffee == 'A') && (size == 'T')){ /* Tall Americano */
    price = 90;
    }
  else if((coffee == 'L') && (size == 'P')){ /* Petite Latte */
    price = 60;
    }
  else if((coffee == 'L') && (size == 'R')){ /* Regular Latte */
    price = 85;
    }
  else if((coffee == 'L') && (size == 'T')){ /* Tall Latte */
    price = 110;
    }
  printf("To clarify, your order is %c %c.\nThat would be %0.2f pesos.\n", size, coffee, price); /* Verify order */
  printf(">OK = O\n"); /* Affirm */
  scanf("%c",&affirm);
  if(affirm == 'O'){ /* Accept order */
    printf("Processing order...\n");
    }
  else{ /* Discard order */
    printf("Discarding order...\n");
    }
  printf("Thank you! Please come again.");
  }
else if(order == 'N'){ /* Decided not to buy coffee */
  printf("Thank you! Please come again.");
  }
getche();
return 0;
}

When I try to run the program, the lines 11 - 16 appear without break, disabling me from executing the other commands. I think it's either in my misapplication of scanf() or if-else statements. I don't know how to solve it though. Help, please?

if(order == 'B'){
  printf("What coffee would you want for today?\n");
  printf(">Espresso = E\n>Americano = A\n>Latte = L\n");
  scanf("%c",&coffee);
  printf("How large would your drink be?\n");
  printf(">Petite = P\n>Regular = R\n>Tall = T\n");
  scanf("%c",&size);

Please also tell me if there is any other mistakes in my code. Tysm to those who will help :>

2 Answers2

1

Wherever you have a character input such as

scanf("%c",&coffee);

put a space in front of the format spec, like

scanf(" %c",&coffee);

This will cause any previous whitespace left in the input buffer, such as the newline after a previous input, to be skipped, instead of being read as that char.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

When ever you are pressing enter after every input character, the new line is being counted as a input character. That means you have to avoid these new line characters. You can do this by:

  • Incluing getchar() after every scanf.

  • Or, rewrite the scanf as scanf("%c%*c",&order).

This'd avoid the new line characters to be counted as input.

Shahid
  • 2,288
  • 1
  • 14
  • 24
  • I'm actually a newbie to C... Thanks for the help! the `%*c` actually works, but how does it work? Also, how do I show their order such that when they chose 'American Tall' the next `printf` statement would be: `To clarify, your order is one Tall Americano. That would be 90 pesos.` instead of: `To clarify, your order is T A. That would be 90 pesos.` – glae_nine099 Aug 21 '16 at 04:21