returning an appropriate string for an expected int value ??

This is a discussion on returning an appropriate string for an expected int value ?? within the C++ Programming forums, part of the General Programming Boards category; Hi can anyone help? Im trying to write a single function which sets a small array with 2 characters to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    returning an appropriate string for an expected int value ??

    Hi can anyone help?

    Im trying to write a single function which sets a small array with 2 characters to the ordinal suffix of a number. 1st, 2nd, 3rd 4th etc

    and the suffix refers to the laters.

    my function prototype is

    void suffix(int n, char suff[]);

    and i have made the following declarations

    char s[5];
    int x;

    and with the result of the call

    suffix(x,s);

    will give the appropriate string in s for any given positive value of x from 0 to 9999

    a possible pattern i have found is that 4 - 20 end in th

    then the first three after that have st,nd,rd

    is there a command that can read the last digit of an integer number ?

    because if there is then i can use conditions easier and compare the last digit against the tests to see what its suffix ( letters ) are.

    any help much appreciated.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    how would you strip that last digit from the number using maths?

    maybe like this....

    last_digit = num%10;

    you need a bigger array also.

    9999th is 7 chars long. That cant be stored in 5
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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