While the loop causes many problems with the program

advertisements
void answerme();

int main() {

 char *answer = malloc (MAX_NAME_SZ);
 ....

printf ("\nWould you like to begin? [Y/N]");
fgets (answer, MAX_NAME_SZ, stdin);

answerme();

if(*answer == 'y' || *answer == 'Y'){
getinfo();
printf("\nprogram starting now...");
}

else if(*answer == 'n' || *answer == 'N'){
printf("\nThank you, program will close now....");
return 0;
}
...
}                 //end of main

void answerme(){
 char *answer = malloc (MAX_NAME_SZ);
 while(*answer !='n' && *answer != 'N' && *answer != 'y' && *answer != 'Y'){
    printf("\nPlease enter [Y], or [N]");
    fgets (answer, MAX_NAME_SZ, stdin);
 }

};

What the point of this while loop or the whole function is that for it to check if the user has answered the question with a y/n rather than another random key. I want this while loop to continue asking the user for a Y/N input until the user inputs it. However for some reason when this program is run, the first step asks you if you would like to begin the program, and if you do answer Y, it will for some reason tell you "please enter Y or N" even though you did enter the right answer, and then when you do enter for example "n" or even any other random letter it will still let you through. So it seems like it registers the input but for some reason it still asks runs the while loop instead of skipping to the if(answer == Y) or the if(answer ==N).

Does anyone know what could be the reason this is happening?

Also once the user says "Y" and begins the program there will be a message asking the user to input certain information and this information gets stored into a structure which I created (not shown in the code), however with this while loop, this somehow gets skipped. If I take off this while loop, the whole program works fine, but of course the user will be able to skip through steps of the program without strictly inputing what I've asked of him.

If there's any better alternative way of restricting the user into only inputing what I've asked, please do enlighten me on that as this has been causing me issues and headaches for the past 3 days. Thank you !


The problem is that you set a variable *answer in the function and there is another one in the main program. However, it looks like they are expected to be the same variable.

To fix this, declare only one and share it between the two functions. Do that by declaring it outside any function, or pass it from main to the subfunction. Note that it should be malloc() only once.

Example of the parameter passing technique is:

void answerme (char *answer)
{
    while (*answer !='n'  &&  *answer != 'N'  &&
           *answer != 'y' && *answer != 'Y')
    {
        printf ("\nPlease enter [Y], or [N]");
        fgets (answer, MAX_NAME_SZ, stdin);
    }
}

int main()
{

    char *answer = malloc (MAX_NAME_SZ);
    ....

    printf ("\nWould you like to begin? [Y/N]");
    fgets (answer, MAX_NAME_SZ, stdin);

    answerme(answer);

    if (*answer == 'y' || *answer == 'Y')
    {
         getinfo();
         printf("program starting now...\n");
    }
    else
    if (*answer == 'n' || *answer == 'N')
    {
        printf("Thank you, program will close now.\n");
        return 0;
    }
    ...
}                 //end of main