I'm new to C language. My task is to Read and Print Struct array in a loop. I wanted to read and print using IF ELSE statement. But the program doesn't store the the previous memory I inputted. Please use printf, Scanf, and fgets. I couldn't figure out how to make the program print all the given input from the users.
#include <stdio.h>
#include <stdlib.h>
struct Data
{
char name[50];
};
void readData(struct Data *array);
void showData(struct Data *array);
int main()
{
int i, j;
int choice;
struct Data array[2];
printf(" 1) Read Data 2) Show Data");
do
{
printf(" \n Option: ");
scanf("%d", &choice);
if(choice==1)
{
readData(array);
}
else if(choice==2)
{
showData(array);
}
else
{
printf("unkown option! \n");
}
}
while(1);
}
void readData(struct Data *array)
{
int i;
for (i = 0 ; i < 2 ; i++)
{
printf("enter name: ");
fgets(array[i].name, sizeof(array[i].name), stdin);
}
}
void showData(struct Data *array)
{
int i;
for (i = 0 ; i < 2 ; i++)
{
printf("%s", array[i].name);
}
}
Below is my Output:
1) Read Data 2) Show Data
Option: 1
enter name: enter name: dd
Option: 1
enter name: enter name: ss
Option: 2
ss
Option:
Thanks, much appreciate!