int main(int argc, char* argv[])
{
    std::cout << argc << std::endl;
}

The code above will print a number depending on the number of arguments added to it. However I am writing a game in c++/python and I need the program to accept a integer argument that will define the current level the player is on. May someone please show me how?

Recommended Answers

Use stringstreams for formating.Argc will always contain the number of arguments passed to the program at the command line so you can't use that directly.Argv is an array in which you can index an argument like this:

argv[argc];

Use a stringstream for formatting the input instead

int value; …
Jump to Post

All 3 Replies

Use stringstreams for formating.Argc will always contain the number of arguments passed to the program at the command line so you can't use that directly.Argv is an array in which you can index an argument like this:

argv[argc];

Use a stringstream for formatting the input instead

int value;
std::istringstream (argv[argc-1]) >> value; 
//returns the last argument in this case and converts it to an integral type

Or if stringstream is too advanced, atoi(argv[i]) will also suffice.
i will be an index into the argv array.

Thank you I will use the atoi method, although I will look into the concept of string streams.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.