Thread: Spliting up a string of numbers.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    12

    Spliting up a string of numbers.

    I have just started with C++ and am making some basic text stuff. I am making some "encryption" software, but am stuck with the decoding part.

    The user will enter a string of numbers like
    "766.555.2344.4324.23.2133"
    What I am stuck with is splitting the string so the first group of numbers (till the dot) becomes a variable and then the next.

    Any help?

    I can store each of the numbers in their own variable using gets() but then I need to combine them to get one variable containing the first group of numbers.


    Hopefuly their is some built in function that helps with this, but if not could someon point me the right way.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Check the doc for std::getline, especially the delimiter argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Sorry back ,problem was not solved. I tried using the getline() but it didn't work.

    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main ()
    {
        char decode_message;
        int length;
        //will be like 48657865.435435643.42143256467.243125435.2143214.5683465.4732985793.43278567.13527186.54396984
        cin>>decode_message;
        length=strlen(decode_message);
        getline(decode_message,length,".")
        
        
        
        
        
        
        
        
        for(;;)
        
     return 0;   
    }
    This just slipts the string into bits, but what I would like is for the first "section" (before the dot) to be assigned to a variable called "numbers" and then the second "section" as well. I was thinking of using some for() loops, but I'm stuck.

    I can get the first number and the second ect but then I can combine them to form one full variable. Just saying numbers=decode_message[0] + decode_message[1] dosn't work.

    Ahy help?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use strings (std::string), not single characters (char).
    You still need to use std::getline. std::cin >> will not work.
    Never use strlen. Use std::string's size member.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Will that assing each section to a variable? I have only been using C++ for a few weeks, and I am not quite sure what getline does.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    getline reads from a stream until it finds delimiter (argument).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    could i then say numbers=getline(decode_message, ".");

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If decode_message is a stream and number is a std::string, then yes.
    If decode_nessage is a string, then you have to use a std::stringstream, put the contents in it, and use the stringstream as the stream argument to getline.
    std::vector may also be of some interest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    I'm sorry but I'm completly lost. Could you maybe post a snippet showing what it looks like?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Are you trying to break your string into a series of smaller strings or into a series of numbers? Should the string "48657865.435435643.42143256467" be broken into the strings "48657865", "435435643", "42143256467" or the numbers 48657865, 435435643, 42143256467?

    If you are trying to convert to integral values, do you know the what the highest possible value would be?

    Jim

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    I am wanting to convert the into numbers enabling me to do some maths giving just a small number at the end.

    The largest number will not exceed maybe 8 didgitis but it can become smaller if that is easyier/better

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You can simply go by a loop like the following:
    Code:
    while(in>>temp) //'temp' is an int and 'in' is your input stringstream
    {
        result.push_back(temp); //result is a vector<int>
        char c;
        if(!(in>>c))break; //For ignoring the '.' s 
    }
    Now look up how getline and stringstreams work to understand the whole picture.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't have to read into a string then parse the string, you can read directly into an int, but you have to take the structure of the input into account:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main (void) {
    	int x;
    	char dot;
    	while (cin.good()) {
    		cin >> x;
    		cin >> dot;
    		cout << x << endl;
    	}
    }
    So eg:

    root~/C++»echo -n "54321.1345.1234" | ./a.out
    54321
    1345
    1234

    The next step is retaining the data in a vector, as manasij7479 demonstrated.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Could you prehaps explain the loop? Also would this work

    if decode_message is a char string, and then I said:

    Code:
    numbers=decode_message[0]+decode_message[1]+decode_message[2]+... all the way to the dot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spliting in group of two
    By RisingACK in forum C Programming
    Replies: 6
    Last Post: 11-10-2010, 12:00 PM
  2. spliting strings
    By tyroiusrtmaonm in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2007, 12:21 AM
  3. Spliting among 2 files
    By Suchy in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2006, 03:34 AM
  4. reading and spliting a string
    By nitinjainb in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2004, 08:35 AM
  5. Spliting the bytes
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-05-2002, 01:12 AM