sprintf problems

M

merrittr

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;
}
 
J

Jack Klein

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?
sprintf( i1, "%s %s \0",v3,v4 );

Do you realize that this sprintf() just overwrites the value of the
last sprintf()?
printf("answer %s\n",add(cTypeCurr,i1,i2));

What is 'cTypeCurr'?
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
 
F

Flash Gordon

merrittr wrote, On 21/05/07 07:43:
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.
 
M

merrittr

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
 
N

Nick Keighley

leave some context in your posts
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

so why don't you POST A COMPLETE COMPILABLE EXAMPLE? As you
have been asked to (twice). Don't put in things like
"typically" and "this was left over from one I did before".

For instance this is complete and compilable. It is also
problably wrong because you made me guess a lot

****************
#include <stdio.h>
#include <stdlib.h>

char *add (char *ctype, char *i, char *j)
{
double x,y,z;
static char retval[10] = "retval";

printf ("---%s %s %s\n", retval, i, j);
x = atof(i);
y = atof(j);
z = x + y;
sprintf (retval, "%f", z);

return retval;
}

int main (void)
{
char i1 [100];
char i2 [100];
char *r1;
char *r2;
char *v1 = "5 i";
char *v2 = "4.5 d";
char *v3 = "5 i"; /* 4 variables and you give me two
examples... */
char *v4 = "4.5 d";

sprintf (i1, "%s %s", v1, v2);
sprintf (i2, "%s %s", v3, v4);


r1 = add("h",i1,i2); /* no idea what cTypeCur is */
printf ("answer %s\n", r1);
r2 = add("i","3 i","4 i");
printf ("answer %s\n", r2);

return 0;
}

*************


the output looks like this:-

G:\Debug>mer.exe
---retval 5 i 4.5 d 5 i 4.5 d
answer 10.000000
---10.000000 3 i 4 i
answer 7.000000

which is different from yours so I guessed wrong.
 
F

Flash Gordon

Nick Keighley wrote, On 21/05/07 12:55:
leave some context in your posts

Because of this


so why don't you POST A COMPLETE COMPILABLE EXAMPLE? As you
have been asked to (twice). Don't put in things like
"typically" and "this was left over from one I did before".

and this which Nick pointed out, Merrittr has lost the opportunity of me
trying to help, and others may feel the same. If s/he can't be bothered
to make it easy for those helping the chances people being helpful
rapidly decrease.

I may or may not change my mind if s/he takes note and posts sensibly.
 
F

Francine.Neary

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

Guess 1: You didn't allocate enough (any?) memory for i1 and i2.
Guess 2: Bug in the function add you tell us nothing about results in
the garbage.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top