Arguments to Main

Hi all,

So I'm trying to create a C++ program that will generate and output a random set of 2D points to be passed via a pipeline to a FNN built w/PyBrain.

I'm a bit rusty with C++ and the current code/output I have/am getting is confusing me (see below)...

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

using std::cout;

int main(int argc, char** argv) {
	cout << argv[0][1] << '\n';
	if(argc > 0) {
		srand(static_cast<int>(argv[0][1]));
		cout << static_cast<int>(argv[0][1]); 
	} // END if(argc > 0)

} // END main(argc, argv) 


Compiled/run using the following on Ubuntu:
g++ -std=c++11 -pedantic <source> -o <exeName>
./<exeName> 5


I get the following output:
/
47


Obviously, I want to use the cmdline argument to seed the random number generator but something else is going on. Additionally, the char '/' and int 47 are corresponding ints on the ASCII chart - but where is my program grabbing the '/' from?
Last edited on
where is my program grabbing the '/' from? 

argv[0] is the name of the program - according to your run command it is
./<exeName>
You are outputting the 1th element of that - the 0th is '.' and the 1th is '/': so that is what is output by your line 7. Array indexing starts at 0.
Last edited on
The second line's 47, is the inner code of character '/'. So, the result is right.
The first argument argv[0] is the name of the program itself.
You need to take the second argument, argv[1] and convert it to an integer,
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>
 
int main(int argc, char** argv) 
{
    if (argc > 0) 
    {
        std::cout << argv[1] << '\n';
        int n = atoi(argv[1]);
        std::cout << "n = " << n << '\n'; 
    } 

}

Topic archived. No new replies allowed.