0

When running my program with parameter 15, atoi returns 1. Atoi only returns the first digit. Is this supposed to happen? How to I get around it?

int _tmain(int argc, char* argv[])
{
    int a = atoi(argv[1]);
}
rcj
  • 631
  • 2
  • 11
  • 27
  • 1
    How did you determine that `atoi` returned 1? – David Schwartz Oct 07 '13 at 19:50
  • by breaking after that line and looking at the value in a – rcj Oct 07 '13 at 19:51
  • 1
    That won't work. Since `a` is local and never accessed, the compiler has no reason to actually write the result to `a`. – David Schwartz Oct 07 '13 at 19:56
  • Not sure what you're suggesting. declaring on a separate line doesn't fix the issue. That 1 is most certainly being written to a. printing a prints 1. printing `atoi(argv[1])` prints 1 – rcj Oct 07 '13 at 19:58
  • Why not have it print the value of `a`? You need to make the value of `a` part of your program's observable behavior. Using a debugger to make behavior observable is unreliable. Using a debugger to analyze observable behavior is fairly reliable. Actually observing behavior is reliable. You've picked the worst way to make this determination. – David Schwartz Oct 07 '13 at 19:58
  • 1
    @user2081737 Why don't you add `printf( "%d\n", a ) ;` or `std::cout << a << std::endl;` – Shafik Yaghmour Oct 07 '13 at 19:58

2 Answers2

0

It should convert the entire NUL-terminated string:

#include <iostream>
#include <stdlib.h>

int main() {
    std::cout << atoi("15");
}

result:

15
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • that worked properly. It seems my argv is being weird.. It was doing this before using the computers at this library but I don't remember how I resolved it. – rcj Oct 07 '13 at 19:54
0

Changing from _tmain to main fixed the issue.

rcj
  • 631
  • 2
  • 11
  • 27
  • 2
    If you use `_tmain` you should also use `TCHAR` instead of char and `_ttoi` instead of `atoi` – Joni Oct 07 '13 at 20:42