Thread: Help on while function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Help on while function

    this error message is coming up for this function:

    Code:
    while (points != EOF) 
    	{
    		fprintf("%lf %lf\n", x[i], y[i]);
    		i++;
    		points = scanf(out, "%lf%lf", x[i],y[i]);
    	}
    error C2440: 'function' : cannot convert from 'float' to 'const char *'


    Can anyone help me please...

    Thank you very much.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Here is the program I am trying to write.

    Code:
    #include <stdio.h>
    int get_corners (FILE *inp, double x[], double y[], int z);
    int output_corners(FILE *inp, double x[], double y[], int z);
    int main()
    {
    	FILE *inp;
    	FILE *out;
    	double x[7], y[7];
    	int k;
    	inp = fopen("file.txt", "r");
    	out = fopen("out.txt", "w");
    	k = get_corners(inp, x, y, 5);
    	return (0);
    }
     
    int get_corners (FILE *inp, double x[], double y[], int z)
    {
    	int i = 0, points;
    	printf("Display Corners\n\n");
    	points = fscanf(inp, "&#37;lf %lf", &x[i], &y[i]);
    	printf("***********\n");
    	while (points != EOF) 
    {
    
    	printf("%.2lf , %.2lf\n", x[i], y[i]);
    	i++;
    	points = fscanf(inp, "%lf%lf", &x[i], &y[i]);
    }
    	printf("***********\n");
    	return (i);
    }
     
     
     
     
    int output_corners(FILE *out, float x[],  float y[], int z)
    {
    	int i = 0, points;
    	
    	fprintf("Output Data\n");
    	points = fprintf(out, "%lf %lf", x[i], y[i]);
    	while (points != EOF) 
    	{
    		fprintf("%.2lf %.2lf\n", x[i], y[i]);
    		i++;
    		points = fprintf(out, "%lf%lf", x[i],y[i]);
    	}
    
    return (i);
    }

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Three quick points:

    1. Your output_corners() function definition doesn't match the prototype.
    2. fprintf() takes a FILE* as the first argument. You're sometimes passing a char*. Either use printf() or specify the FILE*; you can use stderr and stdout, although the latter is equivalent to printf().
    3. You should use &#37;f to print out doubles, not %lf. %lf is correct for scanf(), but not printf() in C89.
    Last edited by cas; 10-30-2007 at 04:58 PM. Reason: Accidentally submitted before done (can I blame my browser?)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    while (points != EOF) 
    	{
    		fprintf("%lf %lf\n", x[i], y[i]);
    		i++;
    		points = scanf(out, "%lf%lf", x[i],y[i]);
    	}
    As stated, fprintf() takes a FILE * as the first argument.

    It seems like "out" is superfluous on this function

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Thank you for your help.




    I was able to get the program in this form:

    Code:
    #include <stdio.h>
    int get_corners (FILE *inp, double x[], double y[], int z);
    int output_corners(FILE *out, double x[], double y[], int z);
    int poly_area(FILE *inp, double x[], double y[], int z);
    int main()
    {
    	FILE *inp;
    	FILE *out;
    	
    	double x[7], y[7];
    	int k;
    	
    	
    	inp = fopen("file.txt", "r");
    	k = get_corners(inp, x, y, 5);
    	
    	
    	out = fopen("out.txt", "w");
    	k = output_corners(out, x, y, 5);
    	
    	return (0);
    }
     
    int get_corners (FILE *inp, double x[], double y[], int z)
    {
    	int i = 0, points;
    	
    points = fscanf(inp, "&#37;lf %lf", &x[i], &y[i]);
    	for (i = 0; i < 5; i++);
    {
    	printf(inp, "%lf , %lf\n", x[i], y[i]);
    	points = fscanf(inp, "%lf%lf", &x[i], &y[i]);
    }
    
    	return (i);
    }
     
    int output_corners(FILE *out, double x[], double y[], int z)
    {
    	int i = 0, points;
    	//fprintf("Output Data\n");
    	points = fprintf(out, "%lf %lf", x[i], y[i]);
    	while (points != NULL) 
    	{
    	printf(out, "%lf %lf\n", x[i], y[i]);
    	i++;
    	points = fprintf(out, "%lf %lf", x[i],y[i]);
    }
    
    return (i);
    }
    it has no "c" errors but something is wrong with the values that it is returning.

    The txt file has numbers such as 1 2 4 7 6 8 1... and it is returning something similar to : 1.0 2.0 -9235243500000000000000000.0 -92342340000000000000000.0

    any ideas or help?

    Thanks once again
    1.0 2.0 -9000000000000000000000000000000000
    Last edited by raortega3; 10-31-2007 at 01:40 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right %lf is the right form for scanf to read a double, but it's also the right form for printing a long double. Since long double isnt't what your data is, then you'll be printing "garbage".

    Use %f for printing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    int get_corners(FILE * inp, double x[], double y[], int z);
    int output_corners(FILE * out, double x[], double y[], int z);
    int poly_area(FILE * inp, double x[], double y[], int z);
    int main()
    {
        FILE *inp;
        FILE *out;
    
        double x[7], y[7];
        int k;
    
    
        inp = fopen("file.txt", "r");
        k = get_corners(inp, x, y, 5);
    
    
        out = fopen("out.txt", "w");
        k = output_corners(out, x, y, 5);
    
        return (0);
    }
    
    int get_corners(FILE * inp, double x[], double y[], int z)
    {
        int i = 0, points; /* you only ever assign points, test it as well */
    
        points = fscanf(inp, "%lf %lf", &x[i], &y[i]);
        for (i = 0; i < 5; i++);/* This ; will hurt you */
        {
            printf(inp, "%lf , %lf\n", x[i], y[i]);
            points = fscanf(inp, "%lf%lf", &x[i], &y[i]);
        }
    
        return (i);
    }
    
    int output_corners(FILE * out, double x[], double y[], int z)
    {
        int i = 0, points;
        points = fprintf(out, "%lf %lf", x[i], y[i]);
        while (points != NULL) { /* NULL is not one of the return values */
            printf(out, "%lf %lf\n", x[i], y[i]);
            i++;
            points = fprintf(out, "%lf %lf", x[i], y[i]);
        }
    
        return (i);
    }
    First off, maximise the warning level of the compiler (or get gcc) and pay attention to what it says.
    With your latest effort, there's plenty to fix
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `get_corners':
    foo.c:31: warning: passing arg 1 of `printf' from incompatible pointer type
    foo.c: In function `output_corners':
    foo.c:41: warning: ISO C90 does not support the `%lf' printf format
    foo.c:41: warning: ISO C90 does not support the `%lf' printf format
    foo.c:42: warning: comparison between pointer and integer
    foo.c:43: warning: passing arg 1 of `printf' from incompatible pointer type
    foo.c:45: warning: ISO C90 does not support the `%lf' printf format
    foo.c:45: warning: ISO C90 does not support the `%lf' printf format
    foo.c: At top level:
    foo.c:38: warning: unused parameter 'z'
    foo.c:24: warning: unused parameter 'z'
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM