• C

If then statement


How can I write the following programatically ?

Print "do you go to the cinema enter y for yes or no"

Then depending on your answer print either "yes you do" or "no you don't"

So far I have this (which does'nt work) as it always prints yes, regardless of what you input.

#include <stdio.h>

 main()

  {
            char cinema_visit;

             printf("Do you gp to the cinema ?\nEnter 'Y' for yes or 'N' for no: ");
            scanf("%c",&cinema_visit);
                  
                  if (cinema_visit = "y") {            
                        printf("You entered Yes\n");       }

                  else if (cinema_visit = "n") {            
                        printf("You entered No\n");      }
                        
                        
            
 }

Any clues ?
andyw27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
main()

  {
          char cinema_visit;

           printf("Do you gp to the cinema ?\nEnter 'Y' for yes or 'N' for no: ");
          scanf("%c",&cinema_visit);
               
               if (cinema_visit == "y") {                          //u were using asssignement, not the equality test operator
                    printf("You entered Yes\n");       }

               else if (cinema_visit == "n") {            
                    printf("You entered No\n");     }
                   
                   
         
 }
0
Jaime OlivaresSoftware ArchitectCommented:
There are two errors in you evaluations:
1st. (Solved by van_dy) You have to use the double equal (==) operator for comparisons
2nd. You are evaluating a character, not a string, so use single quotes for 'y' and 'n'

Correct answer is:

main()

  {
          char cinema_visit;

           printf("Do you gp to the cinema ?\nEnter 'Y' for yes or 'N' for no: ");
          scanf("%c",&cinema_visit);
               
               if (cinema_visit == 'y') {      /* use single quotes */
                    printf("You entered Yes\n");       }

               else if (cinema_visit == 'n') {               /* use single quotes */
                    printf("You entered No\n");     }
                   
                   
         
 }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andyw27Author Commented:

Thanks that done the trick.
0
van_dyCommented:
oh .. lol its "y" not 'y' :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.