Thread: Finding number in string

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Finding number in string

    Would there be an easier way to find and print out one number from a string without knowing the number?
    For example the fifth number.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
              string port1;
              size_t found;
              int port1int;
    
              string ftpmessage = " (66,220,9,50,27,99).";
              
              found  = ftpmessage.find(",");
              found  = ftpmessage.find(",",found+1);
              found  = ftpmessage.find(",",found+1);
              found  = ftpmessage.find(",",found+1);
    
              for(int i=1; i<4; i++)
              {
                  port1 = ftpmessage.substr(found+1,i);
                  if (port1.find(",") != string::npos) break;
    
              }
              cout << "port1: " << port1 << endl;
    
              return 0;
    
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is it possible that by "without knowing the number" you really mean "with the number in a variable"? If so, there are such things as loops, for when you want to repeat something a certain number of times.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Tabstop!

    Im sorry i meant the number after the fourth comma.

    So i guess i should put this in a loop:

    Code:
     found  = ftpmessage.find(",");
              found  = ftpmessage.find(",",found+1);
              found  = ftpmessage.find(",",found+1);
              found  = ftpmessage.find(",",found+1);
    I was thinking about a function that finds the fourth comma and gives back its position
    or the characters after it, that would save the place for a loop.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it's always the fifth number (== after the fourth comma) then I'm not sure you have a question, since that's what you started with.

    Anyway, as a suggestion, because I've been in a stringstream frame of mind for the last little while, why not use a stringstream? You can initialize your stringstream with the string you've got, and peel off numbers with >> until you get to the one you want. (You'll either have to read the comma, or there's probably some magic you can do to make >> think (at least temporarily) that comma is a whitespace character so it will automatically separate and be removed.)

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Good idea! Thanks!
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by tabstop
    (You'll either have to read the comma, or there's probably some magic you can do to make >> think (at least temporarily) that comma is a whitespace character so it will automatically separate and be removed.)
    I wrote this token matching manipulator that could be used for such purposes.
    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

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Laserlight, i look into it.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Finding MAX or MIN within a string
    By DBA1 in forum C Programming
    Replies: 10
    Last Post: 02-27-2004, 08:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM