I am currently required for my laboratory to write a program that will convert a base 10 integer to a different base, beginning with the letter b, between 2 and 16 as specified in the command prompt and output the answer in reserve. An example of a valid input in the command prompt is as follows:
123 b16
I devised the following code in order to figure out a way to split the second command prompt input so that I can obtain the integer to use for base conversion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int a;
char *b;
char c,d;
int e;
a=atoi(argv[1]);
b=argv[2];
c=b[2];
d=b[1];
e=atoi(&d);
cout << a << "\n";
cout << b << "\n";
cout << c << "\n";
cout << d << "\n";
cout << e << "\n";
|
This gives out the following output:
The reason why I wrote the code in this manner was that my original intention was to convert the c and d characters into integers e and f and recombine them into a new string as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
a=atoi(argv[1]);
b=argv[2];
c=b[1];
d=b[2];
e=atoi(&c);
f=atoi(&d);
cout << a << "\n";
cout << b << "\n";
cout << c << "\n";
cout << d << "\n";
cout << e << "\n";
cout << f << "\n";
|
However, I obtained the following output:
Why is it that when I convert the second character, d, to an integer it outputs the second character and first character, c, together?