TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: Frank Adam
date: 1996-09-13 14:40:04
subject: getnum

Hi guys,

Well, if you thought the datediff was weird, here is something i've done
some time ago when trying to grasp type casting.I thought i've lost it, 
dammit i didn't. In those days i was drinking light beer i think..:-)

#include 
#include 
#include 

#define OK           1
#define BADNUM       0

#define ASINT        0
#define ASUINT       1
#define ASLONG       2
#define ASULONG      3
#define ASDOUBLE     4


int getnum(int,void*);
char* getstr(char*,int);

main()
{
int a;unsigned int b;long c;unsigned long d;double e;
if(getnum(ASINT,&a) == OK) printf("\nInteger = %d\n",a);
if(getnum(ASUINT,&b) == OK) printf("\nUnsigned = %u\n",b);
if(getnum(ASLONG,&c) == OK) printf("\nLong = %ld\n",c);
if(getnum(ASULONG,&d) == OK) printf("\nUnsigned long = %lu\n",d);
if(getnum(ASDOUBLE,&e) == OK) printf("\nDouble = %f",e);
return 0;
}

int getnum(int type,void *num)
{
char *ptr;
char input[21];
printf("\nEnter a number :");
getstr(input,20);
if(!*input) return BADNUM;
switch(type)
{
 case ASINT      :*(int*) num = atoi(input);return OK;
 case ASUINT     :*(unsigned int*) num = atoi(input);return OK;
 case ASLONG     :*(long*) num = atol(input);return OK;
 case ASULONG    :*(unsigned long*)num = strtoul(input,&ptr,10);return OK;
 case ASDOUBLE   :*( double *)num = strtod(input,&ptr);return OK;
 default         : break;
 }
return BADNUM;
}

char* getstr(char *retstr, int len)
{
 fgets(retstr,len-1,stdin);
 retstr[strlen(retstr) - 1] = '\0';
 return retstr;
 }

Hm, on second look there is nothing really wrong with it.
                                                                    
 L8r Frank (fadam{at}ozemail.com.au)
 
___ Blue Wave/DOS v2.21

--- Gash
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.