I'm having a terrible time trying to convert a variable. When I convert the value of argv[1] from char* to int using atoi, it just doesn't seem to come out right. Anytime I use a two-digit number as my argument, it comes out as 15, regardless of what number it was I typed. With three digit numbers, it's 16. What exactly am I doing wrong here? It works fine when I use the prompt rather than specify an argument.
Code:#include <iostream> #include <cstdlib> using namespace std; int main ( int argc, char *argv[] ) { int a; int b; int c; int count; int endset; unsigned keepLooping; if (argc != 1) {endset = atoi(argv[1]); goto noprompt; } cout << "\nFibonacci's Number Sequence\n"; cout << "Input how many numbers to go through: "; cin >> endset; noprompt: count = 0; endset -= 1; if (endset == 0) { cout << "\n1\n"; return 0; } if (endset < 0) { cerr << "\nInvalid number specified\n"; return 1; } a = 0; b = 1; keepLooping = 1; while (keepLooping) { count += 1; c = a + b; if (count == 2) { c = 1; a = 1; b = 1; } cout << "\n" << c; a = b; b = c; if (count > endset) { keepLooping = 0; cout << "\n"; return 0; } } }



LinkBack URL
About LinkBacks



