0
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 !

5
  • In answerme, you check *answer - but you don't assign any particular value to it. Your program exhibits undefined behavior, by way of accessing uninitialized variable. Note that the local variable named answer in answerme is completely separate from and unrelated to a local variable named answer in main. Commented Jan 8, 2017 at 5:01
  • Note that the semicolon after the last } of answerme() is superfluous. Commented Jan 8, 2017 at 5:16
  • Please write question in more readable way ,as it would encourage someone to read your question , if you just wrote ton of lines without proper formatting , it will only discourage the person to read whole of it. Commented Jan 8, 2017 at 5:53
  • C++ != C. You're not compiling both at once. Please do not randomly add tags just because they seem similar or start with the first letter. cat is not the same as car just because they both start with ca. Use only the tags that actually apply to your question. Tags have meaning and relevance here. Commented Jan 8, 2017 at 5:55
  • I wonder what happens to the memory allocated to answer in answerme() every time answerme() is called? Do a quick search on memory leak in C. If you allocate it, you are responsible for freeing it when it is no longer needed. (you don't need to dynamically allocate at all here) Commented Jan 8, 2017 at 8:23

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

How can I pass it through from main into "answerme" ? I know its something like : answerme(char *answer) <- in the decleration at the top of main and then later in the function call I say "answerme(&answer) but for some reason it doesnt work?
@HomamMalkawi: I have added an example which shows the mechanism.
Probably the simpler and best way, for this example, is to declare answer in the global scope (early in the source). main() and answerme() can simply access this global variable. Given that this variable is devoted to receive something from the user, which is a single entity, the global variable idea is also logical, instead of passing around the same value via parameters.
0
#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));

}

2 Comments

Using malloc(SIZE) when SIZE is 10 is just wrong; the code should use char answer[SIZE]; and avoid memory leaks and unchecked failed allocation problems.
Thanks for pointing out Jonathan. I corrected it. There were other mistakes also which I had overlooked. Now, it's all corrected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.