fscanf function won't read my float value...?

This is a discussion on fscanf function won't read my float value...? within the C Programming forums, part of the General Programming Boards category; alright, well heres the problem... i have to read this data from a file... Employee_No. Department Pay_Rate Exempt Hours_Worked Base_Pay ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Angry fscanf function won't read my float value...?

    alright, well heres the problem... i have to read this data from a file...

    Employee_No. Department Pay_Rate Exempt Hours_Worked Base_Pay
    0101 41 8.11 Y 49
    0722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45

    i need to create an employee register.. heres what i have so far.. the problem IS i cant figure out how to get the pay rate to read so that i can multiply it by the hours worked in order to calculate the base pay... I COULD use %s to read the pay rate, but you can't multiply an integer by a sequence... i feel like i've tried everything ALL day trying to figure out how to get the fscanf function to read my pay rate numbers... any help would be really appreciated.

    Code:
    #include <stdafx.h>
    
    
    int main (void)
    {	
    //Local Declarations
    	FILE *spEmployeeData;
    	char* titl1 [12];
    	char* titl2 [11];
    	char* titl3 [8];
    	char* titl4 [8];
    	char* titl5 [12];
    	char* titl6 [8];
    	char* num [4];
    	int* deprt;
    	float* rate;
    	int* hours;
    	int* base;
    	int* exempt [1];
    	float* pay;
    
    //Statements
    	printf("\t\tEmployee files and payroll register.\n\n");
    	
    	spEmployeeData = fopen("EmployeeData.txt", "r");
    
    	if (!spEmployeeData)
    		{
    		printf("\t\tCould not open input file.\n");
    		return (1);
    		}// if open file fails
    
    	fscanf(spEmployeeData, "%s%s%s%s%s%s", &titl1, &titl2, &titl3, &titl4, &titl5, &titl6);
    		printf("\t%s %s %s %s %s %s\n", titl1, titl2, titl3, titl4, titl5, titl6);
    
    	fscanf (spEmployeeData, "%s%d%.2f", &num, &deprt, &rate);
    	
    	printf("%s %d %.2f", num, deprt, rate);//i need to continue with this pattern in order to read the rest of my data...
    	//i stopped here, because i can't get &rate to read properly!?
    	
    
    		fclose(spEmployeeData);
    	 
    		return 0;
    }//main

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	FILE *spEmployeeData;
    	char* titl1 [12];
    	char* titl2 [11];
    	char* titl3 [8];
    	char* titl4 [8];
    	char* titl5 [12];
    	char* titl6 [8];
    	char* num [4];
    	int* deprt;
    	float* rate;
    	int* hours;
    	int* base;
    	int* exempt [1];
    	float* pay;
    Looks like a good candidate for a struct if you ask me. Oh, and are you sure this is what you want:
    Code:
    type * name[ SIZE ];
    It means "name" is an array of SIZE elements of type pointer to type. So you've just got an array of pointers. You don't actually have anything usable here unless you're allocating memory some place else, or making those pointers point at something that already exists.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    and also
    fscanf (spEmployeeData, "%s%d%.2f", &num, &deprt, &rate);
    you can't do that

    and
    fscanf(spEmployeeData, "%s%s%s%s%s%s", &titl1, &titl2, &titl3, &titl4, &titl5, &titl6);
    all those addresses should not be there when you scanf into the strings

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Thumbs down welll...

    i could use fscanf and %c, but it just reads one character at a time. i dont know how else to read all the titles for my data without using %s...

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    [QUOTE=nynicue;875556]and also

    you can't do that

    and


    all those addresses should not be there when you scanf into the strings[/QUOTE

    what else could i use to read the 8.11?

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    i suppose i should scratch the whole thing and start over again.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    you can use %f insetead of your %.2f or %s and then convert it to float. I think it sould work.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Quote Originally Posted by nynicue View Post
    you can use %f insetead of your %.2f or %s and then convert it to float. I think it should work.
    i tried using %f and all that came up was a bunch of zero's.. the only thing i can get to read it is %s. i guess i will have to read about how to convert a string to a float. and btw... the


    char* titl1 [12];


    and so forth are pointing at the text file that i created with my employee information on it.. i noticed someone else said I should not use those?...that was the only way i could figure out how to properly get it to read the headings for my data file.. any other recommendations in how to do this? im gonna be busy reading on converting strings to floats. lol thanks.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Make sure you're taking the addresses of the variables you pass to scanf() when you use %f. For strings, you shouldn't, but for floats, you should. (The reason being that strings are really just arrays of characters, and as such if you pass the name of the array to scanf, it already has a pointer to the memory location and can fill it in with data. A float is just a number; for scanf() to be able to change the value inside /your/ float variable, it needs to know the address of that variable.)

    Post your latest code . . .

    Regarding this:
    [code]char* titl1 [12];[code]
    (I'm going to call it "title" instead of "titl" since that's what I automatically type.)

    title1[0] is a char*. You don't want that. What you want is for title[0] to be a char, and title[1] to be another char, etc. So try declaring title1 as
    Code:
    char title1[12];
    Then you'd pass it to scanf like so:
    Code:
    fscanf(..., "...%s...", ...title1...);
    Note that there's no ampersand there for the reasons I described above.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM

Tags for this Thread


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