Hello. :)
This is my first post and it's unfortunately to ask for help.
The problem is as follows:
- The user types in how many names they wish to enter.
- An array must be able to hold this many names.
- The user then types the names.
- Any characters over the limit are ignored, and the rest of the string is copied into the array.
I have been trying for the past hour or so to get this working and any hints in the right direction would be excellent.
I've deleted so much code that I'm basically left with nothing working. Originally, I tried using calloc for the array.
Then I tried this:
char str[numChar];
printf("How many names will you be entering?\n");
scanf("%i", &numNames);
charArray = (char*) malloc(sizeof(str));//creates 2D array for names
I'm not sure if I've made any progress since I started. :(
Originally it looked as shown:
//char or char*??
charArray = (char) calloc(numNames, numChar);//creates 2D array
if(charArray == NULL)
{
printf("Program error.");
exit(0);
}
//get each name
c = 0;
for(i = 0; i < numNames; i++)
{
printf("\nEnter name %i: ", &i);
for()//until final character is reached
{
*charArray = fgetc(stdin);//get char from keyboard
c++;
}
if(i < numNames - 1)//if not on last name
charArray += numChar - c;//point to start of next name
else
charArray -= c;//move to start of last name
}
I would appreciate any help.