Write a proper C program that makes use of a function to accept user inputs and
ID: 3822365 • Letter: W
Question
Write a proper C program that makes use of a function to accept user inputs and updates members of a structure. The structure should contain three members: LastName,Gender and Age. LastName will not exceed 30 characters.
The structure variable should be created in the main function. The function should be called to populate the structure variable. The assigned values of the members should be displayed in the main function.
The input and output should work for any number of records. However, for testing purpose, make this work for two structure variables.
Explanation / Answer
#include <stdio.h>
struct Person
{
char lastName[50];
char gender;
int age;
};
void input(struct Person *person)
{
printf("Enter lastname: ");
scanf("%s", (person->lastName));
printf("Gender (M/F): ");
scanf(" %c", &(person->gender));
printf("Enter age: ");
scanf("%d", &(person->age));
}
int main()
{
struct Person person;
input(&person);
printf("Lastname: %s Gender: %c Age: %d ", person.lastName, person.gender, person.age);
struct Person person1;
input(&person1);
printf("Lastname: %s Gender: %c Age: %d ", person1.lastName, person1.gender, person1.age);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.