Results 1 to 5 of 5

Thread: C++ Help!

  1. #1

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142

    C++ Help!

    hello, im a c++ newbie and i want to write this code,
    it looks as it should work but errors appear because
    im declaring arrays with variables instead of real numbers
    check it out:

    #include<iostream.h>
    void main()
    {
    int x;
    cout<<"Enter a number for x:";
    cin>>x;

    int ary[x]; // the error is here
    }

    is there anyway i could make this work?
    ؊Ϯϊ

  2. #2
    New Member
    Join Date
    Mar 2002
    Location
    BC, Canada
    Posts
    5
    Hi Osiris, you can't declare an array with no specific value in it unless it is a pointer. Try this:

    #include<iostream.h>
    void main()
    {
    int x;
    cout<<"Enter a number for x:";
    cin>>x;

    int *ary[x]; // the error is here
    }
    Joel Kitching - [email protected]

  3. #3

    Thread Starter
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142
    thanx, but i have some more question, can be assigned numbers just as any variable would, or they have to point to a certain variable, and would it work the same for char's?


    editted:
    it still gives me an error
    Last edited by Osiris; Mar 7th, 2002 at 10:58 AM.
    ؊Ϯϊ

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    linoleums solution will only get you in trouble and doesn't work anyway because it doesn't remove the source of the problems.

    You can't use a variable as the size specifier for an array. The solution is to allocate memory dynamically using the new operator. WARNING: dynamic memory allcation is a semi-advanced topic. If you are a real newbie you should do other things now.

    Usage of new:
    Code:
    int main()
    {
      int* ar = NULL;  // this will later point to the allocated memory
      int x;
      cin >> x;
      arr = new int[x];
      // Now you can use it.
      // The next line is extremely important!
      delete[] arr;  // do this when you're finished
     return 0;
    }
    You should not use dynamic memory until you know pointers well.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    #include<vector> is really easy to use
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width