concatenating a variable integer to a structure variable name.

Grazehell

2[H]4U
Joined
Jun 13, 2001
Messages
2,660
is that possible. example

Struct example
{ int apple;
int orange;
};

int counter;

for (counter=1; counter >10;counter++)
{
example orchard+counter; //here I could like to concatenate the counter into the stucture variable here so it's would be orchard1 and then orchard2, orchard3 etc


orchard+concatenated counter integer.apple = UserInput;


blah blah blah
}
 
What language is this? I assume it's probably C/C++, but I can't really be sure since you have some funky things going on in this line:

Code:
orchard+concatenated counter integer.apple = UserInput;

And yes, you can convert an int -> string, either by writing your own itoa-style function or by using some library function in your language of choice, probably.
 
its C++. Sorry that is was just suppose to be an model of what I wanted to do and not meant to be take literally, it really is suppose to be(or whatever the counter number was)

orchard2.apple=UserInput;

I will look into the solution you gave. I am a programming noob.
 
There are multiple ways to do it in C++.

Read this article by Herb Sutter, which discusses multiple ways of doing it:
http://www.gotw.ca/publications/mill19.htm

EDIT:

I finally get what you're asking - declaring a variable name that uses a variable's value as part of the name.

Sorry, can't do that.
 
This is why you have arrays.. :)

Code:
struct example
{ 
  int apple;
  int orange;
};

int main(void)
{
  struct example orchard[10]; // declare a variable of name orchard which is of type struct example[] that has 10 elements.  In other words, declare an array of 10 examples called orchard.

  for (int i = 1; i < 10; i++)
  {
    orchard[i].apple = UserInput;
  
    // blah blah blah
  }

  // something else

  return 0;
}
 
is that possible. example

Struct example
{ int apple;
int orange;
};

int counter;

for (counter=1; counter >10;counter++)
{
example orchard+counter; //here I could like to concatenate the counter into the stucture variable here so it's would be orchard1 and then orchard2, orchard3 etc


orchard+concatenated counter integer.apple = UserInput;


blah blah blah
}

D'oh! :D

You should consider grabbing a C++ tutorial off the 'net. I'm surprised that you'd be learning structures before arrays.
 
D'oh! :D

You should consider grabbing a C++ tutorial off the 'net. I'm surprised that you'd be learning structures before arrays.

Yeah my C++ professor has it that way, along with the fact that in my text structures come 2 chapter before arrays.
 
Yeah my C++ professor has it that way, along with the fact that in my text structures come 2 chapter before arrays.
You're thinking on your feet with that loop. That's a good sign!

Since you haven't learned arrays yet, I'd safely bet that at least one student in your class is going to declare ten apple and ten orange variables ;)
 
You're thinking on your feet with that loop. That's a good sign!

Since you haven't learned arrays yet, I'd safely bet that at least one student in your class is going to declare ten apple and ten orange variables ;)

lol, you bet'cha that did and my professor wanted to snap my head off for mentioning arrays because he has not covered it!
 
D'oh! :D

You should consider grabbing a C++ tutorial off the 'net. I'm surprised that you'd be learning structures before arrays.

I'm not...

Properly teaching arrays means that the student needs to have an understanding of pointers beforehand.

Also, teaching structures first gets students used to the idea of complex types with something that is, at least from my experience, easier for them to conceptually grasp.

Personally I'd suggest that he grab a used copy of a textbook, something like: C++ How to program by Deitel & Deitel. He could probably get one a few versions old for $10 or something, and have a solid reference. Just make sure to avoid any of those learn C++ in 24 hours kind of books. I haven't ever seen one of those that's worth anything.
 
Back
Top