I have a c program and I ask for the user input twice during my program. The first time the input function (my own) worked perfectly, but the second time the getchar() function seemingly doesn't run. I have looked into the problem myself and I found something related to trailing characters, but I'm still confused on my problem. I've tried to create a second function where the function keeps asking for input if the input given is a \n, and I've also tried using an extra getchar() function to get rid of the stored character in the input stream (I still don't fully understand this either). I've also tried other things, but they were involving my larger structures but not the getchar() function itself
My input function:
int getInput(char s[])
{
char c;
int i = 0;
while((c = getchar()) != EOF){
s[i] = c;
i++;
}
return i;
}
My main function:
main()
{
printf("Enter a string to convert: \n");
char s[1000];
int len = getInput(s);
char t[1000];
printf("Press 1 to convert from escape to real, or 2 to convert from real to escape: ");
char a[1];
getInput(a);
printf("%d\n", a[0]);
if(a[0] == '1') {
real(s, t, len);
} else if (a[0] == '2') {
escape(s, t, len);
} else {
printf("\nRestart the program and enter a 1 or a 2\n");
exit(0);
}
printf("\n%s\n", t);
}
Thank you