Fscanf not working properly

This is a discussion on Fscanf not working properly within the C Programming forums, part of the General Programming Boards category; Program reads 4 values from a file (x1, y1, x2, y2) and calculates the slope-intercept form of their line. The ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Fscanf not working properly

    Program reads 4 values from a file (x1, y1, x2, y2) and calculates the slope-intercept form of their line.

    The program simply will not do what I want, though, as the slope always comes up as zero.

    Code:
    /*Brendan Keeler - Oct. 5, 2009 - The purpose of this program is to find the slope-intercept form of a given line.*/
    
    #include <stdio.h>
    
    float slope(x1, y1,x2, y2){
    	float s=0;
    	if ((x2 - x1)== 0){
    		printf("Not expressible in slope-intercept form");
    	}
    	else{
    		s = (y2 - y1)/(x2-x1);
    	}
    	return s;
    }
    
    float y_int(x1, y1, slope){
    	float b;
    	b = -slope*(x1)+y1;
    	return b;
    }
    
    void disp_slope_int(float slope, float y_int){
    
    	printf("Slope-intercept form is:\n\n y = %fx + %f\n", slope, y_int);
    }
    
    main(){
    
    	FILE *fin;
    	float x1, y1, x2, y2, m, b;
    	int i = 0;
    	float numbers[4];
    	fin = fopen("input.txt", "r");
    	while(fscanf(fin, "%d", &numbers[i])) { 
          		i++;
    	}
    	x1 = numbers[1];
    	y1 = numbers[2];
    	x2 = numbers[3];
    	y2 = numbers[4];
    	m = slope(x1,y1, x2, y2);
    	b = y_int(x1,y1,m);
    	disp_slope_int(m,b);
    	fclose(fin);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ye canna read float numbers with %d.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Fixed that to %f but it's still not working.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    why don't you do fscanf(fin,"%f %f %f %f", &x1, &y1, &x2, &y2) directly, instead of using an array?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also arrays go from 0-3 not 1-4.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Still not working with either fix. Coordinates and slope still comes out as zero after fscanf and arithmetic.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Post your latest code.

    Also I think some of your functions need types for the arguments:
    Code:
    float slope(float x1, float y1, float x2, float y2){
    	float s=0;
    	if ((x2 - x1)== 0){
    		printf("Not expressible in slope-intercept form");
    	}
    	else{
    		s = (y2 - y1)/(x2-x1);
    	}
    	return s;
    }
    
    float y_int(float x1, float y1, float slope){
    	float b;
    	b = -slope*(x1)+y1;
    	return b;
    }
    Otherwise, the compiler might assume ints and that could be the cause of some of your problems.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Never use == with floating point numbers. If you do not know why, run this very simple program and check the output.

    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    Some numbers (such as 0.1) cannot be exactly represented and depending on an exact representation will lead to error. You need to use <=, >= or some combination of both.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    the main function is an int type and also needs a return somewhere...should be:

    Code:
    int main () {
    ...
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    Post your latest code.

    Also I think some of your functions need types for the arguments:
    Code:
    float slope(float x1, float y1, float x2, float y2){
    	float s=0;
    	if ((x2 - x1)== 0){
    		printf("Not expressible in slope-intercept form");
    	}
    	else{
    		s = (y2 - y1)/(x2-x1);
    	}
    	return s;
    }
    
    float y_int(float x1, float y1, float slope){
    	float b;
    	b = -slope*(x1)+y1;
    	return b;
    }
    Otherwise, the compiler might assume ints and that could be the cause of some of your problems.
    This solved it. Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  3. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  4. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  5. MergeSort function not working with randomn number
    By Madshan in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2005, 12:15 AM

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