'I need a user to enter a string and said string should be stored in a struct variable in C
I have to create an athlete management system. I am fairly new to C so pardon me being a noob and asking silly questions. So when I accept all the input for an athlete it prints the string components as a heart emoji. So changed it to use strcpy() but when I do this the program crashes after I input the name. so idk how else to store the user inputted name.
typedef struct athlete
{
char name[100];
int id;
int age;
}ath;
this is my struct
do
{
printf("Please enter the participant's name:\n");
scanf("%s",aname);
for ( i = 0; i < strlen(aname); i++ ){
if(isalpha(aname[i]) == 0){
printf("here");
printf("Name is invalid! Please re-enter the name\n");
j = 1;
}
else{
printf("Here");
a.name = strdup(aname);
j = 0;
}
}
} while(j==1);
this is my validation and user input section
so after the name is entered it crashes (none of the here print statements are executed so I really do not know what to do or if I missed a step along the way any help would be appreciated.
Solution 1:[1]
Try something like: do { printf("Please enter the participant's name:\n"); scanf("%99s", aname); if(aname[0] != 0) j = 0; strcpy(ath.name, aname); } while(j==1);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Top-Master |
