3 Replies - 419 Views - Last Post: 21 August 2011 - 07:45 PM Rate Topic: -----

#1 Jaimok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-August 11

Query regarding C++ Structures and Variable Variables

Posted 21 August 2011 - 06:51 PM

I'm quite new to programming, and even newer to C++, having started out on PHP, so please forgive my backwardness! From what I've read, using variables in the names of other variables doesn't quite work in C++ as it does in PHP. But is there a way round this, maybe using pointers, which I confess I don't fully grasp yet, or some other means? Here's my problem:

Say I have some code for a structure as follows:


struct golfholes{
	string nickname;
	int par;
	string difficulty;
} h1, h2, h3;

h1.nickname = "Giant's Elbow";
h3.par = 5;




That's all fine. But then suppose I want to use a separate int variable to pick out the holes, e.g.

int x = 2;

h[x].difficulty = "easy";
h(x).par = 4;
h/x/.nickname = "The Swan";




Of course, none of these work, but you can see what I'm trying to do. After much searching, I even tried using some sort of botched concatenation thus:


int x = 2;
std::stringstream sstm;
sstm << "h" << x;
string result = sstm.str();

result.par = 4;




Though cout<<result was good enough to output "h2" to the screen, I didn't really expect result.par to work.

I have managed to bypass the problem by using several one-dimensional arrays (e.g. diff[3] = "easy", nickname[3] = "The Swan" etc.), but all the advice I've read for the type of project I'm working on seems to be to use structures or classes, which do look much tidier.

Any help, particularly gently couched, much appreciated! Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Query regarding C++ Structures and Variable Variables

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,407
  • Joined: 13-June 08

Re: Query regarding C++ Structures and Variable Variables

Posted 21 August 2011 - 06:56 PM

Or, how about, an array of structures.


golfholes golf[5];
int x = 2;
golf[x].difficulty = "Easy";




Though the way you want can work too if you set up 'function pointers' tied to the text h2. but you're still just matching h2 and substituting it for a pointer to h2.

This post has been edited by ccubed: 21 August 2011 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: Query regarding C++ Structures and Variable Variables

Posted 21 August 2011 - 07:15 PM

The
} h1, h2, h3;
isn't making an array, but three structure instances named similar.
if you declare an array by
} h[3];
then you can use a variable or a constant to access the "elements" of the structure array.

Variable types are quite forced in C/C++, so a "string" will not have a "par" variable that a "golfholes" would.
Was This Post Helpful? 1
  • +
  • -

#4 Jaimok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-August 11

Re: Query regarding C++ Structures and Variable Variables

Posted 21 August 2011 - 07:45 PM

Thank you both! I had tried a combination of array and structure, but got hit with so many error messages that I was put off. Turns out that writing h[3] instead of h1, h2 etc. made all the difference. Up and running - thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1