简体   繁体   中英

While loop causing many issues with program

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 ay/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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
void answerme();

int main() {

char answer[SIZE]="0";


printf ("\nWould you like to begin? [Y/N]");
scanf(" %s",answer);
if((strcmp(answer,"y")==0) || (strcmp(answer,"Y")==0))
{
    printf("Answer is y\n");
    printf("\nprogram starting now...");
    answerme();
}
else
{
    printf("Wrong input..exiting\n");
    exit(1);
}
return 0;
}

void answerme()
{                    
    char answer[SIZE]="0";
    do
    {
           printf("\nPlease enter [Y], or [N]");
           scanf(" %s",answer);
           printf("You entered %s\n",answer);
    }while((strncmp(answer,"y",1)!=0) && (strncmp(answer,"Y",1)!=0) && (strncmp(answer,"n",1)!=0) && (strncmp(answer,"N",1)!=0));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM