0

I am trying to compile the example codes from the shorts with Doug Lloyd. I was putting together the switch.c example, and there seems to be a problem with the c of switch (c). Here is what I have.

include < cs50.h>  
include < stdio.h>  

int main(void)  
{  
switch(c)   
    {  
    case 'Y':  
    case 'y':  
        printf("yes\n");  
        break;  
    case 'N':  
    case 'n':  
        printf("no\n");  
        break;  
    }  
}  

The error is error: use of undeclared identifier 'c' and I'm not quite sure what to do about it. I see that I did not declare anything for or about c, but at this point I am not exactly sure how to, as Doug did not do that in his video on conditions. Thanks!

1

Simply put, c doesn't exist because it was never declared, and no value has been assigned to it. I could explain in detail, but the following site has already done it very nicely, including an example that is very similar to your code with all the missing pieces. ;-)

https://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • I fixed it! But I don't know if I fixed it correctly. On the line above switch(c), i put char c = get_char(""); , so that c had a job persay, and that seems to have done the trick. Thanks for your response! – Kelsey M. Hill Jun 24 '18 at 22:58
  • yeah, that'll work. It's the correct usage of get_char and a good declaration. – Cliff B Jun 24 '18 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .