Simple Array Question - Help

Discussion in 'C++' started by Ali, Aug 25, 2004.

  1. Ali

    Ali Guest

    Hi,

    I want to create an array, whose size should be entered by the user,
    instead of me hardcoding it on the code.

    The following is what i want to do:

    #include <iostream.h>

    int main()
    {
    int numElements=0;
    cin>>numElements;

    int Array[numElements];

    //do something

    return 0;
    }

    The compiler gives an error asking for a constant, such as int
    Array[5], etc.

    How do i solve the problem, if i want to let the user choose the Array
    Size?

    Thanks,

    Ali
     
    Ali, Aug 25, 2004
    #1
    1. Advertising

  2. Ali wrote:
    > Hi,
    >
    > I want to create an array, whose size should be entered by the user,
    > instead of me hardcoding it on the code.
    >
    > The following is what i want to do:
    >
    > #include <iostream.h>



    #include <iostream>

    Not needed in the code below.




    > int main()
    > {
    > int numElements=0;
    > cin>>numElements;
    >
    > int Array[numElements];



    int *Array=new int[numElements];






    >
    > //do something



    delete[] Array;


    Or better use std::vector.






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Aug 25, 2004
    #2
    1. Advertising

  3. Ali wrote:
    > I want to create an array, whose size should be entered by the user,
    > instead of me hardcoding it on the code.
    >
    > The following is what i want to do:
    >
    > #include <iostream.h>
    >
    > int main()
    > {
    > int numElements=0;
    > cin>>numElements;
    >
    > int Array[numElements];
    >
    > //do something
    >
    > return 0;
    > }
    >
    > The compiler gives an error asking for a constant, such as int
    > Array[5], etc.
    >
    > How do i solve the problem, if i want to let the user choose the Array
    > Size?


    You would have to create the array dynamically. Read your favourite C++
    book on 'new[]' and 'delete[]'.

    Victor
     
    Victor Bazarov, Aug 25, 2004
    #3
  4. Ali

    Jon Bell Guest

    In article <>,
    Ali <> wrote:
    >
    >The following is what i want to do:
    >
    >#include <iostream.h>
    >
    >int main()
    >{
    > int numElements=0;
    > cin>>numElements;
    >
    > int Array[numElements];
    >
    > //do something
    >
    > return 0;
    >}


    Use a vector instead.

    #include <iostream>
    #include <vector>

    using namespace std;

    int main ()
    {
    int numElements = 0;
    cin >> numElements;

    vector<int> Array[numElements];

    // do something

    return 0;
    }


    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Aug 26, 2004
    #4
  5. Ali

    Jon Bell Guest

    In article <>,
    Ali <> wrote:
    >
    >The following is what i want to do:
    >
    >#include <iostream.h>
    >
    >int main()
    >{
    > int numElements=0;
    > cin>>numElements;
    >
    > int Array[numElements];
    >
    > //do something
    >
    > return 0;
    >}


    Use a vector instead.

    #include <iostream>
    #include <vector>

    using namespace std;

    int main ()
    {
    int numElements = 0;
    cin >> numElements;

    vector<int> Array(numElements); // note parentheses not brackets

    // do something

    return 0;
    }


    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Aug 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    585
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    694
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    871
    Daniel Frey
    Jan 12, 2005
  4. Replies:
    14
    Views:
    555
    Karl Heinz Buchegger
    Nov 2, 2005
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    236
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page