0

I want to start a program of Y and N Q&A.

#include <stdio.h> 
#include <stdlib.h>

int main(){
    char answer[256];
    do {
    print("\nDo you want to delete yourself of the record?\n");
    scanf("%s", answer);
    printf("%s", answer);
    }while(answer != "Y" || answer != "N")
;
    return 0;
}

As you can see, I declared a variable of type char of 256 elements, and then using scanf I recorded the user input and store it in answer. Then the loop will be keeping asking as long the user enters either an uppercase Y or N. The problem is that with this implementation the program keeps asking even if I enter a Y or N. Should I change the char declaration to a single character? I already tried this:

#include <stdio.h> 
#include <stdlib.h>

int main(){
    char answer;
    do {
    print("\nDo you want to delete yourself of the record?\n");
    scanf("%c", answer);
    printf("%c", answer);
    }while(answer != 'Y' || answer != 'N')
;
    return 0;
}

but I received a warning:

warning: format '%c' expects argument of type 'char *', but argument 2 has type int' [-Wformat=]
scanf("%c", answer);

Does anyone has a clarification for this problem?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
almrog
  • 141
  • 6

1 Answers1

3

This statement

Then the loop will be keeping asking as long the user enters either an uppercase Y or N.

means that the loop will stop its iterations when the user will enter either "Y" or "N" will not it?

This condition can be written like

strcmp( answer, "Y" ) == 0 || strcmp( answer, "N" ) == 0  

So negation of this condition (when the loop will continue its iterations) looks like

!( strcmp( answer, "Y" ) == 0 || strcmp( answer, "N" ) == 0 )

that is equivalent to

strcmp( answer, "Y" ) != 0 && strcmp( answer, "N" ) != 0  

Pay attention to that you have to compare strings (using the C string function strcmp) not pointers to their first characters that always will be unequal.

So the condition in the do-while loop in the first program should be

    do {
    print("\nDo you want to delete yourself of the record?\n");
    scanf("%s", answer);
    printf("%s", answer);
    }while( strcmp( answer, "Y" ) != 0 && strcmp( answer, "N" ) != 0 )
;

That is there should be used the logical AND operator.

In the second program you have to use a call of scanf like this

scanf( " %c", &answer);
       ^^^^   ^

and the same logical AND operator

    do {
    print("\nDo you want to delete yourself of the record?\n");
    scanf(" %c", &answer);
    printf("%c", answer);
    }while(answer != 'Y' && answer != 'N')
;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for sharing your answer. There is something I still cannot grasp though. In the condition after the while I put the logical OR operator because if answer is either "Y" or "N", then the program will stop. I cannot see why I have the use the logical AND, since I won't fulfill both condition when prompt to an answer. – almrog May 12 '20 at 21:06
  • 1
    @almrog "or" makes sense for the reasons to stop the loop. But the `... while()` is looking for reasons to continue the loop. So the answer cannot be Y AND the answer cannot be N. – chux - Reinstate Monica May 12 '20 at 21:10