sprintf problems

Discussion in 'C Programming' started by merrittr, May 21, 2007.

  1. merrittr

    merrittr Guest

    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;
    }
     
    merrittr, May 21, 2007
    #1
    1. Advertising

  2. merrittr

    Jack Klein Guest

    On 20 May 2007 23:43:15 -0700, merrittr <> 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
     
    Jack Klein, May 21, 2007
    #2
    1. Advertising

  3. merrittr

    Flash Gordon Guest

    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
     
    Flash Gordon, May 21, 2007
    #3
  4. merrittr

    merrittr Guest

    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
     
    merrittr, May 21, 2007
    #4
  5. On 21 May, 08:43, merrittr <> 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
     
    Nick Keighley, May 21, 2007
    #5
  6. merrittr

    Flash Gordon Guest

    Nick Keighley wrote, On 21/05/07 12:55:
    > On 21 May, 08:43, merrittr <> 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
     
    Flash Gordon, May 21, 2007
    #6
  7. merrittr

    Guest

    On May 21, 8:43 am, merrittr <> 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.
     
    , May 21, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,612
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,088
  3. CJ
    Replies:
    1
    Views:
    1,355
    Davlet Panech
    Oct 28, 2003
  4. Yodai

    problems with sprintf...

    Yodai, Jan 13, 2004, in forum: C Programming
    Replies:
    13
    Views:
    597
    Yodai
    Jan 14, 2004
  5. jt
    Replies:
    6
    Views:
    370
    -berlin.de
    May 23, 2005
Loading...

Share This Page