Command Line arguments

I have been toying with this code for a few minutes, and I cannot find a way to convert the third argument from the command line to an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main( int argc, char *argv[] )
{
	try
	{
		unsigned int size = int ( argv[2] );
		
		cout << size << endl;

		vector<int> randvector;
		srand(1000);
		
		for(int count=0; count < size; count += 2)
		{
			randvector[count] = rand() % 100;
		}
		
		for(int count=1; count < size; count += 2)
		{
			randvector[count] = - ( rand() % 100);
		}
		
		cout << randvector.size() << endl;
		
		for( int x = 0 ; x < 20; x++ )
		{
			int y;
			y = randvector[x];
			cout << y << endl;
		}
		
	}
	catch( ... )
	{ exit(1); }
}


Thanks in advance for any help.
Use a stringstream:

1
2
3
int my_int = 0;
std::stringstream str(argv[1]);
str >> my_int;


You need to include sstream to use it though.
1
2
3
4
5
{
    std::stringstream stream(argv[2]);
    if (!(stream >>size))
        //not a number
}
Topic archived. No new replies allowed.