Thread: Converting question

  1. #1
    KevyKev
    Guest

    Converting question

    I need to convert a character input into numbers. For instance, in this example:

    127.0.4.56

    Since this is not a float, I need to convert 127, 0, 4, and 56 into numbers. My question is how can I read those numbers separately.... use an array? use a particular input stream? or something else??

    Thanks.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Here's one way -

    Code:
    #include <iostream> 
    #include <sstream>
    using namespace std; 
    
    int main() 
    { 
    
    	stringstream ss;
    	int a,b,c,d;
    	char discard;
    	char* str = "127.0.4.56";
    	
    	ss << str;
    	ss >> a >> discard >> b >> discard >> c >> discard >> d;
    	
    	cout << a  << ' ' << b << ' ' << c << ' ' << d;
    
    	return 0; 
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can use atoi() and take advantage of the fact that atoi() stops at non-numeric input so the . delimeter will not be a problem
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    just out of curiousity, what the heck is <sstream>? I have never seen it in my life.

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I would do what stoned_coder says... parse an array of your individual character sets with the '.' as a delimiter and then atoi() each result....
    Blue

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Its a string stream. works like sprintf() and sscanf()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    KevyKev
    Guest

    Thanks

    Thank giys for your info. I'll see if it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM