i messed up with or statement i guess!

+1 imran LP · January 31, 2015
Okay ! so i was trying out tutorial 22 in codeblocks.the problem is whenever i enter something that starts from either y or n it the output is "good job you didn't mess up anything" for example if i enter "yeaah , yo , yasgag or naaaa, nope , nadsfasfasd" anything which starts from n or y its accepting it i mean it doesn't prompt me saying "keyboard much ??" so.. i really don't know where am going wrong . This is my code . let me know where  am  going wrong. 

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

int main()
  char answer;
  printf("do you like bagels? (y/n)\n");
  scanf(" %c", &answer);
     printf("good job u didnt mess  up anything");
    printf("keyboard much?");
  return 0;

Post a Reply


Oldest  Newest  Rating
+2 Steven vd_W · January 31, 2015
The statement only checks if the first letter is a 'n' or 'y', so you didn't really mess anything up 
but I'm not sure how to solve it though x).
+1 c student · January 31, 2015
observe the changes in your answer variable below:http://i.imgur.com/kY6KYxo.png
if you haven't guessed already, your variable answer is of char type and can only hold one char value.  when you call scanf() to read in a character, it will place the first character into your variable and essentially drop anything that follows.  feel free to have a browse through the scanf() man page.
if you want to include all of the following characters, you can declare a simple array for answer to hold in more characters and then str(n)cmp() it with (a) desired string(s).
0 imran LP · February 1, 2015
i get it ! well thanks that helped :happy:
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator