Thread: Program Help

  1. #16
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    rags_to_riches I saw that. That is what was causing the else error, but I have fixed that part now.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You had two erroneous semicolons. Did you get both of them?

  3. #18
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Yea I fixed both of them after the two if statements, but now I cant get output from anything but the onesDig. It outputs stuff like -eight, -two. A couple times it was actually right for the ones digit but now it seems to be random.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I took the code you posted, and made the edits you said you made, and everything looked fine. You know this wont work for single digit numbers, right? Can we get the full code and some sample input to help you debug this?

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Yea here's the complete code:
    Code:
    #include <stdio.h>
    int main(void)
    {
        int a, b, num, onesDig, tensDig;
    
    
        printf("Enter two two-digit numbers separated by a space: ");
        scanf("%d %d", &a, &b);
        if (a>b){
            a=num;}
        else
            b=num;
        onesDig = num%10;
        tensDig = num/10;
        if (num) {
            switch (num){
                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!=1) {
                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;}
            }
    
                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;}
            }
    
        return 0;
    }
    Some sample inputs I tried are 78 45 and it gave me -four
    89 78= -eight
    52 12 = -six

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Gah! Why did you change i to num, and change the if to "if (num)"? You were so close. You also still haven't rearranged the order of your assignments! For the THIRD AND FINAL TIME, GET THE BIGGER OF THE TWO NUMBER LIKE THIS:
    Code:
    if (a > b) {
        num = a;
    }
    else {
        num = b;
    }
    Assignments in C work like this:
    Code:
    where_you_want_to_put_the_data = where_you_are_getting_the_data_from;
    Also, you haven't put the correct braces around your else block. Look at my example. One outer if-else, to separate "teens" from the rest. Then, two individual if blocks (tens digit and ones digit), both inside the else clause of the if (num >= 10 && num <= 19) statement. Read the dangling else article if you haven't yet, then get in the habit of being explicit with your { } and get an editor that auto-indents code for you to help make this easier.

  7. #22
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    OMG thank you so much. I finally got it working perfectly. I always forget little things that really mess me up. Again thanks for the help I probably wouldn't have gotten it without you.

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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10: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