LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2004, 07:39 AM   #1
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Rep: Reputation: 30
C++ converting strings to variables


Hello,
I am in the process of writing a aLife simulation program and have come unstuck, I would be very grateful for any help.

I have 900 variables all called Shape1, Shape2, Shape3 etc. and I would like to be able to access them in a simple for loop. (I should point out that the variable names are fixed so any suggestions about doing something like:
Code:
double shape[900];
are entirely unhelpful I'm afraid!)

I would like to be able to do something like

Code:
for (int i = 0; i < 900; i++)
{
  "Shape" + i = someValue;
}
if anyone has any idea how to do this I would be most grateful!!
 
Old 05-30-2004, 08:04 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Code:
int main()
{
	 double shape[900];
	 double someValue = 1.0;

	 for (int i = 0; i < 900; i++)
	 {
		  someValue = someValue + i;  // Just an example value
		  shape[i] = someValue;
	 }
}
 
Old 05-30-2004, 09:08 AM   #3
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Original Poster
Rep: Reputation: 30
I think you misunderstood me, I can't use an array for the Shape objects. I'm not sure if what I want can be done
 
Old 05-30-2004, 09:37 AM   #4
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
AFAIK you can not do that.
Why do you want to do that? You can do something better with linked list or a hash map for some specific purposes.
 
Old 05-30-2004, 09:53 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Quote:
I can't use an array for the Shape objects.
Weird...Why not?
Quote:
I'm not sure if what I want can be done
Well, why not tell us here what it is that you want.
Until now we only know what you "can not", or don;t want....
 
Old 05-30-2004, 09:57 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
You could do this:
Code:
int main()
{
	 double shape[900];
	 double someValue = 1.0;

	 for (int i = 0; i < 900; i++)
	 {
		  someValue = someValue + i;  // Just an example value
		  *(shape + i) = someValue;
	 }
}
...but that still uses an array. So I guess you don't want this either.
 
Old 05-30-2004, 11:42 AM   #7
Ma3oiS
LQ Newbie
 
Registered: May 2004
Posts: 12

Rep: Reputation: 0
Hi,

if you use C++ you can create function:
Code:
int & getShape(int no) {
   switch(no) {
      case 0: return Shape0;
      case 1: return Shape1;
      case 2: return Shape2;
      // ....
   }
   return Shape0; // just to return something
}
and now you can do:
Code:
for (i=0; i<900; i++) {
   getShape(i) = someValue + i;
}
Ma3oiS
 
Old 05-30-2004, 11:46 AM   #8
Ma3oiS
LQ Newbie
 
Registered: May 2004
Posts: 12

Rep: Reputation: 0
Of course it should be "double & getShape(int no) { ... }"
and not "int & getShape(int no) { ... }"
 
Old 05-30-2004, 12:12 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
No, to my knowledge, what you want cannot be done like you want it to be done. You can't create a symbolic name by concatenating pieces of information together (like "Shape" + i ). It just doesn't work that way, and that's why everyone (including myself) will suggest using an array to accomplish the task. When you have X number of like-named variables, it is much more convenient to put them in an array and use an array syntax than to specify them by name. For large values of X, your program becomes completely unamageable.

However, you didn't make the post to listen to us extol the virtues of arrays or pointers. I'm assuming you've either:
1. Taken the source code from someone else and are modifying it in their style (and they didn't use an array)
2. You just don't like arrays for whatever reason

That being the case, you could follow Ma3oiS's lead if you use C++. If you're using C, you can do the setup below. This will work in both C and C++, but Ma3oiS has a much cleaner implementation by using objects.

NOTE: I don't always send code I post through a compiler. So some of the syntax may be incorrect (especially regarding the declaration of a constant array of structs). It should be trivial to correct it if there is a problem.

Code:
// define a max number of characters to store the names in.
// 5 chars for "string", 3 chars for "0-900", 1 char for NULL, and
// an extra thrown in for good measure (allowing for things to expand).
#define MAX_SYMBOL_NAME_LENGTH                                       10

#define ENTRIES_IN_LOOKUP_TABLE                                     901

struct lookup_entry_struct
{
  char symbol_name[MAX_SYMBOL_NAME_LENGTH];
  double *p_Shape;
};

typedef struct lookup_entry_struct lookup_entry;

...

double Shape0;
double Shape1;
...
double Shape900;

lookup_entry[] = { { "Shape0",  &Shape0 },
                   { "Shape1", &Shape1 },
                   { "Shape2", &Shape2 },
                   ...
                   { "Shape900",  &Shape900 } };

...

double *GetShape( char *shape_name )
{
  int lookup_index;

  lookup_index = 0;
  while ( ( lookup_index < ENTRIES_IN_LOOKUP_TABLE ) &&
               ( strcmp( shape_name, lookup_entry[lookup_index] ) != 0 )
    lookup_index += 1;

  if( lookup_index == ENTRIES_IN_LOOKUP_TABLE )
    return NULL;
  else
    return lookup_entry[lookup_index].p_Shape;
}

...

// Somewhere in main() or support functions...
int shape_index;
char shape_name[MAX_SYMBOL_NAME_LENGTH];
double *p_target_shape;

for( shape_index = 0; shape_index < ENTRIES_IN_LOOKUP_TABLE; shape_index++ )
{
  memset( shape_name, 0, MAX_SYMBOL_NAME_LENGTH );
  sprintf( (void *)shape_name, "Shape%d", shape_index );
  p_target_shape = GetShape( shape_name );
  *p_target_shape = some_value;
}

Last edited by Dark_Helmet; 05-30-2004 at 12:17 PM.
 
Old 05-31-2004, 04:06 AM   #10
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Original Poster
Rep: Reputation: 30
Hey guys thanks for all the help,

believe me I would have liked to use an array (or a list, or vector for that matter) if at all possible but I'm working with objects that have been added onto a Form. If you could add objects dynamically to a Form at run-time then it would be possible to create a vector to hold Shape objects and then push them back as they are created. something like
Code:
vector<double> shape_list1;
vector<double> shape_list2;

for (int i = 0; i < 900; i++)
{
  create Shape;
  shape_list.push_back(Shape);
}
From your suggestions it has become clear that at some point I'm going to have to write out all the shape objects one by one, something I was hoping to avoid as it looks pretty horrible but there you go . Thanks for all the help, it has been much appreciated

C++ does my nut with things like this, I mean I can do it in JavaScript for heavens sake!!!

Last edited by dhbiker; 05-31-2004 at 04:07 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
C++ -> Converting strings to char* Dark Carnival Programming 1 01-25-2005 04:17 PM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 04:57 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration