Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sprintf problems

Reply
Thread Tools

sprintf problems

 
 
merrittr
Guest
Posts: n/a
 
      05-21-2007
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;
}

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      05-21-2007
On 20 May 2007 23:43:15 -0700, merrittr <(E-Mail Removed)> wrote in
comp.lang.c:

> 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
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      05-21-2007
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.
--
Flash Gordon
 
Reply With Quote
 
merrittr
Guest
Posts: n/a
 
      05-21-2007
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

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-21-2007
On 21 May, 08:43, merrittr <(E-Mail Removed)> wrote:

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.


--
Nick Keighley

"Remember, the TAB key is your friend!!"
Computer Training at Large Insurance Company

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-21-2007
Nick Keighley wrote, On 21/05/07 12:55:
> On 21 May, 08:43, merrittr <(E-Mail Removed)> wrote:
>
> leave some context in your posts


Because of this

<snip>

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


<snip>

> 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.
--
Flash Gordon
 
Reply With Quote
 
Francine.Neary@googlemail.com
Guest
Posts: n/a
 
      05-21-2007
On May 21, 8:43 am, merrittr <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a function similar to C++'s sprintf Pep Java 5 08-25-2005 08:23 PM
Problems with sprintf, need help on this one jt C Programming 6 05-23-2005 11:11 AM
sprintf shea martin Java 5 09-03-2004 01:40 AM
problems with sprintf... Yodai C Programming 13 01-14-2004 07:51 PM
String class, how to implement sprintf? CJ C++ 1 10-28-2003 10:54 PM



Advertisments