[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

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 ?
0
andyw27
Asked:
andyw27
  • 2
1 Solution
 
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 OlivaresCommented:
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
 
andyw27Author Commented:

Thanks that done the trick.
0
 
van_dyCommented:
oh .. lol its "y" not 'y' :(
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now