me lamer need help- fprintf

This is a discussion on me lamer need help- fprintf within the C Programming forums, part of the General Programming Boards category; Hi everyone well I have a new problem that makes me go crazy. let me explain this code enter n, ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    22

    me lamer need help- fprintf

    Hi everyone

    well I have a new problem that makes me go crazy.

    let me explain this code

    enter n, steps x1

    example:
    n=6 ; x1=1.25 ---->1.25 2.5 3.75 5 6.25 7.5
    calculate yi= xi^2 + 3xi +3
    and fprintf to test.dat.

    Then use the data to draw with gnuplot on screen, but thats another story.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main (void)
    {
    
       double *y, *x, xi, x1;
       int i,n;
       FILE *fp;
       printf(" n-->"); scanf("%f",&n);
       printf("x1-->"); scanf("%f", &x1);
       y=   malloc( n*sizeof(double) );
       x=   malloc( n*sizeof(double) );
       fp=fopen("test.dat", "w");
       xi=x1;
       for (i=0; i<n; i++)
       {
    	   y[i]= xi*xi - 3*xi +3;
    	   x[i]= xi;
    	   xi= xi+x1;
    	   fprintf(fp,"%f  %f\n",y[i], x[i]);
    	   fflush(fp);
    
       }
    
       fclose(fp);
       free(y);
       free(x);
       return 0;
    }
    result endless loop, huge growing .dat file 14Meg
    and this
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    -1.#QNAN0 -1.#QNAN0
    .
    .

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here are the offending lines:

    printf(" n-->"); scanf("%f",&n);
    printf("x1-->"); scanf("%f", &x1);

    In the first scanf the variable 'n' is of type integer but you try and read it as a floating-point number. You need %d or %i instead of %f. In the second x1 is a double floating-point. You try to read it with a single floating-point specifier. You need %lf for long float.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > printf(" n-->"); scanf("%f",&n);
    > printf("x1-->"); scanf("%f", &x1);
    Get a compiler which can diagnose scanf abuse
    First one should be %d, since n is an integer
    Second one should be %lf, because x1 is a double
    Both values will be junk in your code.

    Also, since you calculate and use x[i] and y[i] in the same loop iteration, there is no actual need to store these values in an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    silly me. THX!!!!!


    well I use lcc win32, and it didnīt give a warning ar an error. Any suggestion?
    Last edited by staticalloc; 09-22-2004 at 02:33 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    No idea - read the manual page for whatever warnings it has, and turn them all on.

    gcc for example has -Wall which checks a whole bunch of stuff
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 03:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 09:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21