1

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!

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Why wouldn't `hundreds = (num - thousands) / 100)` and so on using your approach? Usually you would just `mod 10` and divide by `10` in a loop to get the digits (in reverse order) allowing you to simply get the 1's, 10's, 100's and so on. Or you can use `sprintf()` and just write the number as a string, get the length and read the ASCII digits directly as `array[0]`, `array[1]` and so on. – David C. Rankin Oct 09 '22 at 03:39
  • Thanks for your comment! My class hasn't actually tackled that yet and the way i did it is purely for learning purposes on when and how to use switch statements. – yourfriendlyneighborhoodcat Oct 09 '22 at 03:48
  • No worries, it's good to look at all problems from all different directions. Take a look at [How to convert an int to string in C?](https://stackoverflow.com/q/8257714/3422102) for all the different ways to isolate the digits. You can find just about anything you need using the Search bar above giving the [tag] and what you are looking for. I did `"[c] convert int to string"` to locate that question/answer for you. – David C. Rankin Oct 09 '22 at 03:50
  • You could 'hoodwink' the values... Once you've got the 4 values (thou, hund, tens, units), quickly check the tens amount. If it is 1, then add 10 back into the units (setting tens to zero) and making the units run from "one" all the way up to "nineteen" in a single location... (I can't see where you print the value "ten" for 10... Oops!) – Fe2O3 Oct 09 '22 at 03:59
  • Tip: Instead of this huge blob of code, consider a *look-up table*. It's also better to declare the type as close a possible to initialization, like `int thousands = ...` vs. pre-declaring `int thousands` and then some time later populating it. This helps avoid inadvertently uninitialized values, and also makes it easier for us to understand the code without having to look around for definitions. – tadman Oct 09 '22 at 04:02

3 Answers3

2

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...)

Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • 1
    Man, I type in an answer and discover that I’m just too darn slow. I’m getting old, man. +1 – Dúthomhas Oct 09 '22 at 04:26
  • @Dúthomhas I'll see your "old" and raise you a couple of "ancients"... As I type this reply the word "seventeen" is in view in the code window... Gotta quote Jim Croche's "Five Short Minutes"... "I know I used to like to do it but I don't remember why"... `:-)` – Fe2O3 Oct 09 '22 at 04:31
0

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.

Decator
  • 11
  • 2
0

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

abdo Salm
  • 1,678
  • 4
  • 12
  • 22