Advertisement

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

Started by July 30, 2017 10:38 AM
22 comments, last by MarcusAseth 7 years, 4 months ago

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
Asokanta™ Working for freelance own project.If you want help or get information just send private message. ______________________________________________________________________________

All the links of Asokanta™

nick-name.ru/nickname/Asokanta/ Steamcommunity.com/id/Asokanta Github.com/Asokanta(Empty)

fb.me/Asokanta t.co 

AND OTHER LINKS BELONG TO ASOKANTA IS HERE

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.

Advertisement

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

Asokanta™ Working for freelance own project.If you want help or get information just send private message. ______________________________________________________________________________

All the links of Asokanta™

nick-name.ru/nickname/Asokanta/ Steamcommunity.com/id/Asokanta Github.com/Asokanta(Empty)

fb.me/Asokanta t.co 

AND OTHER LINKS BELONG TO ASOKANTA IS HERE

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

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

Asokanta™ Working for freelance own project.If you want help or get information just send private message. ______________________________________________________________________________

All the links of Asokanta™

nick-name.ru/nickname/Asokanta/ Steamcommunity.com/id/Asokanta Github.com/Asokanta(Empty)

fb.me/Asokanta t.co 

AND OTHER LINKS BELONG TO ASOKANTA IS HERE

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 = ...").

Advertisement
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 ?

Asokanta™ Working for freelance own project.If you want help or get information just send private message. ______________________________________________________________________________

All the links of Asokanta™

nick-name.ru/nickname/Asokanta/ Steamcommunity.com/id/Asokanta Github.com/Asokanta(Empty)

fb.me/Asokanta t.co 

AND OTHER LINKS BELONG TO ASOKANTA IS HERE

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

This topic is closed to new replies.

Advertisement