Thread: how to take integer from a string

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    how to take integer from a string

    If I have a input string like
    rows: 3

    I'd like to assign that "rows" to a particular string and "3" to a integer variable. how do I do it...

    similar to python where I simply take

    Code:
    val = int ( raw_input("Enter") )

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would think that you have seen how scanf, or fgets with sscanf or strtol or atoi, have been used.
    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
    Jan 2011
    Posts
    113
    Thanks.. I forgot.. I could use strtok()..

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by suryak View Post
    Thanks.. I forgot.. I could use strtok()..
    Now why would you use strtok() and all it's clumsiness when you can simply use...
    Code:
    sscanf(string," %s:%d",str,&num);

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by CommonTater View Post
    Code:
    sscanf(string," %s:%d",str,&num);
    This is also a wrong solution and will never work.
    A working solution is
    Code:
    if( 1==sscanf("rows: 3","%*[^:]:%d",&intvar) )
    {
      /* have fun */
    }
    else
    {
      /* bad input */
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    This is also a wrong solution and will never work.
    A working solution is
    So, is it now your plan to run around following me from thread to thread telling people that simple solutions --that I test before posting-- don't work?

    I'm betting you didn't even read the original message where he said he wanted to put the first part in a string and the second part in an int... you just decided to try to tell him that a perfectly simple, by the book, solution wouldn't work and then substitute your own.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    I have not test your wrong solution, i have see it.
    A simple wrong solution is a wrong solution and if you have problems to post simple working C code, you should never post your wrong newbie mistakes here.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    I have not test your wrong solution, i have see it.
    A simple wrong solution is a wrong solution and if you have problems with simple C code, you should never post your wrong newbie mistakes here.
    Oh come on... what the hell kind of idiot are you?

    You "solution" totally ignored the OP's stated requirements.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well I went and tested both, and for some reason CommonTater's example does only gets the string, but BillyTKid's example only gets the int. In fact for some reason I couldn't get it to read in both using a single scanf. Probably something to do with me mostly using C++ rather than C. If anyone can explain exactly why CommonTater's example didn't read in the integer, or how to make it work inside a single scanf then please share.

    FWIW, Here's a solution that works, complete with buffer overrun protection:
    Code:
    char input[] = "rows: 3";
    char str[20];
    int num;
    if (sscanf(input," %20s*[^:]", str) + sscanf(input,"%*[^:]:%d", &num) == 2)
    {
    	//Bingo!
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Looking at the op's sample again you may need...
    Code:
    sscanf(string," %s: %d",string,&num);
    It's pattern matching so sometimes you have to mess with things a little....

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    yes, CommonTater's 1st threat solution is not working...

    But guys I'm just not looking for string like this rows:2 but after rowsif can be any non-numeric char that has to be eliminated...
    I tried like this
    Code:
    sscanf(string,"%s %*c %d",str, &num);
    .. here str is taking taking the whole part of "string" and num is showing 0...

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It's pattern matching so sometimes you have to mess with things a little....
    You should really listen to BillyTKid sometime and not go round biting everyone's head off every time they correct one of your mistakes.

    Code:
    $ cat bar.c
    #include <stdio.h>
    
    int main()
    {
      char input[] = "rows: 3";
      char string[100] = { 0 };
      int num = 0;
      int result = sscanf(input," %s: %d",string,&num);
      printf("%d %s %d\n", result, string, num);
      return 0;
    }
    $ gcc bar.c
    $ ./a.out 
    1 rows: 0
    %s will also eat the :, which prevents the rest of the format string matching at all when it tries to match a literal : as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    Code:
    if (sscanf(input," %20s*[^:]", str) + sscanf(input,"%*[^:]:%d", &num) == 2)
    Haven't tested, but I don't think that is correct because order of evaluation is unspecified.
    Last edited by laserlight; 11-06-2011 at 12:45 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

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Haven't tested, but I don't think that is correct because order of evaluation is unspecified.
    I'm a bit puzzled by that comment. It certainly doesn't matter which order the scanf's are executed, they are quite independent as the only side-effect is to fill in str or num.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    I'm a bit puzzled by that comment. It certainly doesn't matter which order the scanf's are executed, they are quite independent as the only side-effect is to fill in str or num.
    Oh yeah. You're using sscanf, not scanf, so that is fine. If you used scanf instead, then the order matters because the input buffer would be changed.
    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. Convert Integer to String and String to Integer
    By hqt in forum C++ Programming
    Replies: 26
    Last Post: 09-15-2011, 11:39 AM
  2. string to integer
    By slash_axl in forum C Programming
    Replies: 2
    Last Post: 10-09-2010, 06:15 AM
  3. Replies: 4
    Last Post: 12-04-2009, 10:22 AM
  4. hex string to integer...
    By xion in forum C Programming
    Replies: 4
    Last Post: 07-31-2003, 01:58 PM
  5. Integer to String
    By shazow in forum C Programming
    Replies: 5
    Last Post: 05-06-2002, 02:01 PM

Tags for this Thread