Heya,

I'm trying to slim down some code by using a for loop, but the only way I can do it is by building a variable string to put in the format bit.

for (n=0; n<13; n++)
	{	
		strTmp.Format("%ld", pFrame->m_pAppWnd->state.Variable); strBuf.Replace(saveloadtypes[n], strTmp);
	}

pFrame->m_pAppWnd->state.Variable fetches strings from another .cpp file. Variable can be whatever - Width, Height, Colour. In it's form above, it all works fine. However when it outputs, it just outputs the same thing because the Variable is the same.

My question is, how do I make the variable change? I want the variable to be whatever saveloadtypes[n] is.

Thanks!

Recommended Answers

All 6 Replies

This is typically where polymorphism is used, but without an idea of the design of your objects, I can't really offer much more than that hint.

I'm limited as to what I can give you (security reasons), however I can give you the following line:

state.Variable = RegGetInt("Name");

pFrame->m_pAppWnd->state.Variable
That line above goes off to the line above that, fetches whatever the variable is, and then copies it through. There's quite a list of state.Variables, hence why I need the for loop to automatically change the variable of pFrame->m_pAppWnd->state.Variable.

Hope that helps some!

Basically what I want to do is create a string and echo that string in the for loop in strTmp.Format

>I'm limited as to what I can give you (security reasons)
Hehe, the Top Secret Addendum to the C++ Language ;)

It seems nobody can tell you anything with unknown types of state.Variable and saveloadtypes array or strTmp and strBuf scopes or correct pFrame and m_pAppWnd pointer values...

There are lots of possibilities to get troubles in this code snippet.
Regrettably I can't enumerate all ones here (security reasons)...

commented: lmao @ last line +1

>I'm limited as to what I can give you (security reasons)
Hehe, the Top Secret Addendum to the C++ Language ;)

It seems nobody can tell you anything with unknown types of state.Variable and saveloadtypes array or strTmp and strBuf scopes or correct pFrame and m_pAppWnd pointer values...

There are lots of possibilities to get troubles in this code snippet.
Regrettably I can't enumerate all ones here (security reasons)...

Well, luckily I'm not some noob that thinks his Hello World project is top secret ;) This project gets served to 150,000 people and we don't want cheaters so yeah...

I'll give you as much as I can below:

So, State.h:

struct state_struct 
{
//Long list of variables, example:
int Variable;
}
class CState : public CObject
{
public:
	CState();
	void LoadSettings();
	state_struct state;
	int RegGetInt(CString key);
};

State.cpp:

CState::LoadSettings()
{
//List of pre-set variables.
state.Variable    = RegGetInt("Variable");
CState::RegGetString(CString key)
{
	CRegString myStr(regPath + key);
	return (CString)myStr;
}
}

Fetcher.h:

class Fetcher
{
	public:
		Fetcher();
		~Fetcher();
	int SaveValues();
};

Fetcher.cpp:

extern CMainFrame *pFrame;
char *savetypes[] = {
	"Variable1", "Variable2", "Variable3"};
int n, result=0;
CString tplFp = "Stuff";

Fetcher::SaveValues()
{
	FILE *file;
	CString strBuf, strTmp;
		strBuf.Format("%s", tplFp);
		for (n=0; n<13; n++)
		{	
			strTmp.Format("%ld", pFrame->m_pAppWnd->state.Variable); strBuf.Replace(saveloadtypes[n], strTmp);
		}
		
	fputs(strBuf, file);
	fclose(file);
	return 1;
}

I know I've missed a bit here and there but you should be able to get the point now, I hope. ;]

Ultimately I want something like this (it fails, I know):

for (n=0; n<13; n++)
		{	
			onetwo = "pFrame->m_pAppWnd->state.";
			strTmp.Format("%ld", cout << onetwo << saveloadtypes[n]); strBuf.Replace(saveloadtypes[n], strTmp);
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.