0

Every time I have used atoi it has returned zero.

Here's some example code

int key = atoi(argv[1]);

and

int atoi(message);

Where am I going wrong?

1 Answer 1

2

If this is for example for Caesar cipher and you're running ./caesar 2 then key should indeed be set to an integer of 2 as argv[0] is the program name and argv[1] is the string 2.

But int atoi(message); first doesn't set the return value of atoi to anything, no variable, and also if message is a string of chars, which from it's name it seems to be, then calling atoi on it will indeed return 0. Although you're not saving that anywhere yet.

Hope that helps Sean

1
  • Also, if you run the program without arguments ./caesar , and forget to check that argc>1, then you'll get a seg fault.
    – Cliff B
    Commented Oct 14, 2015 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .