Reading unsigned int from command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh369
    New Member
    • Sep 2006
    • 10

    Reading unsigned int from command line

    From the command line, I passed a value like this:

    a.out -seed 3333333333

    But when I did:
    unsigned int seed;

    seed = atoi(argv[2]);

    cout << seed;

    it just puts out the decimal equivalent of 0x7fffffff; I cannot change the internal variable to int. Any hints?
  • risby
    New Member
    • Sep 2006
    • 30

    #2
    Originally posted by Suresh369
    From the command line, I passed a value like this:

    a.out -seed 3333333333

    But when I did:
    unsigned int seed;

    seed = atoi(argv[2]);

    cout << seed;

    it just puts out the decimal equivalent of 0x7fffffff; I cannot change the internal variable to int. Any hints?
    Use strtoul() rather than atoi().

    #include <stdlib.h>
    unsigned long strtoul(const char *nptr, char **endptr, int base);

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The actual problem is that atoi returns int even though you are assigning the value to unsigned and 3333333333 is outside the range of an int so it has returned INT_MAX = 0x7FFFFFFF

      Comment

      Working...