what's wrong w/ this extraction of integer

Why won't the former code work ?

doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>

int main( int argc, char* args[] )
{
    std::istringstream iss( args[1] );
    
    std::size_t i = 0;
    
    if( !iss >> i ) // <== HERE
        std::cout << "error";
    else
        std::cout << i;
}


works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>

int main( int argc, char* args[] )
{
    std::istringstream iss( args[1] );
    
    std::size_t i = 0;
    
    if( iss >> i ) // <== WORKS
        std::cout << i;
    else
        std::cout << "error!";
}


both w/ cmd line arg 10
Last edited on
Try if ( !(ss >> i) ).
wow, tnx guys,

why didn't i think of that
Topic archived. No new replies allowed.