user4287698
user4287698

Reputation:

How to save/retrieve data in C?

It has been a while since I program in C.

I was wondering on how to save/retrieve a data in C. The program suppose to print out the avg grade of all students at the end.

Let's say

Result should print out like this.

A:1 B:1 C:0 D:0 F:1

Every student, I print out the result. I can do that no problem. But printing all of them out at the end, I don't know how to do that.

I was thinking to save a student grade to a certain variable each time the loop run. and call every single one of them back in this A:1 B:1 C:0 D:0 F:1 format at the end.

I am NOT asking you guys to correct my code. I know.

Let's get to the point. I am asking, how do I print out like that.

A:1 B:1 C:0 D:0 F:1

if you don't know how to do it move on. Please do NOT ruin my comments section.

main.c

#include<stdio.h>
#include <string.h>
#include <math.h>

void main_menu(void)
{
    printf("\n ");
    printf(" Menu Options");
    printf(" 1.Enter grades");
    printf(" 2.Display the results");
    printf(" 3.Quit ");
    printf("\n ");
}

void display_menu(void)
{
    printf("\n ");
    printf(" Menu Options");
    printf(" 1.Table of letter grades");
    printf(" 2.Histogram");
    printf(" 3.Main Menu ");
    printf("\n ");
}


void display_grade(void)
{

    float average;
    int stud;


if(average >=97 && average <= 100)
{
    printf("A+\n\n");
}
else if(average >=93 && average <= 96)
{
    printf("A\n\n");
}
else if(average >=90 && average <= 92)
{
    printf("A-\n\n");
}
else if(average >=87 && average <= 89)
{
    printf("B+\n\n");
}
else if(average >=83 && average <= 86)
{
    printf("B\n\n");
}
else if(average >=80 && average <= 82)
{
    printf("B-\n\n");
}
else if(average >=77 && average <= 79)
{
    printf("C+\n\n");
}
else if(average >=73 && average <= 76)
{
    printf("C\n\n");
}
else if(average >=70 && average <= 72)
{
    printf("C-\n\n");
}
else if(average >=67 && average <= 69)
{
    printf("D+\n\n");
}
else if(average >=63 && average <= 66)
{
    printf("D\n\n");
}
else if(average >=60 && average <= 62)
{
    printf("D-\n\n");
}

else
{
    printf("F\n\n");
}

     ++stud;

}


int main(void)
{


    int n , stud = 1, class_size, menu_option, test_grade,quiz_grade,hw_grade, test_avg, quiz_avg, hw_avg, total_avg;

    float average;  
    printf(" Please enter your Class Size: ");
    scanf("%i", &class_size);
    main_menu();
    printf(" Please enter your choice: ");
    scanf("%i", &menu_option );
    printf("Class size: %i | Menu Option : %i\n", class_size, menu_option );

    if(menu_option == 1){

        printf("Student %i:", stud);

        int test_sum = 0, quiz_sum = 0, hw_sum = 0;

        printf("Tests:");

        for(n=1; n<=2; ++n)
        {
            printf("Enter grade %i: ", n);

            scanf("%i", &test_grade);

            if(test_grade>=0 && test_grade <=100)
            {

                test_sum = test_sum + test_grade;

            }
            else
            {
                n=n-1;
                printf("Invalid grade.. Please try again");
            }
        }


        printf("Quizes:");
        for(n=1; n<=3; ++n)
        {
            printf("Enter grade %i: ", n);

            scanf("%i", &quiz_grade);

            if(quiz_grade>=0 && quiz_grade <=100)
            {

                quiz_sum = quiz_sum + quiz_grade;

            }
            else
            {
                n=n-1;
                printf("Invalid grade.. Please try again");
            }
        }


        printf("Homeworks:");
        for(n=1; n<=10; ++n)
        {
            printf("Enter grade %i: ", n);

            scanf("%i", &hw_grade);

            if(hw_grade>=0 && hw_grade <=100)
            {

                hw_sum = hw_sum + hw_grade;

            }
            else
            {
                n=n-1;
                printf("Invalid grade.. Please try again");
            }
        }

        test_avg = (test_sum/2) * .4 ;
        quiz_avg = (quiz_sum/3) * .3 ;
        hw_avg = (hw_sum/10) * .3 ;

        total_avg = test_avg + quiz_avg + hw_avg;
        average = total_avg;                

        printf("\tStudent average: %6.2f ", average);  

        display_grade();

        main_menu();

        printf(" Please enter your choice: ");
        scanf("%i", &menu_option );

    }else if ( menu_option == 2){

        int display_choice;
        int A=0;
        int B=0;
        int C=0;
        int D=0;
        int F=0;

        display_menu();
        printf(" Please enter your choice: ");
        scanf("%i", &display_choice);

        if(display_choice == 1){

                printf("A:%i    B:%i    C:%i    D:%i    F:%i\n",A,B,C,D,F);
                printf("\n");

        }

    return 0;
}

Upvotes: 1

Views: 1103

Answers (2)

thurizas
thurizas

Reputation: 2528

So it seems to me, that because the class size is not known until run time you will need some type of dynamic structure. Also, you have two pieces of data (student name and grade) that you want to keep together. This is how I would approach this, treat this as a skeleton solution.

  1. First, seeing as how you have two pieces of data, I'd recommend a structure:

    typedef struct s_tag
    {
        char*       name;
        int         grade;
    } studentT, *pstudentT;
    
  2. So for the dynamic part, assuming the size of the class is stored in size;

       pstudentT      pClass = malloc(size*sizeof(studentT));
    
  3. Now as loop get student data, (presented as pseudo-code):

       ndx <- 0
       for(ndx = 0; ndx < size; ndx++)
           name <- read student name
           grade <- read student grade
    
           pClass[ndx].name = malloc(strlen(name) * sizeof(char) + 1)
           strcpy(pClass[ndx].name, name);
           pClass[ndx].grade = grade
    
  4. once you have this information in memory is should be easy to create display functions (or modify the grade field into an array to hold the individual test and quiz data

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

The actual problem with your code, apart from some minor issues with the user interface is that void display_grade(void) function declares float average; and int stud;.

You expect them to be the same as those declared in the main function but your assumtion is incorrect, to fix it you should pass them to the function, one of them by value, while the other by reference like this void display_grade(float average, int *stud), so when you want to increment stud you do it like this ++(*stud).

And you call it from main like this display_grade(average, &stud);

Upvotes: 0

Related Questions