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
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;

 

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

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

Advertisement
46 minutes ago, Asokanta said:

How can i set local (function) variable to global variable ?

You cant set a local to global it needs to be returned

So:


void GetSettings()
{
	ifstream nameFileout;
	nameFileout.open("Settings.conf");
	string line;
    //Declare the aviable above
    double Z2;
    //Make a get function here:
    double GetTheVariable(){
      return Z2;
    }
  
	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);
    //Use the variable here as a pointer
	Z2 = atof(temp.c_str());

		AllocConsole();
		freopen("CONOUT$", "w", stdout);
		std::cout << line << std::endl;
	}
	nameFileout.close();
  
//Use something like this where you need it
  double A2 = GetSettings.GetTheVariable(); //Should return: atof(temp.c_str())

You could have found this by searching the net for 10 seconds.

http://www.cplusplus.com/forum/beginner/2097/

Advertisement

In your updated code, you have not declared a global variable "Z2". It is not clear to me whether you really want to introduce a new global variable, or whether you want to update one of your existing variables from the configuration settings.

In general, setting a global variable is done like so:


#include <iostream>

int global = 13;

void changeValue()
{
    global = 42;
    std::cout << "Changed value: " << global << std::endl;
}

int main()
{
    std::cout << "Starting value: " << global << std::endl;
    changeValue();
    std::cout << "Updated value: " << global << std::endl;
}

If you want to declare a new variable, then you need to add it to those globals like you have done for the others. The other thing to be careful of is to not declare a local variable with the same name:


#include <iostream>

int confusing = 13;

void changeValue()
{
    int confusing = 42;
    std::cout << "Changed value: " << confusing << std::endl;
}

int main()
{
    std::cout << "Starting value: " << confusing << std::endl;
    changeValue();
    std::cout << "Updated value: " << confusing << std::endl;
}

If you run this, you'll notice that the global has not been updated. This is because a new variable with the same name was introduced in "changeValue".

I've hidden some more unhelpful replies.

To Asokanta, this is not a real time chat. Please have patience and wait for a reply. Also understand that we're not here to write your code for you, but to help you learn how to write your own code.

To others, please bear in mind that Asokanta is relatively new to this and what might seem like a straightforward suggestion could be confusing or not very obvious.

All future replies should be respectful and on topic. Failure to follow these instructions will result in moderation action being taken.

There is no member write code for me i just wan't be more specify like example code :D and i will try this codes 

1 hour ago, Scouting Ninja said:

You could have found this by searching the net for 10 seconds.

http://www.cplusplus.com/forum/beginner/2097/

i tried google and i just found other private forums but i didin't understood and i created a topic from here.

Thank you guys helping me anyway :D.  

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

Thank you guys it's working now.Topic can be locked.

The last code is here : 


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

		std::string s = line;
		std::string delimiter = "[Zoom]";

		size_t pos = 0;
		std::string token;
		while ((pos = s.find(delimiter)) != std::string::npos) {
			token = s.substr(0, pos);
			std::cout << token << std::endl;
			s.erase(0, pos + delimiter.length());

			stringstream convert(s);
			Z2 = atof(s.c_str());
		}


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

}

Output : 

Z2 = -10;

Settings.conf : 

[Zoom]-10

int main(int argc, char** argv) {



	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);

	//Create the window
	glutCreateWindow("Window");
	initRendering();

	GetSettings();
	AllocConsole();
	freopen("CONOUT$", "w", stdout);
	std::cout << Z2 << std::endl;

	//Set handler functions
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);

	glutReshapeFunc(handleResize);
	glutSpecialFunc(specialKeys);

	glutMainLoop();

}

 

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

This topic is closed to new replies.

Advertisement