I need help with my project homework The problem is, how can I enter a value with spaces using scanf() and gets()?
my code:
#include <stdio.h>
#include <stdlib.h>
struct person {
char *name_lastname[30];
char *job[30];
};
int main()
{
int number;
printf("How many names will you enter? ");
scanf("%d", &number);
struct person *people = (struct person*) malloc(number*sizeof(struct person));
for(int i = 0; i<number; i++)
{
printf("name and last name: ");
gets((people+i)->name_lastname);
printf("job: ");
gets((people+i)->job);
}
return 0;
}
I know there are similar answers here for this.
How do you allow spaces to be entered using scanf? "But we are not allowed to use
fgets()
in our assignment."
Or such uses solve my problem, I tried all of them.
scanf("%10[0-9a-zA-Z ]", &number);
scanf("%19[^\n]", &number);
As you can see, the functions I can use are very limited as it is a project assignment.
strlen()
, fgets()
I am not allowed to use such functions.
My algorithm malfunctions when I enter a blank value without using scanf() and gets() .