Thread: Problem with input and output

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    4

    Problem with input and output

    Hey guys, so I'm Writing a text rpg and i'm having a issue with the the scanf function. Firstly, here is the function that contains the problem.

    Code:
    void Character_Creation_Players(Player *player)
    {
        char* name;
        char* race;
        char input;
    
        printf("=====================================================\n");
        printf("================Character Creation===================\n");
        printf("=====================================================\n\n");
        
        printf("What will be the name of the hero brave enough to venture into the Forest of Darkness?\n");
        scanf("%s", name);    
    
        printf("What Race is this %s?\n", name);
        scanf("%s", race);
        printf("%s", race);
        
        printf("What class will this %s be? (1) = Warrior, (2) = Rogue, (3) = Wizard, (4) = Cleric\n", name);
        scanf("%c", &input);
    
        switch (input)
        {
            case '1': //Warrior
                Init_Players(player, name, race, "Warrior", 1, 18, 0, 10, 11, 0, 120, 20, "Rusted Iron Axe", 3, 9, 5, 5);
                break;
    
            case '2': //Rogue
                Init_Players(player, name, race, "Rogue", 1, 12, 8, 7, 14, 0, 120, 50, "Rusted Iron Daggers", 2, 11, 2, 5);
                break;
    
            case '3': //Wizard
                Init_Players(player, name, race, "Wizard", 1, 10, 15, 6, 18, 0, 120, 20, "Basic Staff of Magic", 2, 6, 5, 5);
                break;
    
            case '4': //Cleric
                Init_Players(player, name, race, "Cleric", 1, 12, 12, 7, 12, 0, 120, 20, "Magical Battle Staff", 2, 8, 5, 5);
                break;
            
            default: //Invalid Input
                printf("Error 1:\nYou have entered an invalid selection\n");
                return;
        };
    }
    So the problem is that after I type the name of the race it won't let me enter the class selection number and goes straight to the default case in the switch statement.
    When I added the printf after inputting race it outputs "(null)" instead of the string that I typed. I know that the '\0' might still be in the input stream so I've already tried adding a space before the second %s and %c in the scanf statements, but still getting the same problem.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In the future, you should create the smallest complete program that we can compile, which exhibits the problem you're seeing.

    Anyhow, you're attempting to read strings into uninitialized character pointers. If you want to use this approach, you need to allocate memory, i.e. with "malloc()". It might be easier, however, just to use character arrays instead of dynamic memory allocation - but of course, this is up to you.

    Question 7.1

    Additionally, adding the space before "%c" was on the right track (although that's because the newline, '\n', is left in the buffer, not the null character '\0'). See the FAQ for more details on this: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    Using "scanf()" to read strings is not the best approach - it's a pain to limit the characters it will read (to avoid overflow) in a neat manner, and it stops reading at the first whitespace. The method preferred by many is to use "fgets()": FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    I also find it odd that you force the user to select from a discrete list of classes (which is good), but do not do the same for race. Presumably, there is a set list of races, each of which has an effect on abilities, attributes, etc. Allowing someone to enter whatever they want does not seem the most effective method for this kind of input (unlike the characters name, which can really be anything the person wants).

    That being said, adding this stuff will make your function a bit long and varied. It's good practice to write functions that do a single thing, and do it well. You might want to consider having your "create character" function call some helper functions to deal with different aspects of character creation (e.g. a function to prompt and receive class, another to prompt and receive race, etc).

    Finally, I'd suggest you get out of the habit of using magic numbers. You should define names for the constants you use in your program - especially meaningful names. This will not only make your code more self-documenting, but making changes to the program is easier as the values can all be defined in one place. This is especially true if the values are used in multiple places - rather than hunting down the usage of each throughout your code, you just change the defined value for the constant and the value will automatically be updated everywhere in the code you use the named constant.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    Thanks.

    I meant '\n', not sure really why I typed '\0'.

    Although I'm not exactly sure why I don't need a space before the second scanf with "%s", wouldn't the '\n' left in the buffer be the first character in the second string?
    I haven't learned fgets() yet, but I assume I would just use it like I'm using scanf?

    I haven't decided on all the races and how they affect attributes yet.

    As for the numbers, they are just being used to assign the initial values of the members of the Player struct. I thought it would be easier to just manually type them instead of creating variables for each one that would need to be reassigned values for each different class making the function longer.

    I am a fairly new programmer, this is actually the largest project I've done so far, so it's probably not going to have the most efficient and best way of doing things.

    edit:
    I suppose I should click links before asking about things like fgets()
    Last edited by Mechennyy; 12-22-2014 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Although I'm not exactly sure why I don't need a space before the second scanf with "%s", wouldn't the '\n' left in the buffer be the first character in the second string?
    "scanf()" normally skips leading whitespace - one of the exceptions is when you're scanning a single character with "%c".

    I haven't learned fgets() yet, but I assume I would just use it like I'm using scanf?
    No, it is quite a different function. You can search online for information on what arguments need to be passed to "fgets()" - but it's also described in the "get a line of text" link I posted above.

    I am a fairly new programmer, this is actually the largest project I've done so far, so it's probably not going to have the most efficient and best way of doing things.
    That's fine - I'm glad you're having fun with the learning process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with file input output
    By C_programmer.C in forum C Programming
    Replies: 6
    Last Post: 05-21-2010, 09:33 AM
  2. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 03:55 AM
  3. array input/output problem
    By glowstick in forum C Programming
    Replies: 1
    Last Post: 09-10-2002, 07:56 PM

Tags for this Thread