Finding number in string

This is a discussion on Finding number in string within the C++ Programming forums, part of the General Programming Boards category; Would there be an easier way to find and print out one number from a string without knowing the number? ...

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

    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;
    
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    790
    Good idea! Thanks!
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Thank you Laserlight, i look into it.
    Compiler MSVC++ 2013 with Code::Blocks.

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, 10:03 PM
  4. Finding MAX or MIN within a string
    By DBA1 in forum C Programming
    Replies: 10
    Last Post: 02-27-2004, 07:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21