Book's Question:
"Write a program that asks the user for a two digit number, then prints the English word for the number. HINT: Break the number into two digits. Use one switch statement to print the word for the first digit (twenty, thirty, etc). Use a second switch statement to print the word for the second digit. Don't forget that the numbers between 11 and 19 require special treatment (and damn those numbers say I!!!!!)"
I've been working on this little program for about 3 hours straight now, so I'm getting ready to throw my computer out the window. The past 2.5 hours of which spent on the 11 - 19 "special treatment" numbers!!!! Arrgggghhh!!!
My switch statements worked fine with numbers that were not 11 - 19, so I was comfortable with my code at that point, all was well. After I had that working, I decided to add the "if" statement block you see to handle numbers 11 -19. Please don't laugh at my "if" statements. I've written and rewritten them about 20 different ways, sometimes they would compile but produce the wrong output and sometimes they wouldn't compile at all. When they did compile, no matter what two digit number I was inputting, the output always was eleven. Why? I don't know. So anyway, the "if" statements are pretty much garbage, but this is just how I'm leaving them in there in case I'm somehow close.
I thought it would be quite simple, and I followed my code example from last night to handle the special numbers. Like, if (number = 11) print ("eleven"), but every number was printing eleven. Any help would be great. My book doesn't have this one written out on it's website like some others.
Below, I've tried if, and if else, and if else if, but maybe some brackets weren't in the right spot or something, after two hours just plain tired!!
"Write a program that asks the user for a two digit number, then prints the English word for the number. HINT: Break the number into two digits. Use one switch statement to print the word for the first digit (twenty, thirty, etc). Use a second switch statement to print the word for the second digit. Don't forget that the numbers between 11 and 19 require special treatment (and damn those numbers say I!!!!!)"
I've been working on this little program for about 3 hours straight now, so I'm getting ready to throw my computer out the window. The past 2.5 hours of which spent on the 11 - 19 "special treatment" numbers!!!! Arrgggghhh!!!
My switch statements worked fine with numbers that were not 11 - 19, so I was comfortable with my code at that point, all was well. After I had that working, I decided to add the "if" statement block you see to handle numbers 11 -19. Please don't laugh at my "if" statements. I've written and rewritten them about 20 different ways, sometimes they would compile but produce the wrong output and sometimes they wouldn't compile at all. When they did compile, no matter what two digit number I was inputting, the output always was eleven. Why? I don't know. So anyway, the "if" statements are pretty much garbage, but this is just how I'm leaving them in there in case I'm somehow close.
I thought it would be quite simple, and I followed my code example from last night to handle the special numbers. Like, if (number = 11) print ("eleven"), but every number was printing eleven. Any help would be great. My book doesn't have this one written out on it's website like some others.
Below, I've tried if, and if else, and if else if, but maybe some brackets weren't in the right spot or something, after two hours just plain tired!!
Code:
#include <stdio.h>
main ()
{
int first_number, second_number, number ;
printf ("Enter a two digit number: ") ;
scanf ("%d", &number) ;
if (number = 11) {
printf ("eleven\n") ;
}
else (number = 12) {
printf ("twelve\n") ;
}
else (number = 13) {
printf ("thirteen\n") ;
}
else (number = 14) {
printf ("fourteen\n") ;
}
else (number = 15) {
printf ("fifteen\n") ;
}
else (number = 16) {
printf ("sixteen\n") ;
}
else (number = 17) {
printf ("seventeen\n") ;
}
else (number = 18) {
printf ("eighteen\n") ;
}
else (number = 19) {
printf ("nineteen\n") ;
}
printf ("You Entered the Number %d\n", number) ;
first_number = number / 10 ;
second_number = number % 10 ;
switch (first_number) {
case 9: printf ("ninety-"); break;
case 8: printf ("eighty-"); break;
case 7: printf ("seventy-"); break;
case 6: printf ("sixty-"); break;
case 5: printf ("fifty-"); break;
case 4: printf ("forty-"); break;
case 3: printf ("thirty-"); break;
case 2: printf ("twenty-"); break;
}
switch (second_number) {
case 9: printf ("nine\n"); break;
case 8: printf ("eight\n"); break;
case 7: printf ("seven\n"); break;
case 6: printf ("six\n"); break;
case 5: printf ("five\n"); break;
case 4: printf ("four\n"); break;
case 3: printf ("three\n"); break;
case 2: printf ("two\n"); break;
case 1: printf ("one\n"); break;
}
return 0 ;
}
Last edited: