Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to increment the previous value of a , after every time when i compile the program so what should i write the code in the function. I am unable to find. can you help me in this?
C++
class x
{
       private:
        int a,temp;
        fstream f;
       public:
               x()
               {
                     a=0;
               }
               void incr()
               {
                    f.open("data.txt",ios::out);
                    f<<a;
                    f.close();
                    f.open("data.txt",ios::in);
                    f>>a;
                    temp=a+1;
                    f.close();
               }
};

       int main()
       {
            x o;
            o.incr();
       }
Posted
Updated 19-Jun-12 4:54am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jun-12 10:54am    
If you keep doing your trial-and-error attempts, without any understanding of what you are doing and even what do you want, you won't go far...
--SA
nv3 19-Jun-12 11:30am    
Have you confused "compile" and "run" in the title of you message? Then it would make sense.

Why would you want to increment a variable at compile time? Do you perhaps want to build a version number generator? Please let us know what you want to achieve, then we might be able to help you.
Ashutosh_g 19-Jun-12 11:36am    
i mean suppose there is an unique id as a=1 given to one customer and during second run of program i want the value of a to be incremented automatically for second customer as a=2.
nv3 19-Jun-12 15:20pm    
Then why would you want to recompile the program for the second customer run? You seem to believe that every run of a program would require a new compile. That is not so. You just compile and link your program once and then run it as often as you want.

Now, to differentiate between your two customers you could ask for a command line input, or - as you proposed - increment a value that is stored in a text file.

1 solution

This is so clear that you cannot affect the compilation of some program by means of this program. The program generally has none of its source code during run-time, so all your manipulations are irrelevant, no matter what you do. By the way, how come it might be not obvious for you? Do you know what happens during run time, link time, load time and run time? This is about understanding the most basic ideas of programming, on the "infra" level.

The closest possibility is generation of the source code by some program, building it, something like that. I don't think it makes any sense. It's much better to learn the simple life cycle of programming, in very basic principles.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900