🔒 Closed File modifying and deleting

Status
Not open for further replies.
Thanks d2 paps dami ko nakuha tulong at alam kuna para mag reduce ng size dahil dito
I would strongly advice to never use that method of calling "main" function from inside the "main" function or even anywhere else. You are doing recursion on the whole program itself. It can further complicate code and introduce more bugs. The effects are more evident when your programs get larger. It can easily eat all memory available.

What you should do instead is something like this.

Code:
#include <stdio.h>

void main()
{
    int exitFlag, input;

    exitFlag = 0;

    // This is your main program loop.
    while (exitFlag == 0)
    {

        // Put code here that needs to loop.
        printf("Give me any int value. If the value is 69 I will quit.\n");
        scanf("%d",&input);

        // To get out of this loop.
        // Just set exitFlag to 1.
        if (input == 69)
        {
            exitFlag = 1;
        }
    }
}
Kasi paps dpt daw nasa ibang function yung mga choices sa program yan kasi instruction eh
 
Pwedeng pwede mo pa rin i apply yung sinabi mo sa example ko.

Just like this.

Code:
#include <stdio.h>

void main()
{
    int exitFlag, input;

    exitFlag = 0;

    // This is your main program loop.
    while (exitFlag == 0)
    {

        // Put code here that needs to loop.
        printf("Please choose a task. Task number 5 will quit.\n");
        scanf("%d",&input);

        switch (input)
        {
            case 1 : add();
                        break; 

            case 2 : update();
                        break;

            case 3 : dl8();
                        break;
            
            case 4 : show();
                        break;

            case 5 : exitFlag = 1;
                       break;

            default : printf("Task number is not available. Please try again.\n");
        }
    }
}
 
Pwedeng pwede mo pa rin i apply yung sinabi mo sa example ko.

Just like this.

Code:
#include <stdio.h>

void main()
{
    int exitFlag, input;

    exitFlag = 0;

    // This is your main program loop.
    while (exitFlag == 0)
    {

        // Put code here that needs to loop.
        printf("Please choose a task. Task number 5 will quit.\n");
        scanf("%d",&input);

        switch (input)
        {
            case 1 : add();
                        break;

            case 2 : update();
                        break;

            case 3 : dl8();
                        break;
           
            case 4 : show();
                        break;

            case 5 : exitFlag = 1;
                       break;

            default : printf("Task number is not available. Please try again.\n");
        }
    }
}
Pag gamit kona sa functiin call kaya gumamit din ako ng main para bumalik sa main pero yung main function ko di ko na nilagyan
 
Pag gamit kona sa functiin call kaya gumamit din ako ng main para bumalik sa main pero yung main function ko di ko na nilagyan

Hindi mo kailangan mag call ng main function para lang bumalik sa main function. Tandaan mo lahat ng function pagkatapos nito magawa lahat ng instruction sa block/loob nya ipapasa nya pabalik ang daloy ng program sa huling tumawag sa kanya.

For example :

Code:
void foo()
{
    // code execution coming from main()
    // is now here

   printf("Foo : Hello too main!\n");

   // After the closing curly brackets
   // code execution will no go back
   // to main function as it was the last
   // one to call this function at this point.
}

void main()
{
    // The first actual code execution starts here.

    printf("Main : We will call foo.\nMain : Hi there foo!\n");

    // We will call function "foo".
    // Code execution will now jump to
    // function foo's definition.
 
    foo();

    // foo function has just ended.
    // now code execution is back to main function.
 
   printf("Main : I just called foo.\n");
}

Dyan makikita mo na pag natapos ma execute ang isang function ang daloy ng execution ng code ay babalik talaga sa pinakahuling tumawag nito. Hindi mo kailangan tawagin ang main function para bumalik uli ang daloy ng program mo pabalik sa main.
 
Kaya nga pa pinag halo mo ang ideya na yan. Pareha lang yan sa hinahanap mo na sitwasyon.

Code:
#include <stdio.h>

int quitFlag;

void do69()
{
    printf("Slurp. Slurp.\nOh yeah... You dirty boy!\n");
    quitFlag = 1;
}

void main()
{
    int input;

    quitFlag = 0;

    while (quitFlag != 1)
    {
        printf("Give me 69 and I will quit asking.\n");
        scanf("%d",&input);

        switch (input)
        {
            case 69 : do69();
                           break;
            default : printf("Come on. Really?\n");
        }
    }
}
 
Ito yung code.. Sabi ko sayo mas madali ang modification kapag nasa array eh. Ayaw mo maniwala.
Pag-aralan mo. Magtanong ka kung may hindi ka maintindihan.
Pag-aralan mo rin yung mga naming convention ng variables, paano ninaname ang mga function.
Tingnan mo kung paano ako mag-name at paano ka magname.
Ang pangit mo magname ng function. Wag mo pasakitin ulo ng professor mo.

Code:
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

struct Student
{
    char id[20];
    char name[256];
    char course[256];
    int year;
    double score[3];
    double total[3];
    double grade[3];
    double fgrade;
};

Student studentList[100];

const int SIZE = 100;
int sizeOfArray = 0;
FILE *f;

void AddStudent();
void UpdateStudent();
void DeleteStudent();
void SearchStudent();
void ShowStudentHighestToLowest();
void ShowStudentLowesToHighest();
void ShowStudentInfo(Student student);
void SaveToFile();
void SetStudentData();
void ShowList();

int main()
{
    char pick;
    int task;
   
    SetStudentData();
   
    while(true)
    {
        system("CLEAR");  
        printf("=========================================================");
        printf("\n=================Student Records MENU====================");
        printf("\n=========================================================");
        printf("\n# [1]Input New Student Record\t\t\t\t#");
        printf("\n# [2]Modify Student Record\t\t\t\t#");
        printf("\n# [3]Delete Student Record\t\t\t\t#");
        printf("\n# [4]Show All Existing Student Record\t\t\t#");
        printf("\n# [5]Show the information of an specific Student\t#");
        printf("\n# [6]Display Student Record[Best To Worst Final Grade]\t#");
        printf("\n# [7]Display Student Record[Worst To Best Final Grade]\t#");
        printf("\n# [8]Exit\t\t\t\t\t\t#");
        printf("\n=========================================================");
        printf("\nEnter Task: ");
        scanf("%d",&task);
        printf("=========================================================\n");

        switch(task)
        {
            case 1:
                AddStudent();
                SaveToFile();
                break;
            case 2:
                UpdateStudent();
                SaveToFile();
                break;
            case 3:
                DeleteStudent();
                SaveToFile();
                break;
            case 4:
                ShowList();
                break;
            case 5:
                SearchStudent();
                break;
            case 6:
                ShowStudentHighestToLowest();
                break;
            case 7:
                ShowStudentLowesToHighest ();
                break;
            case 8:
                printf("Thank You For Using The Student Database Program");
                return 0;
            default:
                printf("Invalid Option...\n");
                printf("============================================\n");
                cin.get();
                break;
        }  
    }
   
   
}


void AddStudent()
{
    int index = 0;
    for(int i = 0; i < SIZE; i++)
    {
        if(strcmp(studentList[i].name, "") == 0)
        {
            index = i;
            break;
        }
    }
   
    printf("Putting the student in index: %d", index);
    printf("\nEnter the data of the student\n");
    printf("ID Number: ");
    scanf("%s", studentList[index].id);
    printf("Name: ");
    scanf("%s", studentList[index].name);
    printf("Course: ");
    scanf("%s", studentList[index].course);
    printf("Year Level: ");
    scanf("%d", &studentList[index].year);
   
    for(int i = 0; i < 3; i++)
    {
        printf("Input Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].score[i]);
        printf("Input Total Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].total[i]);
        studentList[index].grade[i] = 5 - (4 * (studentList[index].score[i]/studentList[index].total[i]));
    }
    studentList[index].fgrade = studentList[index].grade[0] * 0.2 + studentList[index].grade[1]
            * 0.3 + studentList[index].grade[2] * 0.5;
   
    printf("\n\nSuccessfully added the student");
    cin.get();
    cin.get();
   
}
void UpdateStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
   
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
               
                printf("\nEnter the newdata of the student\n");
                printf("ID Number: ");
                scanf("%s", studentList[i].id);
                printf("Name: ");
                scanf("%s", studentList[i].name);
                printf("Course: ");
                scanf("%s", studentList[i].course);
                printf("Year Level: ");
                scanf("%d", &studentList[i].year);

                for(int j = 0; j < 3; j++)
                {
                    printf("Input Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].score[j]);
                    printf("Input Total Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].total[j]);
                    studentList[i].grade[j] = 5 - (4 * (studentList[i].score[j]/studentList[i].total[j]));
                }
                studentList[i].fgrade = studentList[i].grade[0] * 0.2 + studentList[i].grade[1]
                        * 0.3 + studentList[i].grade[2] * 0.5;
               
                printf("\n\nSuccessfully updated the student");
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
   
}
void DeleteStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
 
    while(true)
    {
        printf("\nEnter the ID number of the student you want to delete: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
               
                printf("\nYou really want to delete this data? [Y/N]:");
                scanf("%s", input);
                if(strcmp(input, "Y") == 0)
                {
                    for (int j = i; j < sizeOfArray - 1; j++)
                    {
                        studentList[j] = studentList[j+1];
                    }
                    printf("\n\nSuccessfully deleted the student");
                    sizeOfArray--;
                    cin.get();
                    cin.get();
                    break;
                }
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void SearchStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
   
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                ShowStudentInfo(studentList[i]);
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void ShowStudentHighestToLowest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade < studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
   
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void ShowStudentLowesToHighest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade > studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
   
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void SaveToFile()
{
    fclose(fopen("Record.txt", "w"));
    f = fopen("Record.txt", "w");

    if(f == NULL)
    {
        printf("Failed to open the file...");
    }
    else
    {
        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].name, "") != 0)
            {
                fprintf(f, "%s", studentList[i].id);
                fprintf(f, "\n%s", studentList[i].name);
                fprintf(f, "\n%s", studentList[i].course);
                fprintf(f, "\n%d\n", studentList[i].year);
               
                for(int j = 0; j < 3; j++)
                {
                    fprintf(f,"%.2lf\n",studentList[i].score[j]);
                    fprintf(f,"%.2lf\n",studentList[i].total[j]);
                    fprintf(f,"%.2lf\n",studentList[i].grade[j]);
                }
                fprintf(f,"%.2lf\n",studentList[i].fgrade);
             
            }
        }
    }
    fclose(f);
}
void SetStudentData()
{
    int index = 0;
    int c;
    f = fopen("Record.txt", "r");
   
    if(f == NULL)
    {
        printf("The file is not existing...");
    }
    else
    {
        while ((c = fgetc(f)) != EOF)
        {
            ungetc(c, f);
            fscanf(f,"%s",studentList[index].id);
            fscanf(f,"%s",studentList[index].name);
            fscanf(f,"%s",studentList[index].course);
            fscanf(f,"%d",&studentList[index].year);
            for(int i = 0; i < 3; i++)
            {
                fscanf(f,"%lf",&studentList[index].score[i]);
                fscanf(f,"%lf",&studentList[index].total[i]);
                fscanf(f,"%lf",&studentList[index].grade[i]);
            }
            fscanf(f,"%lf",&studentList[index].fgrade);
            index++;
            sizeOfArray++;
        }
    }
    fclose(f);
}
void ShowList()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        printf("ID Number:         %s\n", studentList[i].id);
        printf("Name:              %s\n", studentList[i].name);
        printf("Course:            %s\n", studentList[i].course);
        printf("Year Level:        %d\n", studentList[i].year);
               
        for(int a = 0; a < 3; a++)
        {
            printf("Score [%d]:         %.2f\n", a, studentList[i].score[a]);
            printf("Total Score [%d]:   %.2f\n", a, studentList[i].total[a]);
            printf("Grade [%d]:         %.2f\n", a, studentList[i].grade[a]);
        }
               
        printf("Final Grade:       %.2f\n\n", studentList[i].fgrade);
    }
   
    cin.get();
    cin.get();
}
void ShowStudentInfo(Student student)
{
    printf("ID Number:         %s\n", student.id);
    printf("Name:              %s\n", student.name);
    printf("Course:            %s\n", student.course);
    printf("Year Level:        %d\n", student.year);
               
    for(int a = 0; a < 3; a++)
    {
        printf("Score [%d]:         %.2f\n", a, student.score[a]);
        printf("Total Score [%d]:   %.2f\n", a, student.total[a]);
        printf("Grade [%d]:         %.2f\n", a, student.grade[a]);
    }
               
    printf("Final Grade:       %.2f\n\n", student.fgrade);
}
 
