Help with C Programming Tutorial - 22 - or

+2 Mike Mait · January 6, 2015
So I was tinkering with responses with the code that we were using:

#include <stdio.h>

int main(int argc, char *argv[]) {

char answer; 

printf("Do you like bages? (y/n) \n");
scanf(" %c", &answer);

if( (answer == 'y') || ( answer == 'n')){

printf("You Are Sweet!");
}else {
printf("You Suck!");
}


return 0;
}


This is pretty much the same code, except I used different responses. What should happen is that if you type in a y or a n, it prints you are sweet, and it does. However, if you type something like tuna or another word or letter, it prints you suck, like it should. 

What my problem is, if i type a word like yes, yellow, yelp, or no, never, neandrathal, it works too because it starts with the y or n. How do I make it so it only works with just n or just y

Post a Reply

Replies

Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · January 8, 2015
oh my bad.. checked it out. thanks shouldn't have given the first code.
0 c student · January 8, 2015
The lack of error and warning flags for compilation is too damn high... :sick:
Also, be careful of buffer overflows with avner's code.  fgets() is probably a better input method.
0 Bernard Pyc0d333 Parah · January 8, 2015
just corrected the errors he had in his code, note that those are not my codes ok.

In the first code, i showed him why he is getting the output he gets and then bearing in mind that he may not have learnt pointers yet, i gave him the second one to maybe suit someone who is not yet familiar with pointers.

so there is nothing tricky for him as long as he wrote the codes himself.
your's should be an alternative method because they both perform the same operation at the end of the day.

moreover i think his code is far easier for a beginner to understand than what you just gave him.
what if the person has not studied arrays yet? you should consider the person's level of coding before giving answers.

So try as much as possible not to deviate from the persons code.


and please remember to place your codes in a code tag.
0 avner turgeman · January 7, 2015
Hello Mike

Bernard answers are tricky and not recommended. 
If you have more local variables inside your function it might cause data corruption.
I suggest you use a sting instead of char and use strcmp() instead of char compare.
It will check the entire answer string instead of the first character.
The only difference is that you must hit <Enter>  at the end of entered answer.

i.e.:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
char answer[10]; /* array size (10) should be expected max length */

   printf("Do you like badges? (y/n) \n");
   scanf(" %s", answer);   /* Scan String */

   if ((strcmp(answer,"y") == 0) || (strcmp(answer,"n") == 0))   /* Compare string rather than char */
        printf("You Are Sweet!");
   else
         printf("You Suck!");

   return 0;
}

Have a good luck!
0 Bernard Pyc0d333 Parah · January 7, 2015
or you can simply remove the ampersand and still run your code thus:

#include <stdio.h>
int main(int argc, char *argv[])
 {

 char answer;

printf("Do you like badges? (y/n) \n");
scanf(" %c", answer);
  if((answer =='y')||(answer == 'n'))
          {

                printf("You Are Sweet!");
          }
     else

      printf("You Suck!");


return 0;
}

0 Bernard Pyc0d333 Parah · January 7, 2015

#include <stdio.h>
int main(int argc, char *argv[])
 {

 char *answer; //making this a pointer since you used an ampersand for your answer.

printf("Do you like badges? (y/n) \n");
scanf(" %c", &answer); //&answer is y you should make your variable a pointer

  if((answer =='y')||(answer == 'n'))
          {

                printf("You Are Sweet!");
          }
     else

      printf("You Suck!");


return 0;
}


done bro.
  • 1

C

107,081 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator