Jump to content
  • Advertisement
Asokanta

How can i set local variable to global variable in C++ ?

This topic is 412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys,i created a config file and got line by line then parsed to variables.How can i set local (function) variable to global variable ? sorry for bad english.

Current code: 

 

void GetSettings()
{
	ifstream nameFileout;
	nameFileout.open("Settings.conf");
	string line;
	if (!nameFileout.is_open())
	{
	
		exit(0);
	}
	while (std::getline(nameFileout, line))
	{

		string parse = "[Zoom]-10";
		stringstream ss(parse);
		string temp = "[Zoom]";

		getline(ss, temp, ']');
		getline(ss, temp, '[');

	double Result;
	stringstream convert(temp);
	double Z2 = atof(temp.c_str());

		AllocConsole();
		freopen("CONOUT$", "w", stdout);
		std::cout << line << std::endl;
	}
	nameFileout.close();

}

Output : 

Z2 = -10

Share this post


Link to post
Share on other sites
Advertisement

You need to put the variable on top of the function, outside of it.

If you place it inside the function then the scope of that variable is the function block and goes out of scope at the end of it, therefore is not global.

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
5 hours ago, Asokanta said:

I know i need put out of function and top of the code but i don't know how.

Lol. (the complexity of your qurstion is below the complexity of the code you're dealing with)

Just copy the line where you declare that variable and paste it right before  the function (on top of it), and delete the one inside of it

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

code in the function.If i remove the function and just write double Z2 = -10 it won't getsettings from config file...

Share this post


Link to post
Share on other sites

You declare the variable outside the function, and then you use it inside by using the variable name, without declaring the type again (so if is a string, you won't write "string parse = ..." inside the function but you directly use "parse = ...").

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
Just now, MarcusAseth said:

You declare it the variable outside the function, and then you use it inside by using the variable name, without declaring the type again (so if is a string, you won't write "string parse" inside the function but you directly use "parse = ...").

Also if is you who gave me -1 on the previous reply, please remove it since it answers what you asked on the first post, so is not my fault if you didn't get it :/

 

17 minutes ago, Asokanta said:

Hello guys,i created a config file and got line by line then parsed to variables.How can i set local (function) variable to global variable ? sorry for bad english.

Current code: 

 


void GetSettings()
{
	ifstream nameFileout;
	nameFileout.open("Settings.conf");
	string line;
	if (!nameFileout.is_open())
	{
	
		exit(0);
	}
	while (std::getline(nameFileout, line))
	{

		string parse = "[Zoom]-10";
		stringstream ss(parse);
		string temp = "[Zoom]";

		getline(ss, temp, ']');
		getline(ss, temp, '[');

	double Result;
	stringstream convert(temp);
	double Z2 = atof(temp.c_str());

		AllocConsole();
		freopen("CONOUT$", "w", stdout);
		std::cout << line << std::endl;
	}
	nameFileout.close();

}

Output : 


Z2 = -10
//global variables
double X2 = 0.0f;
double Y2 = 0.0f;
// double Z2 need to be here.

i'm trying to make this but i can't remove the function and don't know how to declare without this function can u be more specify like write code ?

Share this post


Link to post
Share on other sites
Just now, rip-off said:

Can you show us your current code, after you tried making the variable global?

I'm getting settings(variables) from this code :

void GetSettings()
{
	ifstream nameFileout;
	nameFileout.open("Settings.conf");
	string line;
	if (!nameFileout.is_open())
	{
	
		exit(0);
	}
	while (std::getline(nameFileout, line))
	{

		string parse = "[Zoom]-10";
		stringstream ss(parse);
		string temp = "[Zoom]";

		getline(ss, temp, ']');
		getline(ss, temp, '[');

	double Result;
	stringstream convert(temp);
	double Z2 = atof(temp.c_str());

		AllocConsole();
		freopen("CONOUT$", "w", stdout);
		std::cout << line << std::endl;
	}
	nameFileout.close();

}

This function's output is here : 

double Z2 = -10;

 

But i wan't use this Z2 like this global variables :

//Global Variables
double rotated_y = 0.0;
double rotated_x = 0.0;
double rotate_y = 0.0;
double rotate_x = -65.0;
double zoom = 90.0;
//String Variables
string Var1 = "[Zoom]";
string null = "\0";
//Camera Variables
double X2 = 0.0f;
double Y2 = 0.0f;
// double Z2 need to be here.
//Movement Variables
double X1 = 0.0f; // ←→
double Y1 = 0.0f; //  ↕
double Z1 = 0.0f; // 
double speed = 1.0f;
//Colission Variables
bool CanMoveToLeft = true;
bool CanMoveToRight = true;
bool CanMoveToUp = true;
bool CanMoveToDown = true;

 

Share this post


Link to post
Share on other sites

I've hidden some negative comments that weren't helping. Please, be respectful and understanding of one another.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!