C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 6th, 2003, 12:09 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old March 6th, 2003, 12:31 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
Find the result of the number modulo 10. That will give you the last digit.

last_digit = number % 10;

Reply With Quote
  #3  
Old March 6th, 2003, 01:40 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
im not sure if that will work

if the user types 5006 the suffix will be 5006 th

because the last digit is 6 which has a suffix of th

i will get a decimal number when im storing in an integer.

Reply With Quote
  #4  
Old March 6th, 2003, 02:00 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
>> i will get a decimal number when im storing in an integer.
I'm not sure what you mean by this. Do you mean a floating point number?? You're not going to get floating point numbers when your variables are declared as int.
Code:
void suffix(int n, char suff[]) {
  int last_digit = n % 10;

  if (last_digit == 1)
     sprintf(suff, "%dst", n);
  else if (last_digit == 2)
     sprintf(suff, "%dnd", n);
  else if (last_digit == 3)
     sprintf(suff, "%drd", n);
  else
     sprintf(suff, "%dth", n);
}

Hope this helps!

Reply With Quote
  #5  
Old March 6th, 2003, 03:11 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by Scorpions4ever
>> i will get a decimal number when im storing in an integer.
I'm not sure what you mean by this. Do you mean a floating point number?? You're not going to get floating point numbers when your variables are declared as int.
Code:
void suffix(int n, char suff[]) {
  int last_digit = n % 10;

  if (last_digit == 1)
     sprintf(suff, "%dst", n);
  else if (last_digit == 2)
     sprintf(suff, "%dnd", n);
  else if (last_digit == 3)
     sprintf(suff, "%drd", n);
  else
     sprintf(suff, "%dth", n);
}

Hope this helps!


thanks for the reply

could you do me a favour and explain why you are using modulus 10 and what the sprintf command is doing. I havent come across it before thats why. Will your method work if the user types in 6777 as the number will it be able to recongnize the last digit which is 7 and assign it a th ?

Reply With Quote
  #6  
Old March 6th, 2003, 04:26 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
>> could you do me a favour and explain why you are using modulus 10
It's modulo, not modulus. What the modulo operator does is to return the remainder of a division operation. For example, 5437 modulo 10 returns 7 (i.e. 5437 / 10 = 543 with a remainder of 7). Hence, when you modulo divide a number by 10, you get the last digit.

>> what the sprintf command is doing
sprintf works similar to printf, except it puts the result into the first argument, instead of the screen. I'm assuming here that you know what the printf() function does. So in our case sprintf(suff, "%dst", n) will put the result into the suff char array.

>> Will your method work if the user types in 6777
Yes it will.

Reply With Quote
  #7  
Old March 6th, 2003, 09:02 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Scorpions4ever, remember what Neildadon stated in the first post: the numbers 11, 12 and 13 do not end with 'st', 'nd' or 'rd' - they end with 'th'. Your code does not check these conditions. The last 2 digits of the number must be checked for these 3 cases (x % 100 would suffice to get the last 2 digits). Then, your code would be fine (x %10 to get the last digit).

Reply With Quote
  #8  
Old March 6th, 2003, 10:29 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
Good call. I was only paying attention to this part of his post:
>> is there a command that can read the last digit of an integer number ?
That's the part I was paying attention to

Reply With Quote
  #9  
Old March 7th, 2003, 06:34 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Understood. I just thought I'd mention it in case a passerby grabbed your code unknowingly thinking it would handle the original question asked.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > returning an appropriate string for an expected int value??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap