Thread: How to read in a character followed by an integer w/o space

  1. #1
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11

    How to read in a character followed by an integer w/o space

    Hi Guys(and Girls),
    Let me preface this by saying that this is for a homework assignment so please don't go into how to carry out the conversions. The assignment is the normal hex to octal and Quart (base 4) via bit munipulation which I have worked out myself. However, I have been trying all day to figure out how to read in a string such as H1234, or O4567 and cannot figure it out. If someone could please just give me push in the right direction on how to parse the input I can handle the remainder myself. I'm just stuck and I've tried for hours. Could someone help me out? Thanks. -court

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First, read a character. Then, read the rest.
    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
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    Thanks for the response. Could you be a little more specific?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char base;
        char number_in_base[5];
        char input[] = "H1234";
        if (sscanf(input, "%c%4s", &base, number_in_base) == 2)
        {
            printf("base: %c; number in base: %s\n", base, number_in_base);
        }
        else
        {
            fprintf(stderr, "The input was invalid.\n");
        }
        return 0;
    }
    So, with %c, I read a character, then with %4s, I read the rest, up to 4 characters since the destination array can only store 5 characters (i.e., one is reserved for the null character).

    There are other ways by which you might do this, e.g., use getchar or something similiar for that one character, then maybe fgets for the rest.
    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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    There are other ways by which you might do this, e.g., use getchar or something similiar for that one character, then maybe fgets for the rest.
    You're right in what you say but mixing manners of input (line-oriented like fgets(), character-oriented like getchar(), format-oriented like fscanf()) on the same stream can produce surprising effects.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    Thank you, laserlight! I really appreciate it. I wasn't aware of sscanf. It's perfect. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer input till space.
    By peripatein in forum C Programming
    Replies: 3
    Last Post: 05-24-2013, 02:20 AM
  2. Is space a character ?
    By freddyvorhees in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2008, 03:35 PM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. Converting space character in string to integer
    By boostage in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2006, 08:40 PM
  5. How do I tell what character is in a space?
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 11:11 PM

Tags for this Thread