Hello everyone,
I've been having a problem with some of my scanf() functions being skipped after I input a string with a space in between (e.g Jack Daniels). I've been trying to fix it, but it has been driving me crazy.
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int studentNumber;
int phoneNumber;
char* studentName;
};
int main()
{
struct Student *list;
int numOfStudents;
int x;
int studentCounter = 1;
printf("\nPlease enter the number of students you would like to input: ");
scanf("%d", &numOfStudents);
list = malloc(sizeof(list) * numOfStudents);
for(x = 0; x < numOfStudents; x++)
{
printf("\nPlease enter name for student #%d: ", studentCounter);
scanf("%s", &list[x].studentName);
printf("Please enter student number for student #%d: ", studentCounter);
scanf("%i", &list[x].studentNumber);
printf("Please enter phone number for student #%d: ", studentCounter);
scanf("%i", &list[x].phoneNumber);
studentCounter++;
}
}
In the code above, after I input the student name, the the 2 remaining scanf()s are disregarded and the loop iterates to 2, going to second student name. I tried flushing using fflush(stdout) but that did not fix anything either, lol. I searched around and found some suggestions that asked me to use scanf(” %[^\n]s”,a) instead of scanf("%s", a) but that did not work either. Any help would be much appreciated, as I think I am losing my mind to such a small hiccup/problem! :(