How woud I convert String to Integer using arguments from command line? I am stuck, and no idea where to start.


Recommended Answers

All 3 Replies

What arguments are you planning on supporting? Are they the strings to convert or switches that alter how the conversion happens? The first step is to solidify your understanding of how the program should work at a higher level, then you can dig down and implement the pieces.

First, arguments on the command line are presented to the main() function via the argv string array. Next, process each argument (argc 0 is the command name, argc 1 is the first argument, etc) and convert each to an integer. IE:

cmdname 1 2 3 999 will pass 5 arguments (arg 0 thru arg 4) as in int main(int argc, const char* argv[]) so you need to iterate argc as an index in argv from 1 thru 4, as in:

for (int i = 1; i < argc; i++)
    int value = atoi(argv[i]);
    /* Do other stuff here, such as output of the value. */


How would I do without the atoi? I want to know in other way.


Be a part of the DaniWeb community

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