Thread: Creating a menu that reads input via an array?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    14

    Creating a menu that reads input via an array?

    Currently I'm working on a text based adventure - I'm not doing this for class or anything, but just because it seemed like a fun idea to try. I'm new to programming, but I've been reading far ahead in my class book, so I know a little more than I should, but there may be things I have misunderstood. Keep that in mind when reading my post.

    So far, I have two problems. The first one is this - I need to have each room have a description. Thus, I need some type of variable to hold a string - at first, I thought I'd use a character array to hold the description. However, whenever I try to change the array, like so:

    desc = "This is a room description.";

    I get an error. If the description matches the exact number of characters the array is set to, this is the error:

    main.cpp(23) : error C2106: '=' : left operand must be l-value

    If the string I try to change the array to is less than the characters the array is set to, it's this error:

    main.cpp(23) : error C2440: '=' : cannot convert from 'char [3]' to 'char [4]'
    There is no context in which this conversion is possible


    So my first question is this: Is there a way to have a character array that will hold a string that I can change depending on the room the character is in? And if so, could you please point me in the right direction?



    My second issue is this. For the menu system, I origionally wanted to have movement commands available. I wanted the movement to be triggered by any number of words. If you've played zork, I'm sure you've realized that "n" and "N" and "north" as well as "North" all do the same thing. Being new, I thought I'd use the Switch case for this system. However, I've discovered that C does not support strings for switch cases. For my second attempt at building a menu-like system, I decided to try a bunch of if statements that compared an input array to see if it matched a menu item. I had planned to have an else statement at the end to catch anything that didn't match a menu item - however, I couldn't get that far. For some reason, my input array doesn't seem to equal the menu items - I'll post a very simplified version of my menu with only the "help" command on it to show what I mean.

    Code:
    printf( "Welcome to Athage. Please type a command.\n");
    		/* print the welcome message. The name of the game is Athage */
    		scanf("%s", desc );
    		/* read a string, store it in the desc array */
    	
    		printf("%s\n", desc );
    		/* print the string back for debugging purposes */
    
    		if( desc == "help"){
    			printf("The following commands are available.");
    		/* if the string equals help, display this */
    		}
    		else {
    			printf("I do not understand.");
    		}
    I have a feeling that I'm thinking about this the right way, but I've just not come across the information that I need to implement it. Please give me guidance as to how to build a menu system that can be driven by strings that are input by the user.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Use the strcmp() function to compare two strings in C.

    Code:
    if ( desc == "help" )
    Becomes ...

    Code:
    if ( strcmp(desc, "help") == 0 )

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't assign strings to arrays other than at the time of variable declaration. Like so:
    Code:
    char array[BUFSIZ] = { "At declaration..." };
    
    array = "Not allowed!";
    You will need to use a function (or a loop of your own) such as strcpy.
    Code:
    strcpy( array, "Will work!" );
    Second...
    Code:
    switch( array[0] )
    {
        case 'a':
            if( strcmp( array, "aardvark" ) == 0 )
                do_command_aardvark( );
            if( strcmp( array, "apple" ) == 0 )
                do_command_apple( );
            ...
        case 'b':
            if( strcmp( array, "ball" ) == 0 )
                do_command_ball( );
            ...
        ...
    }
    You could use something like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    25
    And if you're serious about writing a text adventure, you'd better use the right language for it. It'll save you a lot of trouble

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    14
    Quote Originally Posted by quzah
    Second...
    Code:
    switch( array[0] )
    {
        case 'a':
            if( strcmp( array, "aardvark" ) == 0 )
                do_command_aardvark( );
            if( strcmp( array, "apple" ) == 0 )
                do_command_apple( );
            ...
        case 'b':
            if( strcmp( array, "ball" ) == 0 )
                do_command_ball( );
            ...
        ...
    }
    You could use something like that.


    Quzah.
    But using the case structure like that still requires the action be called by a single character ( a, b, etc) as opposed to a string, correct? Unless I've overlooked something.

    I just did a quick test of your method. Thanks for telling me about the strcpy function by the way! So I can use a scanf to store a string inside of an array, and then use strcmp to compare the array to a set string, but instead of being able to type "help" as a string, I'm still limited to being able to use single characters for interaction? I was really looking for guidance on how to build a system that used strings for communication.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That does use strings. I am comparing the first value in the array with case statements to see what letter (or digit, or number, or...) to jump to, then using strcmp to compare that string to another.

    Think of it this way: If you had one giant list of words, sorted alphabetically, and your typed input started with 'z', you wouldn't want to go through every single word that didn't start with a 'z' one after another just to find it...
    Code:
    if( strcmp( input, "aardvark" ) == 0 )
        ...
    else
    if( strcmp( input, "apple" ) == 0 )
       ...
    ... all the way down to z ...
    Instead, we jump right to the first letter, and then start comparing only words that start with that. Naturally you'll want to make sure your input starts with a "usable" character, by skipping anything you don't want to allow.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    14
    Aha! Now I understand. I didn't realize what the array[0] was doing inside of the switch function - now I realize that it's telling the switch function to make all of it's call comparisons to the first letter of the character array. Awsome!

    That gets me past the first huge hurtle in my game.

    My program is getting a little large....I have a feeling there's a way to simplify what I'm doing by calling custom commands, but I haven't learned how to do that yet. For instance, I feel as if I should be able make a custom command called "move" and simply set up all the movement inside of that command, and then simply call "move" from the main section when I needed to deal with movement. This would keep down on the bloat etc of my program, and help out with the readability.

    So far I have the menu system setup to scan the first char value of the input array, as per your method, and then it cross-refrences the strings alphabetically. If it can't find it, it prints a generic error. However, all of my movement code is currently inside of the switch case function, so it's starting to look sloppy. It works, though!

    Now I just need to wrap my head around how to program an efficient method of giving each set of cordnates properties such as a description of the room and which paths may be travled and which may not. I have a few ideas, unfortunately none of them seem efficient. All would take a lot of code and a lot of comparissons, much like my menu system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM