Answer to Question #205281 in C for Lucy

Question #205281

Write a program that is able to store the record of 30 students using structure and filing in C language. The Data contain roll number (Numeric part only i.e. 007), Registration number (Sp03-BCE-007), Name, Father name, semester result (at least 4 courses marks) and CGPA. Program user will be able to add new input data (if data limit not exceeded already), edit selected data and delete selected data. Also write a function that enable program user to search database with help of either roll number or name. Once match occurs program will display all the relevant credentials (data). Your program main screen displays a menu that give all those options i.e. new, edit, delete, search etc.

You need to put all the checks to restrict user from illegal inputs. Once you input the data and successfully create data file, your program upon executing will able to open the same file without losing any data until user give command through proper instruction (Program menu).

also provides its flow chart and comment on it.


1
Expert's answer
2021-06-10T13:31:16-0400
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


//students using structure and filing in C language. The Data contain roll number (Numeric part only i.e. 007), 
//Registration number (Sp03-BCE-007), Name, Father name, semester result (at least 4 courses marks) and CGPA
struct student{
	char rollNumber[10];
	char registrationNumber[30];
	char name[30];
	char fatherName[30];
	int coursesMarks[4];
	float CGPA;
};
//This method allows to search by roll number or name
int getStudentIndexSearchByRollNumberOrName(struct student students[30],int totalStudents,char rollNumberNameTarget[]){
	int i;
	for(i=0;i<totalStudents;i++){
		if(strcmp(students[i].rollNumber,rollNumberNameTarget)==0 ||
			strcmp(students[i].name,rollNumberNameTarget)==0){
				return i;
		}
	}
	return -1;
}
//This method allows to save students values to the file using the comma delimiter
void saveStudentsToFile(struct student students[30],int totalStudents){
	FILE *fptr;
	int i;
	fptr = fopen("students.txt","w");
	for(i=0;i<totalStudents;i++){
		fprintf(fptr,"%s,%s,%s,%s,%d,%d,%d,%d,%.2f\n",students[i].rollNumber,students[i].registrationNumber,students[i].name,students[i].fatherName,
			students[i].coursesMarks[0],students[i].coursesMarks[1],students[i].coursesMarks[2],students[i].coursesMarks[3],students[i].CGPA);
	}
	fclose(fptr);
}


//This method allows to read students values from the file using the comma delimiter
void readStudentsFromFile(struct student students[30],int* totalStudents){
	FILE *fptr = fopen("students.txt", "r");
    char *token;
    int i;
	int size=0;
    if(fptr != NULL)
    {
        char line[100];
        while(fgets(line, sizeof(line), fptr) != NULL){
            token = strtok(line, ",");
			strcpy(students[size].rollNumber,token);
            token = strtok(NULL,",");
			strcpy(students[size].registrationNumber,token);
			token = strtok(NULL,",");
			strcpy(students[size].name,token);
			token = strtok(NULL,",");
			strcpy(students[size].fatherName,token);
			token = strtok(NULL,",");
			for(i=0;i<4;i++){
				students[size].coursesMarks[i]=atoi(token);
			}
			students[size].CGPA=atof(token);
			size++;
        }
        fclose(fptr);
    } 
	*totalStudents=size;
}
//The start point of the program
int main( void){
	struct student students[30];
	int totalStudents=0;
	int ch=-1;
	int i;
	int selectedStudents=-1;
	int coursesMark;
	int coursesMarkSum=0;
	int indexStudent;
	char rollNumberNameTarget[30];
	readStudentsFromFile(students,&totalStudents);


	while(ch!=6){
		//display main menu
		printf("1. Add a new student\n");
		printf("2. Display all student\n");
		printf("3. Modify a student\n");
		printf("4. Delete a student\n");
		printf("5. Search a student\n");
		printf("6. Exit\n");
		//read choice from the user
		printf("Your choice: ");
		scanf("%d",&ch);
		fflush(stdin);
		if(ch==1){//Add a new student
			//read information about the student from the user
			printf("Enter the student's roll number: ");
			gets(students[totalStudents].rollNumber);
			printf("Enter the student's registration number: ");
			gets(students[totalStudents].registrationNumber);
			printf("Enter the student's name: ");
			gets(students[totalStudents].name);
			printf("Enter the student's father name: ");
			gets(students[totalStudents].fatherName);
			for(i=0;i<4;i++){
				coursesMark=-1;
				while(coursesMark<0 || coursesMark>100){
					printf("Enter the student's courses mark %d: ",(i+1));
					scanf("%d",&coursesMark);
				}
				students[totalStudents].coursesMarks[i]=coursesMark;
				coursesMarkSum+=coursesMark;
			}
			students[totalStudents].CGPA=coursesMarkSum/4.0;
			totalStudents++;
			saveStudentsToFile(students,totalStudents);
			printf("\nA new student has been added.\n\n");
		}else if(ch==2){//Display all student
			if(totalStudents>0){
				printf("\n\n%-15s%-15s%-15s%-15s%-10s%-10s%-10s%-10s%-10s\n","Roll Number","Reg. Number","Name","Father Name","Mark 1","Mark 2","Mark 3","Mark 4","CGPA");
				for(i=0;i<totalStudents;i++){
					printf("%-15s%-15s%-15s%-15s%-10d%-10d%-10d%-10d%-10.2f\n",students[i].rollNumber,students[i].registrationNumber,students[i].name,students[i].fatherName,
						students[i].coursesMarks[0],students[i].coursesMarks[1],students[i].coursesMarks[2],students[i].coursesMarks[3],students[i].CGPA);
				}
			}else{
				printf("\nThe array of students is empty.\n\n");
			}
		}else if(ch==3){//Modify a student
			if(totalStudents>0){
				printf("Enter the student's roll number or name to edit: ");
				gets(rollNumberNameTarget);
				indexStudent=getStudentIndexSearchByRollNumberOrName(students,totalStudents,rollNumberNameTarget);
				if(indexStudent!=-1){
					printf("Enter a new student's roll number: ");
					gets(students[indexStudent].rollNumber);
					printf("Enter a new student's registration number: ");
					gets(students[indexStudent].registrationNumber);
					printf("Enter a new student's name: ");
					gets(students[indexStudent].name);
					printf("Enter a new student's father name: ");
					gets(students[indexStudent].fatherName);
					for(i=0;i<4;i++){
						coursesMark=-1;
						while(coursesMark<0 || coursesMark>100){
							printf("Enter a new student's courses mark %d: ",(i+1));
							scanf("%d",&coursesMark);
						}
						students[indexStudent].coursesMarks[i]=coursesMark;
						coursesMarkSum+=coursesMark;
					}
					students[totalStudents].CGPA=coursesMarkSum/4.0;
					saveStudentsToFile(students,totalStudents);
					printf("\nA new student has been updated.\n\n");
				}else{
					printf("\nThe student does not exist.\n\n");
				}
			}else{
				printf("\nThe array of students is empty.\n\n");
			}
		}else if(ch==4){//Delete a student
			if(totalStudents>0){
				printf("Enter the student's roll number or name to delete: ");
				gets(rollNumberNameTarget);
				indexStudent=getStudentIndexSearchByRollNumberOrName(students,totalStudents,rollNumberNameTarget);
				if(indexStudent!=-1){
					for(i=indexStudent;i<totalStudents-1;i++){
						students[i]=students[i+1];
					}
					totalStudents--;
					saveStudentsToFile(students,totalStudents);
					printf("\nThe student has been deleted.\n\n");
				}else{
					printf("\nThe student does not exist.\n\n");
				}
			}else{
				printf("\nThe array of students is empty.\n\n");
			}
		}else if(ch==5){//Search a student
			if(totalStudents>0){
				printf("Enter the student's roll number or name to search: ");
				gets(rollNumberNameTarget);
				indexStudent=getStudentIndexSearchByRollNumberOrName(students,totalStudents,rollNumberNameTarget);
				if(indexStudent!=-1){
					printf("\n\n%-15s%-15s%-15s%-15s%-10s%-10s%-10s%-10s%-10s\n","Roll Number","Reg. Number","Name","Father Name","Mark 1","Mark 2","Mark 3","Mark 4","CGPA");
					printf("%-15s%-15s%-15s%-15s%-10d%-10d%-10d%-10d%-10.2f\n",students[indexStudent].rollNumber,students[indexStudent].registrationNumber,
						students[indexStudent].name,students[indexStudent].fatherName,students[indexStudent].coursesMarks[0],students[indexStudent].coursesMarks[1],
						students[indexStudent].coursesMarks[2],students[indexStudent].coursesMarks[3],students[indexStudent].CGPA);
				}else{
					printf("\nThe student does not exist.\n\n");
				}
			}else{
				printf("\nThe array of students is empty.\n\n");
			}
		}else if(ch==6){
			//exit
		}else{
			printf("\nWrong menu item.\n\n");
		}
	}
	return 0;
}








flow chart:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS