I, for the life of me, cannot be at peace with c strings and input/output.
For my program I simply enter a string and it gets processed in the following code: (tmpstring and ch are already defined)
For my incomning input, I write in terminal: echo "test" | ./program
int main(int argc, char *argv[]) {
char tmpstring[2048];
int ch;
int r;
int c;
fgets(tmpstring, sizeof tmpstring, stdin);
while((ch = fgetc(stdin))!= EOF && ch != '\n');
tmpstring[strlen(tmpstring)-1]='\0';
strncpy(opponent, tmpstring+1, strlen(tmpstring+1));
move();
Inside of move();
char buffer[2048]={0};
int r, c;
r=0; c=0;
printf("Your move (row column):");
if((fgets(buffer, sizeof buffer, stdin)==NULL) || ((sscanf(buffer,"%d %d", &r, &c))!=2)){
printf("Invalid input. Please insert two numbers separated by whitespace.\n");
exit(1);
}
//continues
Executing this goes straight into the invalid input without asking for more input. I've read all around about how you shouldn't clear stdin (and that it's impossible) but I really don't know what to do. I clearly tried to "dump" stdin with the second part of the while loop. I've changed the && after the first condition to an || in the while loop. No change. Overall, how can I ask for more input after already used fgets?
*edit: more code and separated the original while loop