Thread: is the data being read?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    is the data being read?

    I compile my program and it compiles fine...but when I run it..I come up with zero's for all integers and blanks for strings...

    can anyone see anything wrong? I at my wits ends and now wondering if the data is even being read properly...

    Code:
    #include <iostream>
    using namespace std;
    #include <iomanip.>
    #include <stdio.h>
    #include <string.h>
    #include <cstring>
    
    struct employee 
    {
        int id;
        char name[10];
        float pr;
        int csalary;
    	int raise;
        double rate;
        int nsalary;
    };
    
    void alpha(struct employee *records);
    void calc(struct employee *records);
    void display(struct employee *records);
    
    main()
    {
        char tmp[100];
    	struct employee records[14];
    	int i=0;
    	FILE *fPtr;
    	fPtr = fopen("lab2.txt", "r");
    	do
    	{
    		  fgets(tmp,200,fPtr);
    		  sscanf(tmp, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary);
    		}
    	while (!(feof(fPtr)));
    		printf("ID    Name      PR       Current Salary    $    pct    New Salary    ");
    		printf("\n================================================================\n");
    
    	//readfile(records, buf);
    	alpha(records);
    	calc(records);
    	display(records);
    	
    }
    
    void alpha(struct employee *records)
    {
    	{
    	for(int n=1; n<= 13; n++)
    		for (int i=1; i<= 12; i++ )
    		{
    			int j=i++;
    			int result = strcmp ( records[i].name,  records[j].name);
    			if ( result > 0 )
    				swap( i,j );
    		}
    	}
    
    }
    
    void calc(struct employee *records)
    {
    	for (int i=0; i<=12; i++){
    		
    		if (records[i].pr ==1)
                records[i].rate = 1.045;
            if (records[i].pr ==2)
                records[i].rate = 1.06;
            if (records[i].pr ==3)
                records[i].rate = 1.078;
            if (records[i].pr ==4)
               records[i].rate = 1.09;
    
            records[i].nsalary = (records[i].csalary*records[i].rate);
            records[i].raise = (records[i].nsalary-records[i].csalary);
            records[i].rate = ((records[i].rate*100)-100);
    	}
    }
    
    void display(struct employee *records)
    {
    	for (int i=0; i<=12; i++)
        printf("%d	%s	%.0f		%d	%d	%.2f	%d\n", records[i].id, records[i].name, records[i].pr, records[i].csalary, records[i].raise, records[i].rate, records[i].nsalary);	
    }
    Code this

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Bet your wondering why no-one is replying.

    Have a look at which forum this is.

    Try the C++ forum.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Sorry your coding seems all C but you include the C++ specific header files and using namespace std.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by bigtamscot
    Sorry your coding seems all C but you include the C++ specific header files and using namespace std.

    yes I know, our teacher says he doesnt matter what we code in. So the headers are basically a default for most programs...
    Code this

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    do
        {
              fgets(tmp,200,fPtr);
              sscanf(tmp, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary);
            }
    you do not increment (i) in the loop so element 0 of your array of records is changing in each loop.

    Thats just for starters.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by bigtamscot

    Thats just for starters.

    oh boy...

    Code:
    	for (i=0;i<=14;i++);
    	do{
    		  fgets(tmp,200,fPtr);
    		  sscanf(tmp, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary);
    		}
    Code this

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Also you declare tmp as a character array to hold 100 elements (100 bytes of memory) and then read a file at 200 bytes. Just asking for trouble.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by bigtamscot
    Also you declare tmp as a character array to hold 100 elements (100 bytes of memory) and then read a file at 200 bytes. Just asking for trouble.

    yes you are right, I was tweaking numbers around and never changed that one...thanks
    Code this

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    By the look of his code I dont think he knows what language he is coding in.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    When no one helps you out. Call google();

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > our teacher says he doesnt matter what we code in
    Your teacher is just as clueless as the class then - no wonder you're having so many problems.

    Code:
        do
        {
              fgets(tmp,200,fPtr);
              sscanf(tmp, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary);
            }
        while (!(feof(fPtr)));
    1. You LIE to fgets about the size of the buffer it has.
    2. You use !feof() - see the FAQ
    3. You ignore all status returns.
    4. You don't increment your index variable.

    Code:
    while ( i < 14 && fgets(tmp,sizeof tmp,fPtr) != NULL ) {
        if ( sscanf( sscanf(tmp, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary) == 4 ) {
            i++;
        } else {
            fprintf( stderr, "Bad record: %s\n", tmp );
        }
    }
    Of course, the 14 should be a #define CONSTANT which is also used in the size of your 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 07:15 AM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM