Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- void read_file(void);
- void add_student(void);
- void search_student_id(void);
- void delete_student_info(void);
- void edit_student_info(void);
- void display_info(void);
- void display_all_id(void);
- void save(void);
- struct students {
- int student_id;
- float score ;
- };
- struct students info[100];
- int i;
- void main (){
- int choice ;
- while (1){
- printf("\t------MENU------\n\n");
- printf("1.Read the student info\n");
- printf("2.Add a student\n");
- printf("3.Search based on students ID\n");
- printf("4.Delete a students basic info by ID\n");
- printf("5.Edit a students basic info\n");
- printf("6.Display students info\n");
- printf("7.Display all existing students ID\n");
- printf("8.Save \n");
- printf("\n\nEnter your choice\n");
- scanf("%d",&choice);
- switch (choice ){
- case 1 : read_file () ;
- break;
- case 2 : add_student(); ;
- break;
- case 3 : search_student_id() ;
- break;
- case 4 : //delete_student_info() ;
- break;
- case 5 : edit_student_info() ;
- break;
- case 6 : display_info() ;
- break;
- case 7 : display_all_id() ;
- break;
- case 8 : save() ;
- break;
- case 9 :
- //exit(0);
- default :
- printf(" \n Wrong Input\n");
- }
- }
- }
- void read_file( ){
- int NUM=5;
- FILE*fp;
- errno_t err= fopen_s(&fp,"studentinfo.txt","r");
- if ( err !=0 ) {
- printf("\nFile is not opened\n");
- }else{
- for( i = 0; i < NUM ; i++){
- fscanf_s(fp,"%d",&info[i].student_id);
- fscanf_s(fp,"%f\n",&info[i].score);
- printf("%d\t",info[i].student_id);
- printf("%.2f\n",info[i].score);
- }
- fclose(fp);
- }
- }
- void add_student( ){
- int j=i ;
- printf("\nEnter student id\n");
- scanf("%d",&info[j].student_id);
- printf("\nEnter students score\n");
- scanf("%f",&info[j].score);
- j++;
- i=j;
- }
- void display_all_id( ) {
- int l ;
- for(l=0;l<i;l++){
- printf("%d\t",info[l].student_id);
- printf("%.2f\n",info[l].score);
- }
- }
- void display_info() {
- int l,id , p ;
- printf("\n Enter ID : ");
- scanf("%d", &id);
- for ( l= 0 ; l< i ; l++ ){
- if ( id == info[l].student_id){
- p=l;
- break;
- }
- }
- printf("ID: %d score: %.2f", info[p].student_id , info[p].score);
- }
- void search_student_id( ) {
- int l,id , p = 0;
- printf("\n Enter ID : ");
- scanf("%d", &id);
- for ( l= 0 ; l< i ; l++ ){
- if ( id == info[l].student_id){
- p=l;
- printf("ID: %d score: %.2f", info[p].student_id , info[p].score);
- break;
- }else if ( id != info[l].student_id){
- printf("\nNot Found!!\n\n");
- break;
- }
- }
- }
- void save( ) {
- int e ;
- FILE*fp;
- errno_t err= fopen_s(&fp,"studentinfo.txt","w");
- if ( err !=0 ) {
- printf("\nFile is not opened\n");
- }else{
- for( e = 0; e <=i ; e++){
- fprintf(fp,"%d ",info[e].student_id);
- fprintf(fp,"%.2f\n",info[e].score);
- }
- fclose(fp);
- }
- }
- void edit_student_info( ) {
- int p=0, l , id;
- printf("\n Enter ID : ");
- scanf("%d", &id);
- for ( l= 0 ; l< i ; l++ ){
- if ( id == info[l].student_id){
- p=l;
- printf("\nedit id : ");
- scanf("%d" , &info[p].student_id);
- printf("\nedit score : ");
- scanf("%f" , &info[p].score);
- break;
- }else if ( id != info[l].student_id){
- printf("\nNot Found!!\n\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement