Help with struct program

This is a discussion on Help with struct program within the C Programming forums, part of the General Programming Boards category; Ok I have the majority of this program complete. I've gotten the program to print all of the names along ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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-02-2007, 12:48 AM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 03:56 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM

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