Program Help

This is a discussion on Program Help within the C Programming forums, part of the General Programming Boards category; I am making a program that will take any two two-digit numbers determine the largest and display it in words ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    [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.
    Code:
        char word;
    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"
    Last edited by anduril462; 02-11-2011 at 12:53 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    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

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    How about printf(word);?

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    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.
    Last edited by anduril462; 02-11-2011 at 02:39 PM.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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.
    Last edited by thomasjiii; 02-11-2011 at 02:43 PM.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    I've gotten completely lost lol. I'm not really sure how to use your codes from post 2 and 3.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    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.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Ok thanks for the help I should be able to get it now.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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.
    Last edited by thomasjiii; 02-11-2011 at 03:45 PM.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    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).

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    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

  15. #15
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
        if (i>=10 && i<=19);{
    Why is there a semi-colon in there?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21