Thread: c prog. reading student with highest avg marks,name and student id

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    16

    c prog. reading student with highest avg marks,name and student id

    The code i made is used to read a .txt file and retrieve the largest integer. But im having trouble on how i can make the code read student name and id for student with high avg marks.

    in the txt file there will
    Roll no. Name mark

    example:
    190 james 100

    i would really appreaciate if you guys could help me out. thank you.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include <stdio.h>
    int main()
    {
        FILE *sensor;
        int num,max, sum, count, first;
    
    
            sensor = fopen("C:\\Users\\SMLUser\\Desktop\\test.txt", "r");
            if (sensor != NULL)
            {
                for (sum = count = first = 0;
                fscanf(sensor, "%d", &num) == 1; sum += num, ++count)
                    if (!first)
                    {
                        max = num; first = 1;
                    }
                    else if (num > max) max = num;
                    
                fclose(sensor);
                printf("max = %d \n",max);
            }
            else
            {
                printf("Unable to read file 'C:\\Users\\SMLUser\\Desktop\\test.txt" );
            }
        getch();
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    for (sum = count = first = 0;
    what does this line do?

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    it supposed to calculate the sum

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    This is your code:

    Code:
    #include<stdio.h>
    //#include<conio.h>
    #include<stdlib.h>
    #include <stdio.h>
    int main()
    {
        FILE *sensor;
        int num,max, sum, count, first;
     
     
            sensor = fopen("C:\\Users\\SMLUser\\Desktop\\test.txt", "r");
            if (sensor != NULL)
            {
                /* for */
                for (sum = count = first = 0;fscanf(sensor, "%d", &num) == 1; sum += num, ++count); if (!first)
                    {
                        max = num; first = 1;
                    }
                    else if (num > max) max = num;
               /* end-for */
                     ; /* Do nothing */
                    
                fclose(sensor);
                printf("max = %d \n",max);
            }
            else
            {
                printf("Unable to read file 'C:\\Users\\SMLUser\\Desktop\\test.txt" );
            }
      //  getch();
    }
    The format for a for statement is for ( clause-1 ; expression-2 ; expression-3 ) statement;

    What are clause-1, expression-2, expression-3 and statement in your code?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can do something like
    Code:
    char buffer[1024];
    while(fgets(buffer,sizeof buffer, sensor))
    {
        int num, mark;
        char temp_name[100];
        if(scanf("%d %99s %d", &num, temp_name, &mark) == 3)
        {
           /* do your work here - check if the mark is highest  so far and copy temp_name to name array */
        }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    I was able to update the code but now im getting random number at the end of reading the file.

    Code:
    void fileprint(){
      int c;
            struct student{
                   long id;
                   char name[20];
                    int mid1;
      };
    		int i;
            struct student x[20];
            FILE *file;
            file=fopen("student.txt","r");
    		system("cls");
     if(file==NULL){
    	printf("Can not open the file\n"); 
        		exit(-1);
    }
    for(i=0;i<10;i++)
    { 
    fscanf(file,"%d %s %d\n",&x[i].id,x[i].name,&x[i].mid1);
    printf ("%d %s %d\n",x[i].id,x[i].name,x[i].mid1);
    }
    
    
    fclose(file);
    getch();	
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First, indent your code properly, e.g.,
    Code:
    void fileprint() {
        int c;
        struct student{
            long id;
            char name[20];
            int mid1;
        };
        int i;
        struct student x[20];
        FILE *file;
        file = fopen("student.txt", "r");
        system("cls");
        if (file == NULL)
        {
            printf("Can not open the file\n");
            exit(-1);
        }
        for (i = 0; i < 10; i++)
        {
            fscanf(file, "%d %s %d\n", &x[i].id, x[i].name, &x[i].mid1);
            printf("%d %s %d\n", x[i].id, x[i].name, x[i].mid1);
        }
    
        fclose(file);
        getch();
    }
    Next, what is the content of the file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    hi laserlight the content of the file has 2 integers and one string

    please see the example below:

    Roll no. Student Name Mark
    109 James 90

    so i need the code to get the highest mark from the listed records.

    the txt file could have more than 1000 entries.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by akosinoah
    the content of the file has 2 integers and one string

    please see the example below:

    Roll no. Student Name Mark
    109 James 90
    That's your problem right there: your example file only has one entry, but you are reading 10 entries from the file. Thus, you need to check the return value of fscanf before you presume to have read the entry (and thus do things like print it).

    Quote Originally Posted by akosinoah
    the txt file could have more than 1000 entries.
    Of course, your array of 20 struct student objects cannot hold so many entries. Is there some pre-determined maximum?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    Thank you for replying laserlight.

    Actually the number of records are random depending on the records inserted by the user.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by akosinoah
    Actually the number of records are random depending on the records inserted by the user.
    Yes, yes, but my question remains: what is the maximum number of records that a user is permitted to enter?

    If the maximum is only limited by memory, or is considerably large, then you're going to have to start using dynamic memory allocation. But that is going to make your code more complex, so start by picking some reasonable maximum and implementing your program with that assumption.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    oh i see in that case it would be 100 entries maximum the user are permitted to enter.

  13. #13
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    hi laserlight can you advise how i can make the code get the highest avg from the list of records? i would really appreciate if you could guide me.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by akosinoah
    how i can make the code get the highest avg from the list of records?
    What idea do you have?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    I have updated the code to read the txt file. but i need to get the highest mark from the list of students

    can you advise on how i can achieve that?

    Code:
    void readfile()
    {   
    int c;
    struct student
    {
    long id;
    char name[20];
    int mid1;
    };
          
    		
            struct student x[100];
    		 
            FILE *file;
    		 int i;
    		 i=0;
    		
            file=fopen("student.txt","r");
            system("cls");
    		 i=0;
       while((fscanf(file, "%ld %s %d\n",&x[i].id,x[i].name,&x[i].mid1)) == 3) 
       {
          printf("%ld %s %d\n",x[i].id,x[i].name,x[i].mid1);
          ++i;
    
    
       }
       printf("\n\n\nPRESS ANY KEY TO EXIT");
    		
    		getch();
    		
    		
    fclose(file);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Student who needs help
    By Phazawhy in forum C Programming
    Replies: 2
    Last Post: 08-19-2007, 05:26 AM
  2. new student
    By monu in forum C Programming
    Replies: 2
    Last Post: 08-07-2007, 06:23 AM
  3. Student Looking for Help Again.
    By verd in forum C Programming
    Replies: 4
    Last Post: 03-29-2005, 06:05 PM
  4. Student need your help
    By pinkpanther1586 in forum C Programming
    Replies: 13
    Last Post: 08-23-2003, 06:44 PM
  5. I need help on Student record prog
    By Hursh in forum C Programming
    Replies: 2
    Last Post: 01-18-2002, 07:37 AM