Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Program Problem

Options
  • 13-10-2009 2:15pm
    #1
    Closed Accounts Posts: 627 ✭✭✭


    Hi Guys,

    I'm learning abit of C by my self through a variety of books and online tutorials.

    I am trying to write a a program that enters the ordinal at the end of a number that the user inputs. - 1st 2nd 3rd 4th etc... Seen it on a college paper and though it sounded like a challenge

    I have made some progress but its the terrible teens that have left me muddled as 11th is not the same as 21st or 31st etc... and the same goes for 111th is not the same as 121st.

    I have everything sorted from 1 - 100 but after that, its where i get into trouble. I was trying to add an or statment to include num % 100 but it doesnt seem to work

    My code is below and if anybody has any pointers or advice, that would be great! Not looking for the answer but a push in the right direction would be great :-)

    Cheers

    #include <stdio.h>
    main (void)

    {
    int num;

    printf("Enter a Number: ");
    scanf_s("%d", &num);

    if ((num % 10 == 1) && (num != 11))
    {
    printf("%dst\n", num);
    }

    if ((num % 10 == 2) && (num != 12))
    {
    printf("%dnd\n", num);
    }

    if ((num % 10 == 3) && (num !=13))
    {
    printf ("%drd\n", num);
    }

    if ((num >= 4) && (num <=20))
    {
    printf ("%dth\n", num);
    }

    system ("pause");
    return 0;
    }


Comments

  • Registered Users Posts: 4,769 ✭✭✭cython


    I think I would first recommend that you look into the else clause that can be used with if statements. You only want one of the printf's to be called for the number, and lists of if statments one after the other like you have there can result in two conditions being satisfied, in which case you will get 2 ordinals printed. Admittedly some people will argue that it's stylistic, but in general I think it is better practice.

    Alternatively, you might be able to use a switch - often a useful alternative to loads of if statements. You can only really test one condition/quantity with it, but you can use if statements within each case or the default. It might not be too elegant a solution to this specific problem though, all aspects considered. A rearrangement of what you have might look like this:
    #include <stdio.h>
    main (void)
    
    {
    int num;
    
    printf("Enter a Number: ");
    scanf_s("%d", &num);
    switch (num % 10)
    {
    case 1:
      if(num != 11)
        printf("%dst\n", num);
      else
        printf ("%dth\n", num);
      break;
    case 2:
      if(num != 12)
        printf("%dnd\n", num);
      else
        printf ("%dth\n", num);
      break;
    case 3:
      if(num != 13)
        printf("%drd\n", num);
      else
        printf ("%dth\n", num);
      break;
    default:
      printf("%dth\n", num);
    }
    
    system ("pause");
    return 0;
    }
    

    As to the specific issue you are having, I think you should try to separately isolate the 2 least significant digits of the number that you are dealing with. First of all get the least significant figure (rightmost digit), then the next-to-least significant figure. Once you have those, you can tell whether you are in the teens or not, and chose which set of ordinals to use, so to speak. Then you can examine the LSF and select the appropriate ordinal from the range. For example:
    #include <stdio.h>
    main (void)
    
    {
    int num;
    int ones;
    int tens;
    
    printf("Enter a Number: ");
    scanf_s("%d", &num);
    
    int ones = num % 10; //ones should be in the range 0-9
    int tens = ((num - ones) % 100) / 10; // tens should also be in this range
    
    // Examine the variables ones and tens here, and select appropriate ordinal
    
    system ("pause");
    return 0;
    }
    

    I hope what I've given here is some use to you. I'm trying to find a balance between giving you starting points, without actually doing it for you.


Advertisement