Read the following below and write a program in C using switch case.

QUESTION

1- create (do while loop) 

do … (while choice !=4) if the user enter 4 then the loop will stop. 

2- inside (do while loop) use switch statement 

The switch statement going to check user choice 

Case1: 

If user enter 1 we will call (add student function) inside case 1

Case2: 

If user enter 2 we will call (search about student function) inside case 2

Case 3: 

If user enter 3 we will call (print list function) inside case 3 

Default: 

If the user entered any number rather than (1,2,3,4) this message will appear (invalid choice try again)

############ 

How to Make add student function 

a) YOU MUST Make a structure to store student information. These information are (student name, student ID, student GPA). 

b) YOU MUST Make a file to store students’ structures 

c) YOU MUST search about the student using STUDENT ID only!

-d) If the program found the student, it will print All STUDENT INFORMATION that was stored inside the structure.

 

SUMMARY

A student structure is created which consists of student details like name, id and gpa as the members. Certain operations like adding a student detail, searching for a student and printing the list of student details are performed on the student info.

 

EXPLANATION

A student structure is first created with name, studID and gpa as the members. The function addStudent  takes in a file pointer in write mode, and adds a student structure data to the file. Another function named searchStudent is used to search the entire file for a particular id. A  function named printList is just lists out all the student details which were added to the file until then. All the above methods are checked in the main.

 

CODE

#include <stdio.h>
#include <stdlib.h>
struct student 
{
    char name[30];
    int ID;
    float gpa;
};
void addStudt(FILE *stud)
{
    struct student s;
    printf("Add a student info:\n");
    printf("Enter name: ");
    scanf("%s",s.name);
    printf("Enter student-id: ");
    scanf("%d",&s.ID);
    printf("Enter gpa: ");
    scanf("%f",&s.gpa);
    fprintf(stud,"%d %s %f\n",s.ID,s.name,s.gpa);
}
/* search for a student detail */
void searchStudt(FILE *stud)
{
    printf("Enter student-id to search: ");
    int id;
    scanf("%d",&id);
    int n;
    char str[100];
    while(fgets(str,100,stud)!=NULL)
    {
        n=((int)(str[0])-(int)('0'))*1000 + ((int)(str[1])-(int)('0'))*100;
        n=n+ ((int)(str[2])-(int)('0'))*10 + ((int)(str[3])-(int)('0'));
        printf("%d\n",n);
        if(n==id)
        {
            printf("The student info:\n");
            printf("%s\n\n",str);
            return;
        }
    }
    printf("Not found!!\n\n");
}
void printlist(FILE *stud)
{
    char str[100];
    printf("Students info: (ID, Name, GPA)\n");
    while(fgets(str,100,stud)!=NULL)
    printf("%s",str);
    printf("\n\n");
}
int main()
{
    int choice;
    FILE *stud;
    stud=fopen("student.txt","a");
    FILE *open;
    open=fopen("student.txt","r");
    do 
    {
        printf("Enter your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                addStudt(stud);
                printf("\n");
                break;
            }
            case 2:
            {
                searchStudt(open);
                break;
            }
            case 3:
            {
                printlist(open);
                break;
            }
            case 4:
            {
                printf("Thank you!!!\n\n");
                break;
            }
            default:
            printf("Invalid choice. Please try again!!\n\n");
        }
    }while(choice!=4);
}

 

OUTPUT

Enter your choice: 2

Enter student-id t search: 7678

7678

The student info ; 

7678  Zach  8.5

Enter your choice : 3

Student info : (ID, Name, GPA)

7656  Cathy  9.0

7634  Beth  8.9

Enter your choice: 1

Add a student info : 

Enter name: Monika

Enter student-id : 7675

Enter gpa: 8.41

Enter your choice: 4

Thankyou !!!

Enter your choice: 6

Invalid choice. Please try again !!

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *