-
Program Help
I am making a program that will take any two two-digit numbers determine the largest and display it in words using if/else and switch statements. I'm kinda stuck. I know how to determine the larger number but i am not sure how to make it into words. This is what i have so far:
Code:
#include <stdio.h>
int main(void)
{
int a, b, i, onesDig, tensDig;
tensDig = i/10;
onesDig = i/10;
char word;
printf("Enter two two-digit numbers separated by a space: ");
scanf("%d %d", &a, &b);
if (a>b){
a=i;}
else
b=i;
switch (i){
case 10: word = "ten"; break;
case 11: word = "eleven"; break;
case 12: word = "twelve"; break;
case 13: word = "thirteen"; break;
case 14: word = "fourteen"; break;
case 15: word = "fifteen"; break;
case 16: word = "sixteen"; break;
case 17: word = "seventeen"; break;
case 18: word = "eighteen"; break;
case 19: word = "nineteen"; break; }
switch (tensDig) {
case 2: word = "twenty"; break;
case 3: word = "thirty"; break;
case 4: word = "forty"; break;
case 5: word = "fifty"; break;
case 6: word = "fifty"; break;
case 7: word = "sixty"; break;
case 8: word = "seventy"; break;
case 9: word = "ninety"; break;
}
switch (onesDig) {
case 1: word = "-one"; break;
return 0;
}
-
[QUOTE=thomasjiii;1001990]I am making a program that will take any two two-digit numbers determine the largest and display it in words using if/else and switch statements. I'm kinda stuck. I know how to determine the larger number but i am not sure how to make it into words. This is what i have so far:
Code:
tensDig = i/10;
onesDig = i/10;
These need to happen after you assign i to the greater of a or b.
A char holds a single char. You need a char * to hold a string. You can actually get away without this pointer, if you use something like the following:
I also suggest something like
Code:
if number is between 10 and 19
print special number
else
if tensDig is non-zero
print tens word
if onesDig
print dash
if onesDig
print ones word
EDIT: onesDig should be calculated as the modulus of i and 10, i.e. onesDig = i % 10. And you're case for tens words is off by one. You have "fifty" twice and no "eighty"
-
Code:
if (a>b){
a=i;}
else
b=i;
Just noticed this is backwards. You want i = a; and i = b; to make i contain the greater of the two. A shorthand for this common if-else construct is the following:
Code:
i = a > b ? a : b; // if a > b, assign i a, otherwise, assign it b
-
Thanks so far. I'm still confused on how to get it to print out the words. This is what i have now:
Code:
#include <stdio.h>
int main(void)
{
int a, b, i, onesDig, tensDig;
char* word;
printf("Enter two two-digit numbers separated by a space: ");
scanf("%d %d", &a, &b);
if (a>b){
a=i;}
else
b=i;
onesDig = i%10;
tensDig = i/10;
switch (i){
case 10: word = "ten"; break;
case 11: word = "eleven"; break;
case 12: word = "twelve"; break;
case 13: word = "thirteen"; break;
case 14: word = "fourteen"; break;
case 15: word = "fifteen"; break;
case 16: word = "sixteen"; break;
case 17: word = "seventeen"; break;
case 18: word = "eighteen"; break;
case 19: word = "nineteen"; break; }
switch (tensDig) {
case 2: word = "twenty"; break;
case 3: word = "thirty"; break;
case 4: word = "forty"; break;
case 5: word = "fifty"; break;
case 6: word = "sixty"; break;
case 7: word = "seventy"; break;
case 8: word = "eighty"; break;
case 9: word = "ninety"; break;
}
switch (onesDig) {
case 1: word = "-one"; break;
case 2: word = "-two"; break;
case 3: word = "-three"; break;
case 4: word = "-four"; break;
case 5: word = "-five"; break;
case 6: word = "-six"; break;
case 7: word = "-seven"; break;
case 8: word = "-eight"; break;
case 9: word = "-nine"; break;
}
return 0;
}
-
-
I tried printf("%s", word); and it doesn't print anything. I tried printf(word); and put in 10 and 11 and it displayed -four
-
Where did you put those statements? What was the last value your program set the variable to before printing? You have to make your switch statements exclusive, i.e. don't do switch(tensDig) or switch(onesDig) if number is between 10 and 19. Also, if you insist on using word as a char *, you have to print, you will need to print it's value before reassigning it again in a subsequent switch. Any reason you haven't tried to implement my pseudo code from post #2?
EDIT: Alright, you have to make a little more effort. I just realized your new code totally ignored the critical information I gave you in post #3.
-
I'm taking my first c programming class so I don't know much about it. I wasn't really sure how and where i should use the code you posted. I put the printf at the end of the code. So would I put the switch for the tensDig and onesDig inside of an if statement?
Sorry I just didn't really understand your codes in posts 2 and 3 I'm a noob at programming.
-
I've gotten completely lost lol. I'm not really sure how to use your codes from post 2 and 3.
-
Since you're a noob, skip the weird ?: business for now. Stick with:
Code:
if (a > b)
i = a; // i is on the left, so we assign to i the value in a
else
i = b;
Otherwise, i is uninitialized and it has some random garbage value.
You don't actually need a "word" variable to store a string, just print it directly, like so:
printf("seventeen");
Then, pay attention to the indentation I used, similar to that of a regular program. My pseudo code has the same structure as the logic you need for you program. Here is something a little closer to the end product:
Code:
if (num >= 10 && num <= 19) {
// switch statement that prints "ten" through "nineteen"
}
else {
if (tensDig != 0) {
// switch statemet to print "twenty" through "ninety"
}
...
}
I'm leaving the rest for you to fill in. Please really think through the logic and code I provided, and run through some examples by hand to get a feel for how the if and switch statements control what parts of the program run.
-
Ok thanks for the help I should be able to get it now.
-
I really have problems with switch statements for some reason. I put them inside the ifs and it keeps giving me errors saying else statement without a previous if. I tried just taking the else's out but it always gave me -eight as the output. I don't think i have the correct expressions inside the switch statements either. Here is how I coded them:
Code:
onesDig = i%10;
tensDig = i/10;
if (i>=10 && i<=19);{
switch (i){
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; }}
else
if (tensDig!=0);{
switch (tensDig) {
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;
}
}
else
if (onesDig) {
switch (onesDig) {
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;
}
}
EDIT: Nevermind about the if/else error I figured it out I had a semicolon after the if statement.
-
Try being a little more consistent and sensible about your { braces }. Pick a good indentation/brace style (K&R, 1TBS, Allman and BSD KNF are the most common). It might help to put braces around all if blocks and else blocks too, to avoid ambiguity and the dangling else problem.
You are largely on the right track, except you need to drop the 'else' before the if (onesDig).
-
I fixed the else problem and dropped the else before the if (onesDig) but I don't know why the output seems to be just a random onesDig case; the program will not output ten-nineteen or twenty-ninety just the onesDig
-
Code:
if (i>=10 && i<=19);{
Why is there a semi-colon in there?