Thread: help w/ program

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

    help w/ program

    Hello all

    I need some help with my program. Here is the jist of it. I need to write a program that pulls data from another (i.e. employee id, name, performance rating, and current salary)

    then take that data and perform some calculations and the redisplay it in alphabitical order.

    here is the data file...(lab2.txt)

    339 GOERGE 4 26000
    221 SANDY 4 22600
    101 RONNY 3 35250
    115 MILLY 1 40000
    445 PETE 2 20335
    553 BRIAN 3 34650
    209 RUSS 1 40000
    199 ANGIE 4 49000
    241 BARRY 3 50000
    322 MIKE 2 13400
    789 PETER 3 35500
    900 RICK 2 46000
    111 WENDY 2 21000



    and here is my program....My problem (at the moment is that my output is one line of o's)...


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    struct employee {
        int id;
        char name[10];
        int pr;
        int csalary;
        int raise;
        float rate;
        int nsalary;
    };
    
    main()
    {
        int id;
        char name[10];
        int pr;
        int csalary;
        int raise;
        float rate;
        int nsalary;
    
    FILE *fPtr;
    struct employee emp;
    
    fPtr = fopen("lab2.txt", "r");
        printf("ID    Name    PR    Current Salary    $    pct    New Salary    ");
        printf("\n================================================================\n");
    
    
        if(fPtr != NULL){
            fread(&emp, sizeof(int),1,fPtr);
            fscanf(fPtr, "%s", name);
            fclose(fPtr);
        //while (!(feof(fPtr)))
        //{
        //fscanf(fPtr,"%d%s%d%d",&id,name,&pr,&csalary);
        //emp.raise= (emp.csalary * emp.rate);
        //emp.nsalary= (emp.csalary + emp.raise);
    
        }
        printf("%d    %s    %d        %d    %d    %.2f    %d\n\n", emp.id,emp.name,emp.pr,emp.csalary,emp.raise,emp.rate,emp.nsalary);
    
    //fclose (fPtr);
    return 0;
    
    }
    thanks
    Code this

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    [edit]doh! deleted...wasn't paying close attention (still workin on my morning coffee) and was WAY off.
    Last edited by Scribbler; 02-15-2005 at 09:25 AM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    ok gettin back to the core of the problem...


    why is this not showing any output?
    Code:
    //#include <iostream>
    #include <stdio.h>
    
    //using namespace std;
    
    struct employee {
        int id;
        char name[10];
        int pr;
        int csalary;
    };
    
    main()
    {
        int i,id;
        char name[10];
        int pr;
        int csalary;
        int raise;
        float rate;
        int nsalary;
    
    FILE *fPtr;
    struct employee emp;
    
    fPtr = fopen("lab2.txt", "r");
        printf("ID    Name    PR    Current Salary    $    pct    New Salary    ");
        printf("\n================================================================\n");
    
        if(fPtr != NULL){
            fread(&emp, sizeof(fPtr),1,fPtr);
            
    
            printf("%d    %s    %d        %d    %d    %.2f    %d\n\n", emp.id,emp.name,emp.pr,emp.csalary,raise,rate,nsalary);
    
    
    }
    }
    Code this

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't use fread() like that unless you wrote to the file with something like fwrite(). Try using fgets() instead. If you don't understand why fread() isn't going to work in your situation then let me know and I'll explain it.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    The main problem I see with your code is that you are using fread() on a text file - this will give you the wrong output 'cos that function is meant for reading in binary data. I would recommend using fgets() as described in the faq (which I also recommend you read!) then use sscanf() or strtok() or similar to extract the info you want.

    ::edit:: beaten to it yet again!
    DavT
    -----------------------------------------------

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by itsme86
    You can't use fread() like that unless you wrote to the file with something like fwrite(). Try using fgets() instead.

    I think I understand why I should not use fread, thanks

    And I know fgets reads an entire line...but how would I write that?

    fgets(?,100,fPtr);


    Code this

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You'll have to create a buffer to hold the data. It's usually done something like:
    Code:
    {
      char buf[100];
    
      // Open the file, etc. here
    
      fgets(buf, sizeof(buf), fptr);
    
      // Use something like sscanf() like DavT suggested to parse the string.
    
      // Don't forget to close the file.
    }
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    understood!

    Now I see that the my char buf will hold for example the first line...

    now I need to use sscanf to break that line up into variables? i.e. id, name, pr, salary?


    fyi:
    here is my code now...
    Code:
    //#include <iostream>
    #include <stdio.h>
    
    //using namespace std;
    
    struct employee {
    	int id;
    	char name[10];
    	int pr;
    	int csalary;
    };
    
    main()
    {
    	int id;
    	char name[10];
    	int pr;
    	int csalary;
    	int raise;
    	float rate;
    	int nsalary;
    	char buf[100];
    
    FILE *fPtr;
    struct employee emp;
    
    fPtr = fopen("lab2.txt", "r");
    	printf("ID	Name	PR	Current Salary	$	pct	New Salary	");
    	printf("\n================================================================\n");
    
    	if(fPtr != NULL){
    		fgets(buf,100,fPtr);
    		
    		printf("\n\n%s",buf);
    		sscanf(
    		printf("%d	%s	%d		%d	%d	%.2f	%d\n\n", emp.id,emp.name,emp.pr,emp.csalary,raise,rate,nsalary);
    }
    }
    Code this

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Exactly.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    now that I have the reading of the file done... How can I arrange alphabetically by name?

    Code:
    //#include <iostream>
    #include <stdio.h>
    
    //using namespace std;
    
    struct employee {
    	int id;
    	char name[10];
    	int pr;
    	int csalary;
    };
    
    main()
    {
    	int id;
    	int scale;
    	char name[10];
    	float pr;
    	int csalary;
    	int raise;
    	double rate;
    	int nsalary;
    	char buf[100];
    
    FILE *fPtr;
    struct employee emp;
    
    fPtr = fopen("lab2.txt", "r");
    	printf("ID	Name	PR	Current Salary	$	pct	New Salary	");
    	printf("\n================================================================\n");
    
    	do
    	{
    		fgets(buf,100,fPtr);
    		sscanf(buf, "%d %s %f %d", &id, &name, &pr, &csalary);
    		if(pr==1)
    			rate = 1.045;
    		if(pr==2)
    			rate = 1.06;
    		if (pr==3)
    			rate = 1.078;
    		if (pr==4)
    			rate = 1.09;
    
    		nsalary = (csalary*rate);
    		raise = (nsalary-csalary);
    		rate = ((rate*100)-100);
    		printf("%d	%s	%.0f		%d	%d	%.2f	%d\n", id,name,pr,csalary,raise,rate,nsalary);
    }
    	while (!(feof(fPtr)));	
    }
    Also..I dont need to display them before they are sorted?
    Code this

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're going to have to read the data into (probably) an array of struct employee. Once all the data is read in you can sort the array by the name member and then print out each record.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by itsme86
    an array of struct employee.
    isnt that this...
    Code:
    struct employee {
        int id;
        char name[10];
        int pr;
        int csalary;
    	int raise;
        double rate;
        int nsalary;
    };
    ....

    Code:
            fgets(buf,200,fPtr);
            sscanf(buf, "%d %s %f %d", &id, &emp.name, &pr, &csalary);
    Last edited by SpEkTrE; 02-15-2005 at 01:29 PM.
    Code this

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You've defined the structure, but you haven't actually made any instances of it yet. In main() you should do something like struct employee records[10]; for instance if you want to be able to handle 10 employees. Then you can do things like records[0].name as the first employee's name, records[1].id as the second employee's ID, etc.

    For instance, if you wanted to print each employee's name you could do:
    Code:
    {
      int i;
    
      for(i = 0;i < 10;++i)
        printf("%s\n", records[i].name);
    }
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Quote Originally Posted by itsme86
    struct employee records[10];

    I believe I'm with you so far...

    Code:
            fgets(buf,200,fPtr);
            sscanf(buf, "%d %s %f %d", &records[i].id, &records[i].name, &records[i].pr, &records[i].csalary);
    
    is it safe to perform the sort here and now?
    ....
    
    
    printf("%d %s     %.0f         %d    %d    %.2f    %d\n", records[i].id,records[i].name,records[i].pr,records[i].csalary,raise,rate,nsalary);
            i++;
    Last edited by SpEkTrE; 02-15-2005 at 02:11 PM.
    Code this

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can do it there or after you're done reading the data in from the file. If you do it in the reading loop, you'll only need to find where to put that one record into the array.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM