C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sprintf problems

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
merrittr
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: sprintf problems Reply with quote



I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i1, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i

the function:

char *add(char *ctype, char *i, char *j)
{
double x,y,z,a;
static char retval[10]="retval";
printf("---%s %s %s\n",retval,i,j); <--output line
x=atof(i);
y=atof(j);
z=x+y;
sprintf( retval, "%f",z );
return retval;
}
Back to top
Flash Gordon
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Re: sprintf problems Reply with quote



merrittr wrote, On 21/05/07 07:43:
Quote:
I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );

<snip>

You probably have something wrong before this line is executed. However,
since you have not provided a *complete* compilable example showing the
problem it is anybodies guess.

One thing I do know is that you need to reread the chapter in your text
book that introduces string literals, since you do not need the explicit \0.
--
Flash Gordon
Back to top
Jack Klein
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Re: sprintf problems Reply with quote



On 20 May 2007 23:43:15 -0700, merrittr <merrittr (AT) gmail (DOT) com> wrote in
comp.lang.c:

Quote:
I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );

What is the definition of 'i1', and what are the contents of 'v1' and
'v2'? Are v1 and v2 strings, or are they numeric values?

Quote:
sprintf( i1, "%s %s \0",v3,v4 );

Do you realize that this sprintf() just overwrites the value of the
last sprintf()?

Quote:
printf("answer %s\n",add(cTypeCurr,i1,i2));

What is 'cTypeCurr'?

Quote:
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG? èG?
---0.000000 3 i 4 i

the function:

char *add(char *ctype, char *i, char *j)
{
double x,y,z,a;
static char retval[10]="retval";
printf("---%s %s %s\n",retval,i,j); <--output line
x=atof(i);
y=atof(j);
z=x+y;
sprintf( retval, "%f",z );
return retval;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Back to top
merrittr
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Re: sprintf problems Reply with quote

the v1,v2,v3,v4 vars ar built like this:

cVal=getchar();
while(cVal != '\n')
{
v1[iCharCount++] = tolower(cVal);
cVal=getchar();
}
v1[iCharCount++]='\0';
iCharCount=0;

typically vi v2 v3 v4 are "5 i" or "4.5 d"

cTypeCurr = "i" or "d" I don't think I will need this variable it is a
remnant of something I was trying.


whoops the second i1 should have been i2

I added the \0 after I was getting these results , sort of as a long
shot
to see if my string termination wasn't working. So sprintf null
terminates eh?


sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i2, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.