Home > Back-end >  Converting if statements to switch statements in c
Converting if statements to switch statements in c

Time:09-18

I wrote a program that will ask for an integer input (1 - 9999) and will convert the inputted integer into its corresponding word format in English.

And I'm trying to modify it so that switch statements will be used instead of if-statements (where it is applicable).

Example 1: Input number: 2481 Output: two thousand four hundred eighty one

Here is my code:

#include<stdio.h>
#include<conio.h>

main()
{
      int num,thousands,hundreds,tens,ones;
      printf("Enter number (1-9999): ");
      scanf("%d",&num);
      //&& - check if within the range
      //|| - check if outside of the range
      if (num < 1 || num > 9999)
         printf("Invalid number.");
      else
      //if (num > 0 && num < 10000)
      {
      thousands = num / 1000;
      hundreds = num % 1000 / 100;
      tens = num % 1000 % 100 / 10;
      ones = num % 1000 % 100 % 10;
      //if (num / 1000 == 1)
      if(thousands == 1)
                   printf("one thousand ");
      if(thousands == 2)
                   printf("two thousand ");
      if(thousands == 3)
                   printf("three thousand ");
      if(thousands == 4)
                   printf("four thousand ");
      if(thousands == 5)
                   printf("five thousand ");
      if(thousands == 6)
                   printf("six thousand ");
      if(thousands == 7)
                   printf("seven thousand ");
      if(thousands == 8)
                   printf("eight thousand ");
      if(thousands == 9)
                   printf("nine thousand ");
      //if (num % 1000 / 100 == 1)
      if(hundreds == 1)
                   printf("one hundred ");
      if(hundreds == 2)
                   printf("two hundred ");
      if(hundreds == 3)
                   printf("three hundred ");
      if(hundreds == 4)
                   printf("four hundred ");
      if(hundreds == 5)
                   printf("five hundred ");
      if(hundreds == 6)
                   printf("six hundred ");
      if(hundreds == 7)
                   printf("seven hundred ");
      if(hundreds == 8)
                   printf("eight hundred ");
      if(hundreds == 9)
                   printf("nine hundred ");
      //if (num % 1000 % 100 / 10 == 1)
      if(tens == 1)
      {
              //if (num % 1000 % 100 % 10 == 0)
              if(ones == 0)
                      printf("ten ");
              if(ones == 1)
                   printf("eleven ");
              if(ones == 2)
                   printf("twelve ");
              if(ones == 3)
                   printf("thirteen ");
              if(ones == 4)
                   printf("fourteen ");
              if(ones == 5)
                   printf("fifteen ");
              if(ones == 6)
                   printf("sixteen ");
              if(ones == 7)
                   printf("seventeen ");
              if(ones == 8)
                   printf("eighteen ");
              if(ones == 9)
                   printf("nineteen ");
      }
      if(tens == 2)
                   printf("twenty ");
      if(tens == 3)
                   printf("thirty ");
      if(tens == 4)
                   printf("forty ");
      if(tens == 5)
                   printf("fifty ");
      if(tens == 6)
                   printf("sixty ");
      if(tens == 7)
                   printf("seventy ");
      if(tens == 8)
                   printf("eighty ");
      if(tens == 9)
                   printf("ninety ");
      if (tens != 1)
      {
               if(ones == 1)
                   printf("one ");
               if(ones == 2)
                   printf("two ");
               if(ones == 3)
                   printf("three ");
               if(ones == 4)
                   printf("four ");
               if(ones == 5)
                   printf("five ");
               if(ones == 6)
                   printf("six ");
               if(ones == 7)
                   printf("seven ");
               if(ones == 8)
                   printf("eight ");
               if(ones == 9)
                   printf("nine ");
      }
      
      }
      //else
          //printf("Invalid number.");
      getch();
}

However, when I tried substituting the if statements into switch statements it displays a blank. I'm stuck at the thousands place. I'm trying to fix it first before getting to the hundreds, tenths, and ones. Here is what I tried:

#include<stdio.h>
#include<conio.h>

main()
{
    int num,thousands,hundreds,tens,ones;
      printf("Enter number (1-9999): ");
      scanf("%d",&num);
      if (num < 1 || num > 9999)
         printf("Invalid number.");
      else
    {
        thousands = num / 1000;
        switch (thousands)
        {
            case '1': printf("one thousand ");
                      break;
            case '2': printf("two thousand ");
                      break;
            case '3': printf("three thousand ");
                      break;
            case '4': printf("four thousand ");
                      break;
            case '5': printf("five thousand ");
                      break;
            case '6': printf("six thousand ");
                      break;
            case '7': printf("seven thousand ");
                      break;
            case '8': printf("eight thousand ");
                      break;
            case '9': printf("nine thousand ");
                      break;
        }
    }
}

CodePudding user response:

When you write case '1':, you compare to character '1''s value (converted to an integer), not integer; you should write case 1:.

CodePudding user response:

You're not comparing the same values you were before.

In your original code, you were comparing thousands against 0, 1, 2, etc. In your updated code, you're comparing thousands against '0', '1', '2', etc.

Use the same values you had before and you'll get the expected results.

CodePudding user response:

Single quotes in C are used to define single characters. Since characters size is 1 byte in C, a char can be assigned up to 128 different values (1 byte = 8 bits = 2⁸ = from 0 to 127). But how are char represented, according to that integer value?
That is implementation-defined, but usually C uses the seven-bit US ASCII character set to represent char by default (for example, you might change the locale to have it use something different, like utf-8).

Back to your question, in the second snippet you're comparing the value of thousands with the ASCII values of the characters '1', '2', '3', etc.
That means you are not comparing thousands with the value between single quotes, but with the character's ASCII decimal value corresponding to the character, so for example, case '1': equals to case 49:

decimal -   character

[...]

49          1
50          2
51          3
52          4
53          5
54          6

[...]

Practical Example

The following program shows just what I've said:

#include <stdio.h>

int main(int argc, char** argv)
{
    char ch = '1';

    printf("size of a char: %ld bytes\n", sizeof(char));
    printf("character representation: %c\n", ch);
    printf("character corresponding value in ASCII table: %d\n", ch);

    return 0;
}

Output:

size of a char: 1 bytes
character representation: 1
character corresponding value in ASCII table: 49
  •  Tags:  
  • c
  • Related