I can't seem to get either function to do anything using this code. I'm 99% sure my formatting is correct, and I've tested the functions themselves several times. It seems to be only here that I'm getting any problems.
I apologize if this is a stupid question; I'll delete this if it is. I've also deleted/modified code extraneous to the issue, if the way this is organized seems strange.
Using fgets:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char name[50];
char yesno;
void sleep() {
printf("Try again? (y/n)\n" );
scanf("%s", &yesno);
if (yesno == 'y') {
puts("Name?");
fgets(name,50,stdin);
printf("Name is %s\n", name);
}
else {
printf("Null");
}
}
int main() {
puts("Begin? y/n");
scanf("%s", &yesno);
if (yesno == 'y') {
sleep();
}
}
The code compiles correctly, but when run, it leaves
Name?
Name is
once executed to that point.
Substituting scanf for fgets,
scanf("%[^\n]s", name);
and the same issue occurs. Strangely,
scanf("%s", name);
works, but obviously only scans until whitespace is reached.