0

Hello,

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

Thanks.

3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by hor3045
0

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.

1

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. */
}   
0

@rubberman,

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

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.