-1

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() .

  • There are 7,224 questions tagged ```scanf```. One would think that to be abundant. I do not know what your teacher is trying to accomplish with this. ```gets``` is a dangerous function and shouldn't be used. It was already outdated in the 90's, and removed from the language in ```C11```. It is no longer a part of ```C```. ```scanf``` is no better than ```gets``` when reading an array of characters without a specified limit. But as you're limited to these two, read ```scanf```'s man page, you'd find all the information you need there. You should also be checking the return value of ```scanf```. – Harith Dec 23 '22 at 16:18
  • 5
    If your instructor is requiring you to use `gets()` instead of `fgets()`, then you should quit that class. – Mark Benningfield Dec 23 '22 at 16:18
  • @Haris Thanks for my comment my friend, believe me, I have looked at many topics, but I will continue to look. This is my university teacher, I can't leave the class in my head. – Baran Kanat Dec 23 '22 at 19:40
  • Baran Kanat, `gets()` is no longer part of the C standard library since C11 (11+ years ago). Better to learn modern C. – chux - Reinstate Monica Dec 23 '22 at 20:35

2 Answers2

1

After much research, I solved the problem. Perhaps the compiler thinks that we have entered the value in another element for scanf() when we enter a blank value, or I understood it that way.

The resources below helped me a lot.

scanf(“%[^\n]s”, str) Vs gets(str) in C with Examples

What does scanf("%*[^\n]%*c") mean?

Algorithm working properly:

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: ");
        scanf(" %[^\n]", (people+i)->name_lastname);

        printf("job: ");
        scanf(" %[^\n]", (people+i)->name_lastname);
    }


    return 0;

}

Places I changed in my code: gets((people+i)->name_lastname); ==> scanf(" %[^\n]", (people+i)->name_lastname);

gets((people+i)->job); ==> scanf(" %[^\n]", (people+i)->job);

  • 1
    `scanf(" %[^\n]", (people+i)->name_lastname);` without a _width_ is bad practice. Better as `scanf(" %29[^\n]", (people+i)->name_lastname);`. Certainly your 2nd call `scanf(" %[^\n]", (people+i)->name_lastname);` is amiss to save the job in the name. – chux - Reinstate Monica Dec 24 '22 at 17:04
  • 1
    Cast not needed. Better to allocate to the referenced object than type. `people = (struct person*) malloc(number*sizeof(struct person));` --> `people = malloc(sizeof people[0] * number);` – chux - Reinstate Monica Dec 24 '22 at 17:05
  • Thanks for the warnings, I saw that the algorithm worked more efficiently after applying what you said. Thank you so much. – Baran Kanat Dec 25 '22 at 13:33
-1
typedef struct person {
char name_lastname[30];
char job[30];
} person;
char c;
...
fprintf(stdout,"print name and last_name:\n");
fscanf(stdout,"%s%[\n]%c",people[i].name_lastname,&c);
fprintf(stdout,"print occupation:\n");
fscanf(stdout,"%s%[\n]%c",people[i].job,&c);
....