When the following function is called and run, if the initial "if" condition is met, the program runs as intended; repetitively. If the initial "if" condition is not met, the program proceeds to run the else statement, but gets stuck in an endless loop.
Why?
#include <stdio.h>
#include <string.h>
int num_func();
int main()
{
num_func();
return 0;
}
int num_func()
{
int num;
char yn[1];
printf("Please enter an integer value: ");
if (scanf("%d", &num) == 1)
{
printf("The value you entered is: %d. Is this correct? ", num);
scanf("%s", &yn);
if (strcmp(yn, "y") == 0) {
printf("Great! \n");
}
else if (strcmp(yn, "n") == 0) {
printf(":( \n");
}
else {
printf("Illegal Entry. \n");
}
}
else {
printf("You were told to put in a number!");
}
num_func();
}
I am also interested in finding out how to make num and yn[1] global variables so that num_func() can access them w/o having to allocate memory each run. If you could explain that, I would be grateful.