How the hell would you, read in the first, middle, and last name of 150 people, keeping in mind that each name should be limited to 30 characters.
I have tried using %ns in a scanf, but it just keeps going anyway.
As of right now I am trying to figure out a way to do it in a multidimensional array, looking likeI am also currently using c = getchar(), and trying to figure out a way.Code:char names[150][3][30]
I will post my code, but it's of very little help since it just consists of about 3 practice programs that I am doing to try and understand what the hell to do. Had about 5 of them going, but just deleted a couple once I understood a bit more.
Any help would be greatly appreciated. I would like to specifically use either getchar() or scanf, because it can be done that way. Instead of just jumping to a more useful function, I'd like to learn how to understand these two first.
Thanks again.
Here's the ridiculous code:
Code:
/*#include <stdio.h>
int main ()
{
char names[5][5];
int i,j;
printf("Enter 3 names:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 5; j++)
names[i][j] = '#';
for(i=0;i<3;i++)
scanf("%4s",names[i]);
for(i = 0; i < 3; i++)
for(j = 0; j < 5; j++){
printf("names[%d][%d]: %c ", i, j, names[i][j]);
if ( (j + 1) % 4 == 0)
printf("\n\n");
}
for(i=0;i<3;i++)
printf("%s\n",names[i]);
return 0;
}
*/
/*
char name[150][3][30];
printf("Enter your first name, middle name, and last name.\n");
for (i = 0; i < 1; i++)
for(j = 0;
scanf("%5s", name, name, name);
printf("%s\n", name[0]);
return 0;
}
*/
/*
#include <stdio.h>
int main ()
{
char names[1][3][5];
int i,j,k;
printf("Enter your name:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
for (k = 0; k < 5; k++)
names[i][j][k] = '#';
for(i=0;i<1;i++)
scanf("%s",names);
for(i = 0; i < 1; i++)
for(j = 0; j < 3; j++)
for(k = 0; k < 5; k++)
{
printf("names[%d][%d][%d]: %c ", i, j, k, names[i][j][k]);
if ( (k + 1) % 4 == 0)
printf("\n\n");
}
for(i=0;i<1;i++)
printf("\n%s\n",names[i]);
return 0;
}
*/
#include <stdio.h>
int main ()
{
char names[2][3][30];//, c;
int i, j, k,c;
printf("Enter 2 full names\n");
i = 0;
j = 0;
k = 0;
do {
c = getchar();
if ( k == 29)
names[i][j][k] = '\0';
names[i][j][k++] = c;
if ((j != 0) && (j % 2 == 0)){
i++;
j = 0;
}
//j++;
printf("names [%d][%d][%d]: %c \n", i, j, k, names[i][j][k]);
}
while(c != '\n' && c != ' ');
names[i][j][k++] = c;
return 0;
}