Below is the complete code.
To compile & play with it online use link: https://www.programiz.com/online-compiler/3ENxYbYvXkt5Z
#include <stdio.h>
#include <string.h>
struct studentstruct {
int rollnum;
char stdname[50];
int stdmarks;
char stdgrade[3];
};
struct studentstruct students[100];
int student_count = 0;
int uschoice, a;
void addStudent();
void mainmenu();
int std_count = 0;
int b;
int nmb_std;
int roll_srch, find_roll,srch_choice;
char name_srch[50];
void addStudent(int index) {
if (student_count == 100) {
printf("Storage full! Cannot add more students.\n");
a = 1;
mainmenu();
} else {
b = 1;
printf("Enter Roll Number: ");
scanf("%d", &students[index].rollnum);
if (students[index].rollnum > 10000000) {
printf("Only include integers!");
}
printf("Enter Name: ");
scanf(" %[^\n]", students[index].stdname);
printf("Enter Marks (0-100): ");
scanf("%d", &students[index].stdmarks);
if (students[index].stdmarks > 100) {
printf("Marks more than 100!");
}
printf("Enter Grade: ");
scanf(" %2s", students[index].stdgrade);
student_count++;
mainmenu();
}
}
void showstudent(int index) {
if (student_count == 0) {
printf("No students to display.\n");
return;
}
printf("\n--- Student Record ---\n");
printf("Roll: %d\n", students[index].rollnum);
printf("Name: %s\n", students[index].stdname);
printf("Marks: %d\n", students[index].stdmarks);
printf("Grade: %s\n", students[index].stdgrade);
mainmenu();
}
void mainmenu() {
printf("<----MAIN MENU---->\n");
printf("1.Add record\n");
printf("2.Display record\n");
printf("3.Search record\n");
printf("4.Exit\n");
printf("Enter your choice:");
scanf("%d", &uschoice);
if (uschoice == 1) {
if (a == 1) {
student_count = 100;
}
if (b == 1) {
std_count++;
}
addStudent(std_count);
}
if (uschoice == 2) {
printf("Enter Name:");
scanf(" %[^\n]", name_srch);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].stdname, name_srch) == 0) {
showstudent(i);
found = 1;
}
}
if (found == 0) {
printf("Student with name %s not found.\n", name_srch);
mainmenu();
}
}
if (uschoice == 3) {
printf("[1]Search with Roll no\n[2]Search with name\nEnter your choice here:");
scanf("%d", &srch_choice);
if(srch_choice==1){
printf("Enter Roll no:");
scanf("%d", &roll_srch);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (students[i].rollnum == roll_srch) {
showstudent(i);
found = 1;
}
}
if (found == 0) {
printf("Student with roll %d not found.\n", roll_srch);
mainmenu();
}
} else {
printf("Enter Name:");
scanf(" %[^\n]", name_srch);
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].stdname, name_srch) == 0) {
showstudent(i);
found = 1;
}
}
if (found == 0) {
printf("Student with name %s not found.\n", name_srch);
mainmenu();
}
}
}
if (uschoice == 4) printf("Thanks for using my app!");
}
int main() {
mainmenu();
return 0;
}
