limiting printf output in scanf multi dimension character array help
I have been attempting to get this exercise right but don't know how to write the code to get desired results
Code:
#include <stdio.h>
int main(void)
{
int x=0;
char cName[5][21]= {0};
int iPick;
printf("Enter the name of 5 people\n\n");
for(x=0;x<5;x++){
printf("Enter name %d:", x+1);
scanf("%s", cName[x]);
printf("\n\nWould you like to add another name or view current names stored?\n");
printf("1)\tAdd another name\n");
printf("2)\tView names stored\n");
scanf("%d", &iPick);
switch(iPick){
case 1:
break;
case 2:
for(x=0;x<5;x++){
printf("\nName %d is %s\n",x+1, cName[x]);
}
}
}
}
/*Create a program that allows a user to enter up to five names
of friends. Use a two-dimensional array to store the friends’
names. After each name is entered, the user should have the
option to enter another name or print out a report that shows
each name entered thus far*/
My problem is when I select case 2 of the switch, printf displays 5 names instead of the actual amount entered up to the point of me selecting case 2. If I entered 2 names then select case 2 I will see 5 name results with 3 of them being blank. I'd like for the blank ones to not be displayed but don't know how to code it. I tried using an if statement and ! operator to display only the names that aren't null but got compiler errors.