I'm learning c ++ for over a month and I'm progressing. I'm wondering if it's possible to change the code within the program itself, and then keep that change. For example, to change the variable in the program x = 5 in x = x + 9 and that when I start again, the x be equal to x = x + 9, or you 14,and not x = 5.What should I add in code to succeed?By the way, I apologize if there are any mistakes in my text, I still learn English
Maybe you don't understand me.I want to change variable and keep changes without change code.When i start my program and enter datas,I want these changes to be kept, and not when I'm going into the program again to start everything, but to change the program variable instead of changing the code
#include <iostream>
#include <fstream>
int main()
{
constchar file_name[] = "my_variable.txt" ;
int x ;
// on start up, try to read the value of x from the file
// if attempted input fails, set it to a default value of 5
if( std::ifstream{file_name} >> x ) ; // ok, do nothing more; x was read from the file
else x = 5 ; // input failed; set it to five
std::cout << "initial value of x: " << x << '\n' ;
x = x + 9 ; // change the value of x
std::cout << "changed value of x: " << x << '\n' ;
// on exit, write the value of x into the file
std::ofstream(file_name) << x << '\n' ;
}
It's not possible. If you want your program to remember something from one run to another you need to store that information somewhere outside the program (e.g. in a file).