0

I have some problem with my homework. So this is how it looks like

#include<stdio.h>
int main()
{
    char code;
    int price,discount;
    float total;
    printf("Please input price: ");
    scanf("%d",&price);
    printf("Please input discount code: ");
    scanf(" %c",&code);
    switch(code)
    {
        case 'a':   printf("Your discount code is 25 percent\n");
                    discount = 25;
                    break;
        case 'b':   printf("Your discount code is 15 percent\n");
                    discount = 15;
                    break;
        case 'c':   printf("Your discount code is 5 percent\n");
                    discount = 5;
                    break;
        default:    printf("Wrong code,Your discount is 0 percent\n");
                    discount = 0;
                    break;
    }
    total = (price*((100-discount)/100));
    printf("Your price is = %.2f\n",total);
}

I have 2 questions to ask

  1. My task is I have to input both of uppercase and lowercase letter for discount code (there are only three codes: a, b, c) so how can I put both of them in case command? (in this I only do the lowercase letter)

  2. I have run this. But it seems like the discount value is 0 when I try to used it for calculate in the end. When I print the discount only, it works normally. How can I fix that?

Sorry for my poor English and Thank you for your help!

phuclv
  • 37,963
  • 15
  • 156
  • 475
KillingSpree
  • 105
  • 6
  • 1
    All parts of `price*((100-discount)/100)` are integers. That will give an integer result. Most notably `(100-discount)/100` will *always* be zero, To make it a floating-point calculation turn all integer literals `100` into floating-point literals `100.0`. – Some programmer dude Jul 26 '21 at 07:25
  • Does this answer your question? [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) – phuclv Jul 26 '21 at 07:34
  • [Integer division always zero](https://stackoverflow.com/q/9455271/995714), [C++, my double or int value is always 0](https://stackoverflow.com/q/26650870/995714), [Why does dividing two int not yield the right value when assigned to double?](https://stackoverflow.com/q/7571326/995714) – phuclv Jul 26 '21 at 07:37

1 Answers1

1

There would be different possibilities.

scanf(" %c",&code);
switch(code)
{
    case 'a':
    case 'A':
      printf("Your discount code is 25 percent\n");
      discount = 25;
      break;

or you change the input to lower case before or in the switch!

switch( code | 0x60 ) // make it to lower case

with this you don't have to change the following code.

MiniMik
  • 268
  • 1
  • 10
  • 1
    If I tried to read the code in which there was switch statement with `code | 0x60`, I would probably get stuck there for a while. – whiskeyo Jul 26 '21 at 07:43
  • I'd prefer first suggestion for readability. – Louis Go Jul 26 '21 at 07:45
  • @whiskeyo: sorry, I do not get what you mean? – MiniMik Jul 26 '21 at 07:45
  • The value `0x60` comes out of nowhere and it is the first time I see one in such use. And because of that, the code is not very readable. – whiskeyo Jul 26 '21 at 07:51
  • @wiskeyo: yes, the readability isn't improved with that... You can use a define `#define LOWERCASE_BIT 0x60` and use that. The readability is much better then. – MiniMik Jul 26 '21 at 09:29