Thread: Converting cmd arg to int

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    Converting cmd arg to int

    I am trying to convert the first command argument from char ** to an int i can use to declare an array, but I can't figure out how to do it. Basically that user specifies the size of the array from that cmd argument.

    example: person puts 10 in command argument
    Code:
    int main(int argc, char **argv)
    {
    
        //code here to make an:
        int iArray[argv[1](or whatever) that works as int=10 or any cmd line argument] ] = {0};
    
    }
    I've tried a bunch of things like reinterpret_cast etc but all i can get is a bunch of random values in my int

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You can use atoi( ) to convert a string to an int.

    Also, you're going to need to dynamically allocate the array. You can't do it the way you are right now.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You don't want the compiler to pretend that a string is an int, you want to actually take the int represented by the string and use it as an int. To do this you need to do a real conversion:
    Code:
    // With atoi
    #include <stdlib.h>
    ...
    int i = atoi ( argv[1] );
    Code:
    // With strtol because atoi sucks
    #include <cstdlib>
    ...
    int i = static_cast<int> ( strtol ( argv[1], 0, 0 );
    Code:
    // With stringstreams because they're cool :)
    #include <sstream>
    ...
    stringstream ss ( argv[1] );
    int i;
    
    ss>> i;
    Or you could do it manually, but that's tricky and I don't recommend it unless you're in learning mode.

    >int iArray[ /* Something */ ] = {0};
    This isn't legal C++. You would be declaring an array with a non-constant size. While such a feature is a common extension, it isn't standard. You'll need to either declare a dynamic array:
    Code:
    // Assuming i was suitably initialized
    int *iArray = new int[i];
    Or use a standard vector:
    Code:
    #include <vector>
    ...
    // Assuming i was suitably initialized
    vector<int> iArray ( i );
    [edit]
    I'm officially embarrassed...
    [/edit]
    Last edited by Prelude; 03-17-2004 at 09:33 PM.
    My best code is written with the delete key.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >#include <stdlib.h>
    You're really starting to worry me, Prelude... we may have to find a new coding guru.

    Oh, and just a quick note to the OP: for Prelude's last two examples, you need to put 'using namespace std;' at the top of your code, just below the #includes.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You're really starting to worry me, Prelude... we may have to find a new coding guru.
    Two goofs in less than an hour worries me as well.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    thanks, it works like a charm now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM