I have a for loop that searches my array which is full of structs and read from a file. I have created an int variable for the user's response since they will respond with a student id and it will iterate over the other 'Students.id' to find the matching int.
When I run the program, the response from the for loop is always 'item not found' even if I am using an existing student id?
The searchInput is an int and the Students[i].id is an int so they should be able to compare each other and since I used scanf there shouldn't be a trailing "\n". I don't get any errors just the wrong reponse from the for loop.
struct studentStruct
{
int id;
char firstName[MAXNAME];
char lastName[MAXNAME];
char email[MAXEMAIL];
char course[MAXCR];
int grade;
};
// variable for reposnse
int response;
struct Student Students[MAXC]; // array of struct
char responseStudent = ' ';
int searchInput;
int chars;
size_t count = 0; // counter is here so it is global I can can reach it
struct studentStruct students[MAXC]; // array of struct
if ((fptr = fopen("students.txt", "rb")) == NULL)
{
printf("Error! opening file to read");
exit(1);
}
do
{
printf("Please enter the Student ID to search for a record: \n");
scanf("%d", &searchInput);
for (size_t i = 0; i < count; i++)
{
if (searchInput == Students[i].id)
{
printf("Item found\n");
}
else
{
printf("Item not found\n");
}
}
printf("\nWould you enter a student? ('Y' for yes, 'N' for no)\n");
scanf("%s", &responseStudent);
responseStudent = toupper(responseStudent); // change to upper case incase the user puts lowercase
} while (responseStudent == 'Y');