I'm trying to write some code that will allow me to read in a character array from the user. I managed to get something that will read everything (up to the specified length), including spaces. Since the code for that one function is slightly long, I tried to put it into a function. My problem is that the code inside the function doesn't read anything after the first space is put in.
Here's the working code that I have:
*********************************************
#include <stdio.h>
int main()
{
char cString[15] = "\0";
printf("Please type a name:");
int nCurrChar = 0;
for(int i = 0; (i<sizeof(cString)) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
cString[i] = (char) nCurrChar;
cString[i] = '\0';
printf("%s\n", cString);
return 0;
}
*********************************************
Here's the code that I have that isn't working:
*********************************************
#include <stdio.h>
#include <string.h>
void ReadString(char string[]);
int main()
{
printf("Please type a name:");
char cName[15] = "\0";
ReadString(cName);
printf("%s", cName);
return 0;
}
void ReadString(char cString[])
{
int nCurrChar = 0;
for(int i = 0; (i<sizeof(cString)) && ((nCurrChar = getchar()) !=EOF) && (nCurrChar != '\n'); i++)
cString[i] = (char) nCurrChar;
cString[i] = '\0';
}
*********************************************
I copied and pasted the code that I put into the function directly from what I had above it, so I'm not sure what's going on. Any help would be greatly appreciated. :-)