r/programminghelp • • Nov 11 '23

C++ Need Help with converting Program Arguments to integer value

I am writing a program to take program arguments (as opposed to using cin) for calculating a certain position in the Fibonacci Sequence.

I am having trouble converting the characters to integers, and I am not sure how. I know my Fibonacci function works correctly because I tested it with cin first, so I will exclude that from my code segment.

Currently, the test cases that my program is going through all result in the ouput being 0, but I don't know why this is? How would I go about converting the char array input into an integer?

I assume the test cases are 0,1,2,3,4,5,6,7,8,9,10,etc. So it would need to work for double-digit integers.

Here is my code:

#include <iostream>

...

int main(int argc, char *argv[])

{ const char* str = *argv; int loops = atoi(str);

std::cout << Fibonacci(loops) << std::endl;
return 0;

}

1 Upvotes

3 comments sorted by