Thread: change what printf says depending on function arguments

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    change what printf says depending on function arguments

    i have the following function that someone kindly did for me that i have modified slightly so that it gets information from the user like number of players and their names rather than just the names.
    Code:
    int get_input(char line[], int linesize)
    {
    
        if (!fgets(line, linesize, stdin))
        {
            // maybe the user triggered EOF or something, so for our purposes, bail out:
            fprintf(stderr, "Error: unexpected end-of-file or read failure\n");
            exit(EXIT_FAILURE);
        }
    
        char *newline = strchr(line, '\n');
        if (!newline) //name too long
        {
            printf("Incorrect Entry: name must have a length of no more than %d characters\n",
                   linesize - 1);
            flush_line();
            return 0;
        }
    
        if (newline == line)
        {
            printf("Incorrect Entry: name cannot be blank\n");
            return 0;
        }
    
        *newline = '\0';
    
        return 1;
    }
    i pass into it the line array and size of the array so i can modify the buffer size accordingly however the if statement that checks for the \n character is fine if im looking for a string it reports the length accordingly. however to get a single number i have to pass into it line[3] which then says it wants an input of 2 characters.

    is there a way i can change the argument that printf takes according to what i am looking for.
    many thanks
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write another function.

    EDIT:
    Especially since if you want to read an if, you would want to use sscanf or strtol for the parsing.
    Last edited by laserlight; 05-16-2019 at 03:19 AM.
    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

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thats what i have in the function for getting the number of players i pass the string back to it and then use sscanf to extract the number and rather than just repeating your code i modified it slightly

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want the function to be more reusable, don't print from within the function. Rather, define an enum of error codes (e.g., LINE_INPUT_SUCCESS, LINE_INPUT_READ_FAILURE, LINE_INPUT_TOO_LONG, LINE_INPUT_BLANK), then call the function and take action according to the error code returned.

    By the way, the error message for too long an input is wrong: it should be linesize - 2.
    Last edited by laserlight; 05-16-2019 at 03:36 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  2. function with variable function arguments
    By Kyl in forum C Programming
    Replies: 10
    Last Post: 12-21-2011, 02:56 PM
  3. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  4. Function Arguments...?
    By Eminence in forum C++ Programming
    Replies: 5
    Last Post: 12-31-2002, 01:17 AM
  5. Printf arguments
    By evilmonkey in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2001, 02:50 PM

Tags for this Thread