convert string to int

This is a discussion on convert string to int within the C++ Programming forums, part of the General Programming Boards category; I have this program where the user can enter commands, followed by either a string, or int, depends on which ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    convert string to int

    I have this program where the user can enter commands, followed by either a string, or int, depends on which command asks for.

    so i store the input all into one string, then cut it into two strings command and input

    is there a simple way to convert a string of numbers into integers?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string str = "123 456 789";
        std::stringstream sstr(str);
        int number;
    
        while( sstr >> number )
            std::cout << number << std::endl;
    
        return 0;
    }
    Output:
    Code:
    123
    456
    789
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    thanks, worked perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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