0

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');
KLee
  • 25
  • 6
  • `scanf` reads from `stdin`, so you probably want to use `sscanf` here: `tmp.id = scanf("%d", &tempInput);`, but keep in mind the order of parameters would be different. `tmp.firstName = malloc(sizeof(char) * strlen(tempInput) + 1);` isn't going to work because `tmp.firstName` is not a pointer. Skip that and just use `strcpy(tmp.firstName, tempInput);` You'll also want to read [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) since you probably don't want to keep the newline in the input. – Retired Ninja Apr 10 '22 at 02:52
  • char firstName[MAXNAME]; char lastName[MAXNAME]; char email[MAXEMAIL]; char course[MAXCR]; - this is static allocation. Note that in this case you don't have to malloc/calloc anything. `strcpy` is enough. Here's function that does what comment above mentioned: ` int bc_fgets(char* tab, int size, FILE* stream){ fgets(tab, size, stream); if (*((tab)+strlen(tab)-1) != '\n') { while (getchar() != '\n'); return 1; } else *((tab)+strlen(tab)-1) = '\0'; return 0; } ` It removes `'\n'` from the input or clears buffer if it's not there. – Jakub Bednarski Apr 10 '22 at 02:53
  • KLee, 1) Avoid mixing `scanf()` and `fgets()`. Consider replacing all `scanf()` calls. 2) check return values of input function calls. – chux - Reinstate Monica Apr 10 '22 at 05:34

0 Answers0