I am trying to write a program that will input a bunch of names into an array of structures under the .name heading. Currently I have what is below to get it to work. But it's not very flexible. You have to input both a first and a last name before it will go on. Is there any command similar to cin.getline for C++ that will work in this instance? Or a way to leave last as null if a second ("last") name isnt input that time around?

for (count_s=0; count_s < num_stu; count_s++){
        printf("\nEnter name of student #%i: ",count_s+1);
        scanf("%s %s", &first, &last);
        strcpy(student[count_s].name, first)
        strcat(student[count_s].name, " ");
        strcat(student[count_s].name, last);
        
    }

Recommended Answers

All 2 Replies

I am trying to write a program that will input a bunch of names into an array of structures under the .name heading. Currently I have what is below to get it to work. But it's not very flexible. You have to input both a first and a last name before it will go on. Is there any command similar to cin.getline for C++ that will work in this instance? Or a way to leave last as null if a second ("last") name isnt input that time around?

for (count_s=0; count_s < num_stu; count_s++){
        printf("\nEnter name of student #%i: ",count_s+1);
        scanf("%s %s", &first, &last);
        strcpy(student[count_s].name, first)
        strcat(student[count_s].name, " ");
        strcat(student[count_s].name, last);
        
    }

Do you mean like C's getline function?

fgets( student[count_s].name, sizeof (student[count_s].name), stdin );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.