Home > Software design >  Is it possible to turn this one if statement into a switch statement in c?
Is it possible to turn this one if statement into a switch statement in c?

Time:10-09

I was tasked to create a program that will ask for an integer input and will convert the inputted integer into its corresponding word format in English using if-else statements. Now, I have to modify it by using switch instead. Here is my progress so far.

#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;
  hundreds = num % 1000 / 100;
  tens = num % 1000 % 100 / 10;
  ones = num % 1000 % 100 % 10;
  
    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;
    }

    switch(hundreds) {
    case 0: break;
    case 1: printf(" one hundred"); break;
    case 2: printf(" two hundred"); break;
    case 3: printf(" three hundred"); break;
    case 4: printf(" four hundred"); break;
    case 5: printf(" five hundred"); break;
    case 6: printf(" six hundred"); break;
    case 7: printf(" seven hundred"); break;
    case 8: printf(" eight hundred"); break;
    case 9: printf(" nine hundred"); break;
    }

    switch(tens) {
    {
    case 1: 
    {
        switch(ones) {
            case 0: printf(" ten");break;
            case 1: printf(" eleven"); break;
            case 2: printf(" twelve"); break;
            case 3: printf(" thirteen"); break;
            case 4: printf(" fourteen"); break;
            case 5: printf(" fifteen"); break;
            case 6: printf(" sixteen"); break;
            case 7: printf(" seventeen"); break;
            case 8: printf(" eighteen"); break;
            case 9: printf(" nineteen"); break;
        }
        break;
    }
    break;
    }
        
    case 2: printf(" twenty"); break;
    case 3: printf(" thirty"); break;
    case 4: printf(" forty"); break;
    case 5: printf(" fifty"); break;
    case 6: printf(" sixty"); break;
    case 7: printf(" seventy"); break;
    case 8: printf(" eighty"); break;
    case 9: printf(" ninety"); break;

    }

    if (tens != 1)
    {   
        switch(ones) {
            case 0: break;
            case 1: printf(" one"); break;
            case 2: printf(" two"); break;
            case 3: printf(" three"); break;
            case 4: printf(" four"); break;
            case 5: printf(" five"); break;
            case 6: printf(" six"); break;
            case 7: printf(" seven"); break;
            case 8: printf(" eight"); break;
            case 9: printf(" nine"); break;
        }

    }

}
getch();
}

It works fine but is it possible to make that one if statement (if (tens != 1)) into a switch statement instead? Would really appreciate any feedback, thanks!

CodePudding user response:

Very simple, really:

/* Finished thousands and hundreds */

switch(tens) {
    case 1: ones = ones   10; break;
    case 2: printf(" twenty"); break;
    case 3: printf(" thirty"); break;
    case 4: printf(" forty"); break;
    case 5: printf(" fifty"); break;
    case 6: printf(" sixty"); break;
    case 7: printf(" seventy"); break;
    case 8: printf(" eighty"); break;
    case 9: printf(" ninety"); break;
}

switch(ones) {
    case  0: break;
    case  1: printf(" one"); break;
    case  2: printf(" two"); break;
    case  3: printf(" three"); break;
    case  4: printf(" four"); break;
    case  5: printf(" five"); break;
    case  6: printf(" six"); break;
    case  7: printf(" seven"); break;
    case  8: printf(" eight"); break;
    case  9: printf(" nine"); break;
    case 10: printf(" ten");break;
    case 11: printf(" eleven"); break;
    case 12: printf(" twelve"); break;
    case 13: printf(" thirteen"); break;
    case 14: printf(" fourteen"); break;
    case 15: printf(" fifteen"); break;
    case 16: printf(" sixteen"); break;
    case 17: printf(" seventeen"); break;
    case 18: printf(" eighteen"); break;
    case 19: printf(" nineteen"); break;
}

Sometimes it pays to NOT overthink things and NOT try to manipulate every tiny avenue of logic. Just stop and think - really think - about the task.


EDIT
Perhaps, looking at this code, you spot the idea of moving the "print ones" into its own function. Then, call it with the thousands value and print the words " thousand", then call it with the hundreds value and print the word " hundred"... Learn to use functions to be able to re-use code. Very valuable lesson!!

PS: If you follow this advice, you can extend the range of acceptable input to print, even, "sixteen thousand nine hundred forty two" (16942), and on up above that to 99,999. (And, even above that if you notice how "triplets" of numbers-as-words appear between the commas when written with separators...)

CodePudding user response:

Yes! You can do this. Implementing the final if as a switch might look something like the following:

switch(tens)
    {   
        case 1:
            break;
        default:
            switch(ones) {
                case 0: break;
                case 1: printf(" one"); break;
                case 2: printf(" two"); break;
                case 3: printf(" three"); break;
                case 4: printf(" four"); break;
                case 5: printf(" five"); break;
                case 6: printf(" six"); break;
                case 7: printf(" seven"); break;
                case 8: printf(" eight"); break;
                case 9: printf(" nine"); break;
            }
        break;
    }

The switch default is like the if's else statement; the code within it runs if no other conditions are true.

CodePudding user response:

you can try

    switch(tens)
    {
        case 2 ... 9:
        {
            switch(ones) {
                case 0: break;
                case 1: printf(" one"); break;
                case 2: printf(" two"); break;
                case 3: printf(" three"); break;
                case 4: printf(" four"); break;
                case 5: printf(" five"); break;
                case 6: printf(" six"); break;
                case 7: printf(" seven"); break;
                case 8: printf(" eight"); break;
                case 9: printf(" nine"); break;
            }
            break;
        }

        case 1:
        default:
            break;

    }

where 2 ... 9 means that if tens lies in the range 2 to 9 including both 9 and 2 since that the tens will always be a number from 0 to 9

  •  Tags:  
  • c
  • Related