Ito yung code.. Sabi ko sayo mas madali ang modification kapag nasa array eh. Ayaw mo maniwala.
Pag-aralan mo. Magtanong ka kung may hindi ka maintindihan.
Pag-aralan mo rin yung mga naming convention ng variables, paano ninaname ang mga function.
Tingnan mo kung paano ako mag-name at paano ka magname.
Ang pangit mo magname ng function. Wag mo pasakitin ulo ng professor mo.

Code:
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

struct Student
{
    char id[20];
    char name[256];
    char course[256];
    int year;
    double score[3];
    double total[3];
    double grade[3];
    double fgrade;
};

Student studentList[100];

const int SIZE = 100;
int sizeOfArray = 0;
FILE *f;

void AddStudent();
void UpdateStudent();
void DeleteStudent();
void SearchStudent();
void ShowStudentHighestToLowest();
void ShowStudentLowesToHighest();
void ShowStudentInfo(Student student);
void SaveToFile();
void SetStudentData();
void ShowList();

int main()
{
    char pick;
    int task;
  
    SetStudentData();
  
    while(true)
    {
        system("CLEAR"); 
        printf("=========================================================");
        printf("\n=================Student Records MENU====================");
        printf("\n=========================================================");
        printf("\n# [1]Input New Student Record\t\t\t\t#");
        printf("\n# [2]Modify Student Record\t\t\t\t#");
        printf("\n# [3]Delete Student Record\t\t\t\t#");
        printf("\n# [4]Show All Existing Student Record\t\t\t#");
        printf("\n# [5]Show the information of an specific Student\t#");
        printf("\n# [6]Display Student Record[Best To Worst Final Grade]\t#");
        printf("\n# [7]Display Student Record[Worst To Best Final Grade]\t#");
        printf("\n# [8]Exit\t\t\t\t\t\t#");
        printf("\n=========================================================");
        printf("\nEnter Task: ");
        scanf("%d",&task);
        printf("=========================================================\n");

        switch(task)
        {
            case 1:
                AddStudent();
                SaveToFile();
                break;
            case 2:
                UpdateStudent();
                SaveToFile();
                break;
            case 3:
                DeleteStudent();
                SaveToFile();
                break;
            case 4:
                ShowList();
                break;
            case 5:
                SearchStudent();
                break;
            case 6:
                ShowStudentHighestToLowest();
                break;
            case 7:
                ShowStudentLowesToHighest ();
                break;
            case 8:
                printf("Thank You For Using The Student Database Program");
                return 0;
            default:
                printf("Invalid Option...\n");
                printf("============================================\n");
                cin.get();
                break;
        } 
    }
  
  
}


