I recently started learning C++. I come from a background of python and web development so bear that in mind.
I wanted to write a program which takes a second argument which is an integer and does stuff. But then it started acting crazy and I realized this was a problem with casting. So I wrote this:
int main(int argc, char** argv) {
int iMynum = int(argv[1]);
cout << argv[1] << endl;
cout << iMynum << endl;
return 0;
};
running myprogram 100
results in:
100
3429646
I don't understand this. I found similar questions on SO but the answers were complicated and confused me even more. I tried doing this instead:
int iMynum = (int) *argv[1];
Which resulted in:
100
49
What is going on here?
update
Obviously, I'm an idiot - int(argv[1])
is python. So the answers really helped me understand what I was doing, but suggested code solution did not work (except for atoi which I take is not recommended). I kept looking and tried this:
into mynum = const_cast<int>(argv[1][0])
And got an error saying const_cast cannot convert 'char' to 'int'
. So what should I use instead?