Function which returns a number and it's suffix

Hi

I need help in writing a single function which sets a small array (with 2 characters) to the ordinal suffix of a number.(Ordinals are 1st, 2nd, 3rd, 4th etc and the suffix refers to the letters.)

I need to write the function with the following signiture

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

Assuming the following declarations

1
2
3
char s[5];

int x;


the result of a call:

suffix(x,s);

will return an appropriate string in s for any given positive value of x from 0 to 9999.

Any help/hints on writing the functtion would be great.

Thanks in advance.
Ok So I have had a go in trying to code what I stated above and this is what I have come up with so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

void suffix(int n, char suff[]);
// creates the ordinal suffix
// for a given number

int main()
{
  char s[5];
  int x;

  cout << "Enter a number to find the ordinal suffix for ";
  cin >> x;
  suffix(x,s);
}

void suffix(int n, char suff[])
{
   if(n%100 == 11 || n%100 == 12 || n%100 == 13)
    {
      cout << "suffix is: " << n << "th";
      cout << endl;
    }
  else
    {
      if(n%100 == 1)
	{
	  cout << "Suffix is: " << n << "st";
	  cout << endl;
	}
      else
	{
	  if(n%100 == 2)
	    {
	      cout << "Suffix is: " << n << "nd";
	      cout << endl;
	    }
	  else
	    {
	      if(n%100 == 3)
		{
		  cout << "Suffix is: " << n << "rd";
		  cout << endl;
		}
	      else
		{
		  if(n%100 == 4 || n%100 == 5 || n%100 == 6 || n%100 == 7 || n%100 == 8 || n%100 == 9 || n%100 == 0)
		      {
			cout << "Suffix is: " << n << "th";
			cout << endl;
		      }
		}
	    }
	}
    }
}


Can you tell me if I am on the right path, and is there a way to shorten it so it is not so nested?

Thanks if you can help.
Make it so it can be used with this:
const char *suffixes[]={"th","st","nd","rd"};

By the way, n%100==[0..99], not [0..9], so you should take a look at your conditions. n%10==[0..9].
I've finished my program, however, I am a little stuck on getting the program to set a value outside a permitted range string to "**".

Any Ideas on how I can make this work? I know I need to use the strcpy, but I need help on how to use it in my program.

Thanks if you can help.
 
char* strcpy( char* dst, const char* src );


so pass suff as the first parameter and the string "**" as the second.
well my code is this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  if (n%100 == 11 || n%100 == 12 || n%100 == 13)
    {
      cout << "Suffix is: " << n << strcpy(suff,"th") << endl;
    }
  else if (n%10 == 1)
    {
      cout << "Suffix is: " << n << strcpy(suff,"st") << endl;
    }
  else if (n%10 == 2)
    {
      cout << "Suffix is: " << n << strcpy(suff,"nd") << endl;
    }
  else if (n%10 == 3)
    {
      cout << "Suffix is: " << n << strcpy(suff,"rd") << endl;
    }
  else if (n > 9999)
    {
      cout << "Suffix is: " << strcpy(suff,"**") << endl;
    }
  else
   { 
      cout << "Suffix is: " << n << strcpy(suff,"th") << endl;
   }


And I can't get it to output to ** if it is out of range, can you help me here?
In else/if chains like this, it stops checking further conditions once the first matching condition is found. For example if you run 10013 through this if/else chain, it will match the first condition because n%100 == 13 returns true -- even though the number if out of range. This is why numbers are not even getting to your n > 9999 check.

To fix this, rearrange this chain so that the "higher priority" conditions are first. For instance you might want to make the > 9999 check the first in the chain since it's the most significant.
I don't really see any reason for such an arbitrary limit. 10000th is a perfectly valid ordinal.
Topic archived. No new replies allowed.