Alright, so i'm currently making a simple console program which I need to be able to store information from each time I run it. As far as I can understand - the best and easiest way to do this is to have the program store the information in a .txt file.
Now, a few things:
1. I want to be able to store both numbers, aswell as characters (in the form of names) in the same text document. Later on I wish to recall these values and use them in my program, either in multiplications or in lines of text.
Now as far as I can understand, it's not possible to, in any way "Declare" a given location to a value when storing it in a text document in such a way that it's easy to get ahold of later, am I correct?
This is an example of what I'd like my program to do;
"Input your name:" I'll input John Travolta
"Input your age:" I'll input 40
it then stores this information in a text document, then later I can start the program again and per say, I can run a command to find out what half of my age is, the program then finds my age, stored in the text document, divides it by two and returns that.
Is this possible?
I can easily make a program that stores this information in a text file, like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main()
{
string tango;
ofstream myfile;
myfile.open ("example.txt");
for (int a = 0; a<20; a++)
{
cout << "What do you want to write in your .txt file?";
cin >> tango;
myfile << tango << endl;
}
myfile.close();
return 0;
}
|
And I can write 20 things on here. I can also find all these information and store each line in a string, now if I organize my program carefully it'd still be possible for me to organize such that "Name" is always stored in string[2], I could in other words create a program like this;
1 2 3 4 5 6 7 8 9 10
|
string info[20];
int a = 1;
while (! myfile.eof() )
{
getline (myfile,line);
info[a] = line;
a++;
}
myfile.close();
}
|
All the information would be stored in the string a,and knowing that the third question my program asked was per say the persons age, I could pick this up later in my program by running the function that prints everything to a string called info and returns it to where I am in the program, and then do:
1 2
|
int b = info[3]/2;
cout << "half your age is: " << b;
|
This brings with it a few other problems of course - is there any way I can store both names, and numbers in the same type of file? So that I can simply run a function and get all my info returned like I want to?
And another thing - let's say the person using this program just had his birthday, he'd like to go in and change his age - but only his age, he doesn't want to rewrite all his information again, is there any way I can browse my way to this specific tidbit of information in my .txt document and change only that?
Hope this didn't get too long, thanks in advance to any help given.