Thread: Help with struct program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Help with struct program

    Ok I have the majority of this program complete. I've gotten the program to print all of the names along with all of the salaries.

    One question...how would I go about calculating who has the largest salary and getting the program to print only that?

    Code:
    #include <stdio.h>
    #include <string.h>
          
       typedef struct{
        
       char name[31];
       float wages;
       } people_t;
          
    int main() {  
        
       people_t people[30];
       int i = 0;
       char person[31];
           
       FILE *inFile1;
       FILE *inFile2;
             
       inFile1 = fopen("people.txt", "r");
       if(inFile1 == NULL) {
          printf("%s failed.  Exiting program\n", "people.txt");
          exit(1);
       }
           
       inFile2 = fopen("wages.txt", "r");
       if(inFile2 == NULL) {
          printf("%s failed.  Exiting program\n", "wages.txt");
          exit(2);
       }
       
       fscanf(inFile1, "%s", &person);
       while( i < 20 && !feof(inFile1)){
           strcpy(people[i].name, person);
           fscanf(inFile1, "%s", &person);
           i++;
            }
       
       i = 0;
       fscanf(inFile2, "%f", &people[i].wages);
       while( i < 20 && !feof(inFile2)){
           i++;
           fscanf(inFile2, "%f", &people[i].wages);
       }
       fclose(inFile1);
       fclose(inFile2);
        
       i = 0;
       printf("\t Name \t \t \t Wages\n");
       while (i < 20) {
          printf("\t %s \t \t %.2f\n", people[i].name, people[i].wages);
          i++;
       }
             
       return 0;
       
    }

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    There's a magical thing called a for loop. It doesn't answer your question but your program could use a few of em.
    Don't quote me on that... ...seriously

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    // In an array of people
    float max = people[1]
    for each person in people array
       if max < person's wage
          max = person's wage
    
    // when the loop breaks, max has the highest salary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM