newbie question

S

some

Hi All,

I am trying to write a function similar to scanf which uses scanf to do
most of the work. It is working correctly for char,int but does not
work for double. I read the FAQ's 11.3,15.2,15.10 from
http://www.eskimo.com/~scs/C-faq/ but that could not help me. I would
appreciate any help.

Please excuse if my question is in a inccorrect group or very silly of
me to ask such a thing.

Thanks a lot.

I wrote something similar to this

I know the code is elementary and has lot of bugs.

////////////////////////////////////////////////
#include<stdio.h>
#include<stdarg.h>
#define MAX 25
void scanfunction(char *fmt,...);
int main(int argc,char *argv[]) {
int num;
char chr;
double floatnum;
char str[MAX];
scanfunction("%d %c %s %f ",&num,&chr,str,&floatnum);
return 0;
}

void scanfunction (char *fmt,...) {
va_list arg;
char *str,*s;
int *num;
char *chr;
double *floatnum;
va_start(arg,fmt);
for(s=fmt;*s;s++) {
if(*s != '%')
continue;
switch(*++s) {
case 'd' :
num = va_arg(arg,int*);
fflush(stdin);
printf("enter a num \n ");
scanf("%d",num);
printf("we read %d \n ",*num);
break;
case 'c' :
chr = va_arg(arg,char*);
fflush(stdin);
printf("enter a char \n ");
scanf("%c",chr);
printf("we read %c \n ",*chr);
break;
case 'f' :
floatnum = va_arg(arg,double*);
fflush(stdin);
printf("enter a float \n ");
scanf("%f",floatnum);
printf("we read %f \n ",*floatnum);
break;
case 's' :
str = va_arg(arg,char*);
fflush(stdin);
printf("enter a string \n ");
scanf("%s",str);
printf("we read %s \n ",str);
break;

default :
printf("incorrect format to be inputted \n");
break;
}
}
va_end(arg);
}

///////////////////////////////

Thanks a lot.
 
E

Eric Sosman

some said:
Hi All,

I am trying to write a function similar to scanf which uses scanf to do
most of the work. It is working correctly for char,int but does not
work for double. [...]

The "%f" specifier for scanf() converts a float;
use "%lf" for double.
 
M

Michael Mair

some said:
Hi All,

I am trying to write a function similar to scanf which uses scanf to do
most of the work. It is working correctly for char,int but does not
work for double. I read the FAQ's 11.3,15.2,15.10 from
http://www.eskimo.com/~scs/C-faq/ but that could not help me. I would
appreciate any help.

Please excuse if my question is in a inccorrect group or very silly of
me to ask such a thing.

Thanks a lot.

I wrote something similar to this

Please, give us the _real_ code or a minimal example created
from it. Otherwise, the error might be in the part you left
out or paraphrased.

I know the code is elementary and has lot of bugs.

////////////////////////////////////////////////
#include<stdio.h>
#include<stdarg.h>
#define MAX 25
void scanfunction(char *fmt,...);
int main(int argc,char *argv[]) {
int num;
char chr;
double floatnum;
char str[MAX];
scanfunction("%d %c %s %f ",&num,&chr,str,&floatnum);

If this is a scanf()-like format string, do not forget to
make that %lf, %lg, or whatever for doubles. %f only scans
for floats and assumes that the passed address is the address
of a float.
return 0;
}

void scanfunction (char *fmt,...) {
va_list arg;
char *str,*s;
int *num;
char *chr;
double *floatnum;
va_start(arg,fmt);
for(s=fmt;*s;s++) {
if(*s != '%')
continue;
switch(*++s) {
case 'd' :
num = va_arg(arg,int*);
fflush(stdin);

fflush() has only defined behaviour for _output_.
What you want is s.th. like
int ret;
while ( (ret=getchar()) != EOF )
if (ret == '\n')
break;
or similar.
printf("enter a num \n ");

_Now_ would be the time for fflush(stdout) in order to make
sure that the output is printed to the console so that the
user knows what is happening.
scanf("%d",num);
printf("we read %d \n ",*num);
break;
case 'c' :
chr = va_arg(arg,char*);
fflush(stdin);
printf("enter a char \n ");
scanf("%c",chr);
printf("we read %c \n ",*chr);
break;
case 'f' :
floatnum = va_arg(arg,double*);
fflush(stdin);
printf("enter a float \n ");
scanf("%f",floatnum);

Here you go:
scanf("%lf",floatnum);
(See above for an explanation).
printf("we read %f \n ",*floatnum);
break;
case 's' :
str = va_arg(arg,char*);
fflush(stdin);
printf("enter a string \n ");
scanf("%s",str);
printf("we read %s \n ",str);
break;

default :
printf("incorrect format to be inputted \n");
break;

One case I would add:
case '%' (read '%')
}
}
va_end(arg);
}

///////////////////////////////

BTW: It is considered better to use blanks (spaces) instead of tabs for
code posted to newsgroups.


Cheers
Michael
 
S

some

Thanks Eric/Michael,

My mistake was using %f where I should have done a %lf. Thanks for your
help. I had done a very silly mistake.
BTW: It is considered better to use blanks (spaces) instead of tabs for
code posted to newsgroups.
You mean I should leave some blanks before and after I post code or
something else instead of using /// like I did. Can you please explain
so that I can be careful in future.

Thanks
 
E

Emmanuel Delahaye

some wrote on 24/02/05 :
You mean I should leave some blanks before and after I post code or
something else instead of using /// like I did. Can you please explain
so that I can be careful in future.

No, it's something else. For example, on your code :

int main(int argc,char *argv[]) {
int num;
^
| you have used the TAB character
here.

The problem is that some news readers clients (or is it some news
server, I don't know) strip the TAB and consequently, your code appears
like

int main(int argc,char *argv[]) {
int num;

which is not the expected way. To prevent that (and actually it solves
a lot of presentation elswere too), it is highly recommended to use
spaces instead of tabs:

[three spaces for a TAB]

int main(int argc,char *argv[]) {
int num;

Most of the text editors used to write code have this function ("insert
n spaces when TAB is pressed"). Lurk at(for, in? please correct me) the
options.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top