How should I do to be able to ask a yes or no question in C

Viewed 56

I'm trying to make my code to... I guess recognize if I am pressing "y" or "n", and and do stuff depending on which one is in the input.

I thought it would be this simple, but unfortunately it isn't.

How am I supposed to do it?

#include <stdio.h>

int main(void)
 {
    char y;

    printf("Single provider (y/n)? ");
    scanf(" %c", &y);

    if (y == '121' || y == '89')
    {
        printf("single");
    }
    else
    {
        printf("not single");
    }
 }
3 Answers

You are making a beautiful mix-up here: apparently have heard about ASCII codes and you want to apply them into your code. The idea is good, not the realisation:

Your code:

if (y == '121' || y == '89')

Correct code (without ASCII codes):

if (y == 'y' || y == 'Y')

Correct code (using ASCII codes):

if (y == 121 || y == 89) // ASCII_Code('Y')=89 and ASCII('y')=121

(Please don't remove the comment, it's very useful for readability)

Good luck

How should I do to be able to ask a yes or no question in C

There are 5 input sets to distinguish:

  1. "Y\n" or something similar: "y\n", "Yes\n", "YES\n" ...

  2. "N\n" or something similar: "n\n", "no\n", ...

  3. End-of-file occurs. scanf() returns EOF and feof(stdin)is true.

  4. Input error occurs. scanf() returns EOF and ferror(stdin)is true. (Rare)

  5. Something else. Usually a user mistake.

In general, best to avoid scanf()and use fgets().
Yet sticking with scanf():

Read in a line of input. Use a space to consume leading white-spaces including a prior line's '\n'.

unsigned char buf[80];
buf[0] = 0;
int retval = scanf(" %79[^\n]", buf);

Then disambiguate. Suggest case insensitivity. Perhaps allow just the first letter or the whole word.

if (retval == EOF) {
  if (feof(stdin) {
    puts("End-of-file");
  } else { // Input error expected
    puts("Input error");
  }
} else {
  for (unsigned char *s = buf; *s; s++) {
    *s = tolower(*s);
  }
  if (strcmp(buf, "y") == 0 || strcmp(buf, "yes") == 0) {
    puts("Yes");
  } else if (strcmp(buf, "n") == 0 || strcmp(buf, "no") == 0) {
    puts("No");
  } else {
    puts("Non- yes/no input");
  }
}

How about a helper function to handle Yes/No, True/False, ....?

// Return first character on match
// Return 0 on no match
// Return EOF on end-of-file or input error
int Get1of2(const char *prompt, const char *answer1, const char *answer2) {
  if (prompt) {
    fputs(prompt, stdout);
  }
  unsigned char buf[80];
  buf[0] = 0;
  int retval = scanf(" %79[^\n]", buf);
  if (retval == EOF) {
    return EOF;
  }
  for (unsigned char *s = buf; *s; s++) {
    *s = tolower(*s);
  }
  if ((buf[0] ==  answer1[0] && buf[1] == 0) || 
      strcmp(buf, answer1) == 0) {
    return buf[0];
  }
  if ((buf[0] ==  answer2[0] && buf[1] == 0) || 
      strcmp(buf, answer2) == 0) {
    return buf[0];
  }
  return 0;  // None of the above.
}

Sample calls:

int yn = Get1of2("Single provider (y/n)? ", "yes", "no");
int rb = Get1of2("What is your favorite color? (r/b)? ", "red", "blue");
int tf = Get1of2("Nineveh the capital of Assyria? (t/f)? ", "true", "false");
int main(void)
 {
    int y;

    printf("Single provider (y/n)? ");
    do
    {
        y = fgetc(stdin);
    }while(y != EOF && y != 'y' && y != 'n');

    if(y != EOF)
        if (y == 'y')
        {
            printf("single");
        }
        else
        {
            printf("not single");  
        }
 }
Related