how to change the code in the program and keep the changes

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
using namespace std;
int main () {
	
	
	
	int x;
	cout<<"Enter x:\n";
	cin>>x;
	
	x=x+9;
	
	cout<<"X="<<x<<endl;
	
	
	
	
	
	system ("pause");
	return 0;
}
Last edited on
If you want to keep the change applied to variables after exiting your program you can use files so that your program's execution goes like this:

->program starts
->reading variables from file
->changing variables with program
->writing variables to file (saving)
->exiting program
Last edited on
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
Build and then run this program two or three times; observe its output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int main()
{
    const char 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).
Yes, what Peter87 said! Also thank you JLBorges for providing an example of what I was suggesting!
Topic archived. No new replies allowed.