Declaring an variable number of variables

This is a discussion on Declaring an variable number of variables within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that will analyze a simple map that will be defined by a sequential data file. ...

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    Declaring an variable number of variables

    I am writing a program that will analyze a simple map that will be defined by a sequential data file. Each data file may have a different number of vertices and legs.

    For example, the 'map' that looks like a plus-sign would have 5 vertices and 4 legs. I need to define variables, something along the lines of vert1, vert2, ... , vert5, leg1, ... , leg4.

    I tried using the token-pasting operator in a loop to declare the variables, but I must be doing something wrong. First, it seems to want me to declare vert and leg as variables before it will paste. That I can understand, and it wouldn't cause any problems. However, when I do that, I end up with variables like 'legloop' of 'vertn' (where loop or n are the loop controlling variables) instead of leg1 or vert6.

    Is the token paster the way to go here?
    If so, how should it be used?
    If not, what method should I use?


  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Arrays are what you'd want to use in this situation. In C++ at least, you can have arrays without expressly specifying the size of the array, so they should be all you'd need.

    http://www.cprogramming.com/tutorial/lesson8.html

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    you mean youre using foo## bar ?
    thats the wrong way to do it.
    that ## operator is something the preprocessor applies.
    thus the token will be pasted before it is sent to the compiler.

    you might want to used the std::string object for string concatenation
    signature under construction

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually sean you have to specify the size of the array. You however can dynamically allocate an array using new.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Tokenizing won't work because it simply appends the two text portions together. Why don't you allocate the number of variables dynamicly, and use them like an array? Or why not use a linked list, or a vector? If you don't know what an array is, here's how it works:

    Say we need five integers. We could do this:
    Code:
    int v1, v2, v3, v4, v5;
    
    v1 = 0;
    v2 = 3;
    v3 = 6;
    v4 = 9;
    v5 = 12;
    Or whaver you like for your initializations. Or we could use an array:
    Code:
    int vars[5]; // make 5 integers, grouped under the name 'vars'.
    
    vars[0] = 0;    // you index them starting with zero...
    vars[1] = 3;
    vars[2] = 6;
    vars[3] = 9;
    vars[4] = 12;    // indexing through "size -1", in this case 4. (5 - 1 == 4)
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I feel like such a fool.
    I can have the size of the array in the data file, so that's not a problem.

    Thanks for the help.

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by sean_mackrory
    Arrays are what you'd want to use in this situation. In C++ at least, you can have arrays without expressly specifying the size of the array, so they should be all you'd need.
    Only if you define a string after the assignment operator...it'll fill in the appropriate amount of elements for you.

    Else, you need to declare the number of elements.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    I think sean meant the leaving out the number actual number in the middle and letting the compiler determine how large the array needs to be. At least I hope so
    Woop?

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The impression I got was:
    Code:
    int size;
    cin >> size;
    int arr[size];
    Which isn't standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  2. Variable Properties vs Public Variables
    By Rainbowinblack in forum C# Programming
    Replies: 2
    Last Post: 02-19-2008, 12:48 PM
  3. Does declaring a variable cause lag in the executable?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2005, 08:15 PM
  4. Declaring Variables Using Variables
    By Mr. Squirrel in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2001, 11:38 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21