Can I have some help with code in C?
Sure, just ask
@osanseviero I am having some trouble using linear search in this bit of code --- // FUNCTION TO SEARCH FOR STUDENT BY ROLL NUMBER void search_stud() { int id, i, found=0; printf("Enter student ID to search for: "); scanf("%d", &id); /* Linear search begins */ for ( i=0; i < num ; i++) { if( stu[stud_cnt].ID_no == id ) { found = 1; break; } } if ( found == 1) { printf("\n%d\t%s\t\t%s\t\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%c", stu[stud_cnt].ID_no, stu[stud_cnt].first_name, stu[stud_cnt].last_name, stu[stud_cnt].info_tech, stu[stud_cnt].comp_sci, stu[stud_cnt].info_sys, stu[stud_cnt].disc_math, stu[stud_cnt].stud_avg, stu[stud_cnt].avg_stat); } else { printf("Student record not found. Please enter a valid ID."); } }
For starters, where did num come from and what is it's value? Second, if you're searching through an array of stu, why aren't you checking: if( stu[ i ].ID_no == id ) Those are the two things that pop out to me.
Yep. I see the same problems
num is the number of students @rsmith6559 @osanseviero
It worked now that I changed stud_cnt to i :) Thank you so much!
I created another function to search for student by first name, using the procedure as I did in the last function to search for student by roll number..but now it's giving me problems // FUNCTION TO SEARCH FOR STUDENT BY NAME void search_studname() { int i, found=0; char f_name[15], l_name[15]; printf("Enter student name to search for: "); scanf("%s %s", &f_name, &l_name); //Linear search begins for ( i=0; i <= num ; i++) { if( (stu[i].first_name == f_name) && (stu[i].last_name == l_name) ) { found = 1; break; } } if ( found == 1) { printf("\n%d\t%s\t\t%s\t\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%c", stu[i].ID_no, stu[i].first_name, stu[i].last_name, stu[i].info_tech, stu[i].comp_sci, stu[i].info_sys, stu[i].disc_math, stu[i].stud_avg, stu[i].avg_stat); } else { printf("\nStudent record not found. Please enter a valid student name."); } }
Which problems?
It returns "Student record not found. Please enter a valid student name" Which is pretty odd as I followed the same steps in the previous function :S
Put a printf() statement after the scanf() to make sure that you have what you expect. Comparing strings for equality is, at best tricky. When scanf() assigns to the arrays, does it put the '\0' to delimit the strings? Is the capitalization the same? Characters are actually 8 bit integers, and the comparison is basically strlen() subtractions that all have to equal 0 to be equal.
Join our real-time social learning platform and learn together with your friends!