void AddStudent()
{
    int index = 0;
    for(int i = 0; i < SIZE; i++)
    {
        if(strcmp(studentList[i].name, "") == 0)
        {
            index = i;
            break;
        }
    }
  
    printf("Putting the student in index: %d", index);
    printf("\nEnter the data of the student\n");
    printf("ID Number: ");
    scanf("%s", studentList[index].id);
    printf("Name: ");
    scanf("%s", studentList[index].name);
    printf("Course: ");
    scanf("%s", studentList[index].course);
    printf("Year Level: ");
    scanf("%d", &studentList[index].year);
  
    for(int i = 0; i < 3; i++)
    {
        printf("Input Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].score[i]);
        printf("Input Total Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].total[i]);
        studentList[index].grade[i] = 5 - (4 * (studentList[index].score[i]/studentList[index].total[i]));
    }
    studentList[index].fgrade = studentList[index].grade[0] * 0.2 + studentList[index].grade[1]
            * 0.3 + studentList[index].grade[2] * 0.5;
  
    printf("\n\nSuccessfully added the student");
    cin.get();
    cin.get();
  
}
void UpdateStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
  
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
              
                printf("\nEnter the newdata of the student\n");
                printf("ID Number: ");
                scanf("%s", studentList[i].id);
                printf("Name: ");
                scanf("%s", studentList[i].name);
                printf("Course: ");
                scanf("%s", studentList[i].course);
                printf("Year Level: ");
                scanf("%d", &studentList[i].year);

                for(int j = 0; j < 3; j++)
                {
                    printf("Input Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].score[j]);
                    printf("Input Total Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].total[j]);
                    studentList[i].grade[j] = 5 - (4 * (studentList[i].score[j]/studentList[i].total[j]));
                }
                studentList[i].fgrade = studentList[i].grade[0] * 0.2 + studentList[i].grade[1]
                        * 0.3 + studentList[i].grade[2] * 0.5;
              
                printf("\n\nSuccessfully updated the student");
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
  
}
void DeleteStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
 
    while(true)
    {
        printf("\nEnter the ID number of the student you want to delete: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
              
                printf("\nYou really want to delete this data? [Y/N]:");
                scanf("%s", input);
                if(strcmp(input, "Y") == 0)
                {
                    for (int j = i; j < sizeOfArray - 1; j++)
                    {
                        studentList[j] = studentList[j+1];
                    }
                    printf("\n\nSuccessfully deleted the student");
                    sizeOfArray--;
                    cin.get();
                    cin.get();
                    break;
                }
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void SearchStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
  
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                ShowStudentInfo(studentList[i]);
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void ShowStudentHighestToLowest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade < studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
  
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void ShowStudentLowesToHighest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade > studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
  
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void SaveToFile()
{
    fclose(fopen("Record.txt", "w"));
    f = fopen("Record.txt", "w");

    if(f == NULL)
    {
        printf("Failed to open the file...");
    }
    else
    {
        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].name, "") != 0)
            {
                fprintf(f, "%s", studentList[i].id);
                fprintf(f, "\n%s", studentList[i].name);
                fprintf(f, "\n%s", studentList[i].course);
                fprintf(f, "\n%d\n", studentList[i].year);
              
                for(int j = 0; j < 3; j++)
                {
                    fprintf(f,"%.2lf\n",studentList[i].score[j]);
                    fprintf(f,"%.2lf\n",studentList[i].total[j]);
                    fprintf(f,"%.2lf\n",studentList[i].grade[j]);
                }
                fprintf(f,"%.2lf\n",studentList[i].fgrade);
            
            }
        }
    }
    fclose(f);
}
void SetStudentData()
{
    int index = 0;
    int c;
    f = fopen("Record.txt", "r");
  
    if(f == NULL)
    {
        printf("The file is not existing...");
    }
    else
    {
        while ((c = fgetc(f)) != EOF)
        {
            ungetc(c, f);
            fscanf(f,"%s",studentList[index].id);
            fscanf(f,"%s",studentList[index].name);
            fscanf(f,"%s",studentList[index].course);
            fscanf(f,"%d",&studentList[index].year);
            for(int i = 0; i < 3; i++)
            {
                fscanf(f,"%lf",&studentList[index].score[i]);
                fscanf(f,"%lf",&studentList[index].total[i]);
                fscanf(f,"%lf",&studentList[index].grade[i]);
            }
            fscanf(f,"%lf",&studentList[index].fgrade);
            index++;
            sizeOfArray++;
        }
    }
    fclose(f);
}
void ShowList()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        printf("ID Number:         %s\n", studentList[i].id);
        printf("Name:              %s\n", studentList[i].name);
        printf("Course:            %s\n", studentList[i].course);
        printf("Year Level:        %d\n", studentList[i].year);
              
        for(int a = 0; a < 3; a++)
        {
            printf("Score [%d]:         %.2f\n", a, studentList[i].score[a]);
            printf("Total Score [%d]:   %.2f\n", a, studentList[i].total[a]);
            printf("Grade [%d]:         %.2f\n", a, studentList[i].grade[a]);
        }
              
        printf("Final Grade:       %.2f\n\n", studentList[i].fgrade);
    }
  
    cin.get();
    cin.get();
}
void ShowStudentInfo(Student student)
{
    printf("ID Number:         %s\n", student.id);
    printf("Name:              %s\n", student.name);
    printf("Course:            %s\n", student.course);
    printf("Year Level:        %d\n", student.year);
              
    for(int a = 0; a < 3; a++)
    {
        printf("Score [%d]:         %.2f\n", a, student.score[a]);
        printf("Total Score [%d]:   %.2f\n", a, student.total[a]);
        printf("Grade [%d]:         %.2f\n", a, student.grade[a]);
    }
              
    printf("Final Grade:       %.2f\n\n", student.fgrade);
}
Salamat sa code paps pero dpt di kami gagamit ng iostream sa c lng dpt paps pero subrang galing mo paps nakuha ko na kasi yung code pero salamat parin d2
 
Salamat sa code paps pero dpt di kami gagamit ng iostream sa c lng dpt paps pero subrang galing mo paps nakuha ko na kasi yung code pero salamat parin d2
Di palitan mo. Haha! Ibang compiler kasi gamit ko hindi ko mapagana yung System("Pause") Palitan mo lang yung cin.get() nun tapos tanggalin mo yung iostream sa header.
 
Di palitan mo. Haha! Ibang compiler kasi gamit ko hindi ko mapagana yung System("Pause") Palitan mo lang yung cin.get() nun tapos tanggalin mo yung iostream sa header.

Palagay ko Turbo C compiler pa rin gamit nila. Yong pang 16 bit era pa. May mga non ISO standard builtin library at functions kasi yon.

Anak ng tipaklong na education system. Outdated tools pa rin ginagamit. Ang dami na ng updated open source alternatives. Yong Borland C o Turbo C compiler toolset pa rin ginagamit.
 
Palagay ko Turbo C compiler pa rin gamit nila. Yong pang 16 bit era pa. May mga non ISO standard builtin library at functions kasi yon.

Anak ng tipaklong na education system. Outdated tools pa rin ginagamit. Ang dami na ng updated open source alternatives. Yong Borland C o Turbo C compiler toolset pa rin ginagamit.
Bakit kaya may mga ganun na prof no? Kailangan makaluma gusto ipagamit. Hahaha!
 
Ito yung code.. Sabi ko sayo mas madali ang modification kapag nasa array eh. Ayaw mo maniwala.
Pag-aralan mo. Magtanong ka kung may hindi ka maintindihan.
Pag-aralan mo rin yung mga naming convention ng variables, paano ninaname ang mga function.
Tingnan mo kung paano ako mag-name at paano ka magname.
Ang pangit mo magname ng function. Wag mo pasakitin ulo ng professor mo.

Code:
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

struct Student
{
    char id[20];
    char name[256];
    char course[256];
    int year;
    double score[3];
    double total[3];
    double grade[3];
    double fgrade;
};

Student studentList[100];

const int SIZE = 100;
int sizeOfArray = 0;
FILE *f;

void AddStudent();
void UpdateStudent();
void DeleteStudent();
void SearchStudent();
void ShowStudentHighestToLowest();
void ShowStudentLowesToHighest();
void ShowStudentInfo(Student student);
void SaveToFile();
void SetStudentData();
void ShowList();

int main()
{
    char pick;
    int task;
  
    SetStudentData();
  
    while(true)
    {
        system("CLEAR"); 
        printf("=========================================================");
        printf("\n=================Student Records MENU====================");
        printf("\n=========================================================");
        printf("\n# [1]Input New Student Record\t\t\t\t#");
        printf("\n# [2]Modify Student Record\t\t\t\t#");
        printf("\n# [3]Delete Student Record\t\t\t\t#");
        printf("\n# [4]Show All Existing Student Record\t\t\t#");
        printf("\n# [5]Show the information of an specific Student\t#");
        printf("\n# [6]Display Student Record[Best To Worst Final Grade]\t#");
        printf("\n# [7]Display Student Record[Worst To Best Final Grade]\t#");
        printf("\n# [8]Exit\t\t\t\t\t\t#");
        printf("\n=========================================================");
        printf("\nEnter Task: ");
        scanf("%d",&task);
        printf("=========================================================\n");

        switch(task)
        {
            case 1:
                AddStudent();
                SaveToFile();
                break;
            case 2:
                UpdateStudent();
                SaveToFile();
                break;
            case 3:
                DeleteStudent();
                SaveToFile();
                break;
            case 4:
                ShowList();
                break;
            case 5:
                SearchStudent();
                break;
            case 6:
                ShowStudentHighestToLowest();
                break;
            case 7:
                ShowStudentLowesToHighest ();
                break;
            case 8:
                printf("Thank You For Using The Student Database Program");
                return 0;
            default:
                printf("Invalid Option...\n");
                printf("============================================\n");
                cin.get();
                break;
        } 
    }
  
  
}


void AddStudent()
{
    int index = 0;
    for(int i = 0; i < SIZE; i++)
    {
        if(strcmp(studentList[i].name, "") == 0)
        {
            index = i;
            break;
        }
    }
  
    printf("Putting the student in index: %d", index);
    printf("\nEnter the data of the student\n");
    printf("ID Number: ");
    scanf("%s", studentList[index].id);
    printf("Name: ");
    scanf("%s", studentList[index].name);
    printf("Course: ");
    scanf("%s", studentList[index].course);
    printf("Year Level: ");
    scanf("%d", &studentList[index].year);
  
    for(int i = 0; i < 3; i++)
    {
        printf("Input Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].score[i]);
        printf("Input Total Score[%d]: ", i + 1);
        scanf("%lf", &studentList[index].total[i]);
        studentList[index].grade[i] = 5 - (4 * (studentList[index].score[i]/studentList[index].total[i]));
    }
    studentList[index].fgrade = studentList[index].grade[0] * 0.2 + studentList[index].grade[1]
            * 0.3 + studentList[index].grade[2] * 0.5;
  
    printf("\n\nSuccessfully added the student");
    cin.get();
    cin.get();
  
}
void UpdateStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
  
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
              
                printf("\nEnter the newdata of the student\n");
                printf("ID Number: ");
                scanf("%s", studentList[i].id);
                printf("Name: ");
                scanf("%s", studentList[i].name);
                printf("Course: ");
                scanf("%s", studentList[i].course);
                printf("Year Level: ");
                scanf("%d", &studentList[i].year);

                for(int j = 0; j < 3; j++)
                {
                    printf("Input Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].score[j]);
                    printf("Input Total Score[%d]: ", j + 1);
                    scanf("%lf", &studentList[i].total[j]);
                    studentList[i].grade[j] = 5 - (4 * (studentList[i].score[j]/studentList[i].total[j]));
                }
                studentList[i].fgrade = studentList[i].grade[0] * 0.2 + studentList[i].grade[1]
                        * 0.3 + studentList[i].grade[2] * 0.5;
              
                printf("\n\nSuccessfully updated the student");
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
  
}
void DeleteStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
 
    while(true)
    {
        printf("\nEnter the ID number of the student you want to delete: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                printf("Found!\n");
                ShowStudentInfo(studentList[i]);
              
                printf("\nYou really want to delete this data? [Y/N]:");
                scanf("%s", input);
                if(strcmp(input, "Y") == 0)
                {
                    for (int j = i; j < sizeOfArray - 1; j++)
                    {
                        studentList[j] = studentList[j+1];
                    }
                    printf("\n\nSuccessfully deleted the student");
                    sizeOfArray--;
                    cin.get();
                    cin.get();
                    break;
                }
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void SearchStudent()
{
    char searchID[20];
    bool found = false;
    char input[1];
  
    while(true)
    {
        printf("\nEnter the ID number of the student: ");
        scanf("%s", searchID);

        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].id, searchID) == 0)
            {
                found = true;
                ShowStudentInfo(studentList[i]);
                cin.get();
                cin.get();
            }
        }

        if(!found)
        {
            printf("Student not found, try again? [Y/N]: ");
            scanf("%s", input);
            if(strcmp(input, "Y") != 0)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}
void ShowStudentHighestToLowest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade < studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
  
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void ShowStudentLowesToHighest()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        for(int j = 0; j < sizeOfArray - 1; j++)
        {
            if(studentList[i].fgrade > studentList[j].fgrade)
            {
                Student temp = studentList[i];
                studentList[i] = studentList[j];
                studentList[j] = temp;
            }
        }
    }
  
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        ShowStudentInfo(studentList[i]);
    }
    cin.get();
    cin.get();
}
void SaveToFile()
{
    fclose(fopen("Record.txt", "w"));
    f = fopen("Record.txt", "w");

    if(f == NULL)
    {
        printf("Failed to open the file...");
    }
    else
    {
        for(int i = 0; i < sizeOfArray; i++)
        {
            if(strcmp(studentList[i].name, "") != 0)
            {
                fprintf(f, "%s", studentList[i].id);
                fprintf(f, "\n%s", studentList[i].name);
                fprintf(f, "\n%s", studentList[i].course);
                fprintf(f, "\n%d\n", studentList[i].year);
              
                for(int j = 0; j < 3; j++)
                {
                    fprintf(f,"%.2lf\n",studentList[i].score[j]);
                    fprintf(f,"%.2lf\n",studentList[i].total[j]);
                    fprintf(f,"%.2lf\n",studentList[i].grade[j]);
                }
                fprintf(f,"%.2lf\n",studentList[i].fgrade);
            
            }
        }
    }
    fclose(f);
}
void SetStudentData()
{
    int index = 0;
    int c;
    f = fopen("Record.txt", "r");
  
    if(f == NULL)
    {
        printf("The file is not existing...");
    }
    else
    {
        while ((c = fgetc(f)) != EOF)
        {
            ungetc(c, f);
            fscanf(f,"%s",studentList[index].id);
            fscanf(f,"%s",studentList[index].name);
            fscanf(f,"%s",studentList[index].course);
            fscanf(f,"%d",&studentList[index].year);
            for(int i = 0; i < 3; i++)
            {
                fscanf(f,"%lf",&studentList[index].score[i]);
                fscanf(f,"%lf",&studentList[index].total[i]);
                fscanf(f,"%lf",&studentList[index].grade[i]);
            }
            fscanf(f,"%lf",&studentList[index].fgrade);
            index++;
            sizeOfArray++;
        }
    }
    fclose(f);
}
void ShowList()
{
    for(int i = 0; i < sizeOfArray - 1; i++)
    {
        printf("ID Number:         %s\n", studentList[i].id);
        printf("Name:              %s\n", studentList[i].name);
        printf("Course:            %s\n", studentList[i].course);
        printf("Year Level:        %d\n", studentList[i].year);
              
        for(int a = 0; a < 3; a++)
        {
            printf("Score [%d]:         %.2f\n", a, studentList[i].score[a]);
            printf("Total Score [%d]:   %.2f\n", a, studentList[i].total[a]);
            printf("Grade [%d]:         %.2f\n", a, studentList[i].grade[a]);
        }
              
        printf("Final Grade:       %.2f\n\n", studentList[i].fgrade);
    }
  
    cin.get();
    cin.get();
}
void ShowStudentInfo(Student student)
{
    printf("ID Number:         %s\n", student.id);
    printf("Name:              %s\n", student.name);
    printf("Course:            %s\n", student.course);
    printf("Year Level:        %d\n", student.year);
              
    for(int a = 0; a < 3; a++)
    {
        printf("Score [%d]:         %.2f\n", a, student.score[a]);
        printf("Total Score [%d]:   %.2f\n", a, student.total[a]);
        printf("Grade [%d]:         %.2f\n", a, student.grade[a]);
    }
              
    printf("Final Grade:       %.2f\n\n", student.fgrade);
}
Ayaw mag save ng add sa code mo paps dami kaylangan palitan
 
Status
Not open for further replies.

About this Thread

  • 51
    Replies
  • 2K
    Views
  • 6
    Participants
Last reply from:
Jannelle345

Trending Topics

Online now

Members online
1,050
Guests online
598
Total visitors
1,648

Forum statistics

Threads
2,279,475
Posts
28,991,022
Members
1,225,937
Latest member
bunsambath
Back
Top