Thread: Changing a variable

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Changing a variable

    Hi. I'm trying to write a simple program where you need a key to open a door, and it requires you to get the key before opening the door. I'm getting some errors. This is what I have so far:


    Code:
    #include <stdio.h>
    #define KEY 0
    
    int a;
    
    int main() {
    
        char c;
        int key;
        key = 0;
    
        printf("You stand before a locked door. There is a key nearby.\n\n");
        printf("a. Take key.\nb. Open door.\n");
        
        c=getch();
        
        if(c == 'a')  {
            printf("\nYou take the key.\n"); 
        }
        key = 1; 
        
            
        if(c == 'b') && KEY = 0 {
            printf("\nYou do not have the key.\n"); 
            
        if(c == 'b') && KEY = 1 {
            printf("\nThe door unlocks with ease.\n"); 
            
        }
    }
    Sorry for asking such a basic question, but if anybody can help me with this code, I'll appreciate it.
    Last edited by matthew82; 02-05-2009 at 01:31 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    this compiles. try to find the differences.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int a;
    
    int main() {
    
        int c;
        int key;
        key = 0;
    
        printf("You stand before a locked door. There is a key nearby.\n\n");
        printf("a. Take key.\nb. Open door.\n");
    
        c=getch();
    
        if(c == 'a')  {
            printf("\nYou take the key.\n"); 
        }
        key = 1; 
    
    
        if(c == 'b' && key == 0) {
            printf("\nYou do not have the key.\n"); 
        }
    
        if(c == 'b' && key == 1) {
            printf("\nThe door unlocks with ease.\n"); 
    
        }
    }

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thanks so much. but I don't know how to have it display "You do not have the key." I thought it would if you pressed b before getting the key.


    EDIT: oh I found that by putting "key = 1;" inside the first If brace it will work.

    What I don't understand is if this is setting the variable so that the key acts like a key: you need it to open the door. I don't know how to program that.
    Last edited by matthew82; 02-05-2009 at 02:21 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the edit, of course it works, because you are saying that if the user pressed a, you print a message and set the variable key to 1. If you put it outside, key is set to 1 regardless if the user enters a or not.
    And I don't understand the second question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    I think I've cleared it up in this version...

    Code:
    #include <stdio.h>
    
    int a;
    int main()
    
    {
    
        int c; 
        int key; 
        key = 0; 
        
        printf("\n\nYou stand before a locked door. There is a key nearby.\n\n");
        printf("a. Take key.\nb. Open door.\n\n");
        
        c = getch(); 
        
        if (c == 'a')
            {   printf("You take the key. What would you like to do now?\n\n");
                key = 1; 
                printf("a. Open the door!\n");
                printf("b. Examine the key.\n");
                c = getch(); 
                if (c == 'a' && key == 1) printf("You open the door..."); 
                if (c == 'b' && key == 1) printf("It's a nice enough key, shiny and gold.");
            } 
            
        if (c == 'b' && key == 0) 
            {
                printf("You don't have the key.");
            }
               
    }

    Thanks for the help.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly, I would get rid of the variable a, since it's global and never used.
    Then I would write it like this to emphasize readability.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int c; 
        int key; 
        key = 0; 
        
        printf("\n\nYou stand before a locked door. There is a key nearby.\n\n");
        printf("a. Take key.\nb. Open door.\n\n");
        
        c = getch(); 
        
        if (c == 'a')
        {
            printf("You take the key. What would you like to do now?\n\n");
            key = 1; 
            printf("a. Open the door!\n");
            printf("b. Examine the key.\n");
            c = getch(); 
            if (c == 'a' && key == 1) printf("You open the door..."); 
            if (c == 'b' && key == 1) printf("It's a nice enough key, shiny and gold.");
        }
            
        if (c == 'b' && key == 0) 
        {
            printf("You don't have the key.");
        }
    }
    But this is not enough.
    Consider these lines...
    Code:
            if (c == 'a' && key == 1) printf("You open the door..."); 
            if (c == 'b' && key == 1) printf("It's a nice enough key, shiny and gold.");
    Key is always set to 1! So there is no point in checking it here... unless, of course, you don't intend to always set it to 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    At this stage it might be better to set "key" instead of "c" as in.
    Code:
    if (c == 'a')
    {
        printf("You take the key. What would you like to do now?\n\n");
        printf("1. Open the door!\n");
        printf("2. Examine the key.\n");
        key = getch(); 
        if (key == 1) printf("You open the door..."); 
        if (key == 2) printf("It's a nice enough key, shiny and gold.");
    }
    Just my 2c.

  8. #8
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    You've got a good beginning program here. I congratulate you on getting this far.

    What you want is the same question asked over and over again until you open the door. You need a loop, which is the point of variables; making loops act differently different times through.

    Lemme psuedocode something for you:
    Code:
    do 
    {
      show description.
      if (the key is not picked up) describe key in room.
      show choices "a) get key, b) open door"
      get input.
      if (input == 'a') 
      {
        pick up key (set key = 1)
        show pickup key message "It's a nice enough key, shiny and gold."
      }
      else if (input == 'b') 
      {
        if (key is not picked up) 
        {
          show key not picked up message
        }
        else
        {
          open door (you'll need a door variable, I'm thinking)
          show door open message
        }
      }
      else
      {
        show "press 'a' or 'b'" message
      }
    } while (the door is not open)
    show exit message.
    It's not as major a rewrite as it seems, but it is a major rethink. This way when you get out of the room you're out of the loop and you can move on with the next step without nesting the next step inside the previous.

    Here's a Cymon's Games example: http://cymonsgames.retroremakes.com/letterguess/
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thanks everyone. Thanks Elysia -- I see what you mean about no need for checking key there.

    Here is the new code based on guesst's pseudocode:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int c; 
        int key; 
        int door; 
        key = 0;
        door = 0; 
        
        do
        {
            printf("\n\nYou stand before a locked door."); 
            if (key == 0) printf(" There is a key nearby.");
            printf("\n\na) get key\n");
            printf("b) open door"); 
            c = getch();
            if (c == 'a')
            {
                key = 1; 
                printf("\n\nIt's a nice enough key, shiny and gold.");
            }
            else if (c == 'b') 
            {
                if (key == 0)
                { 
                    printf("You don't have the key.");
                }
                else
                {
                    door = 1;
                    printf("The door opens without a creak.");
                }
            }
            else 
            {
                printf("Press 'a' or 'b'"); 
            }
        } while (door == 0); printf(" What do you do now?");
    }
    I will try itCbitC's idea next.

  10. #10
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    FANTASTIC. I actually loaded it in my compiler and there was a small issue with the fact that getch() isn't in stdio, so I switched it to getchar() and it ran fine. Could use a little bit of formatting (a \n after open door for instance), but good job! You're on your way to being a coder.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    don't forget to return a value from main. there are two macros called EXIT_SUCCESS and EXIT_FAILURE in stdlib.h that work well, but 0 is common too.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    That must be why Meldreth added conio.h.

    EDIT: Oh, hey Meldreth.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    yeah, i added conio.h because that's where getch lives. getch and getchar do different things, so while guesst's change works, it might not have the result you expected.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Meldreth
    don't forget to return a value from main.
    That said, with respect to the 1999 (but not 1990) edition of the C standard, main() implicitly returns 0 if control reaches its end without encountering a return statement.

    Quote Originally Posted by matthew82
    That must be why Meldreth added conio.h.
    Yes. However, note that <conio.h> is non-standard. In the first place, you would not need getchar() or getch() if you run your program from the command prompt, or use an IDE that pauses the program for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Right. Changing getch for getchar it responded to the 'enter' character that I had to type after each line by telling me to just type 'a' or 'b'. I probably should have just included conio too, tho that library is technically non-standard.

    EDIT: Dang it laserlight. You slipped yours in a moment before mine.
    Last edited by guesst; 02-06-2009 at 02:23 PM.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Changing variable
    By Mithoric in forum Windows Programming
    Replies: 4
    Last Post: 03-30-2004, 09:45 PM