So I have had an idea for a program to create a text / console output game. I wanted to make the game solely in the console output using cout
but had a different idea.
I understand that a program can open a file, read the information, edit / manipulate the information and output that information into a different file. So something like this:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void fileWrite(int Health); // Prototype for fileWrite
int main()
{
string inFile = "inFile.txt";
string outFile = "outFile.txt";
int Health = 100;
ifstream fin;
ofstream fout;
fin.open("inFile.txt");
fout.open("outFile.txt);
while (!fin.oef())
{
fin >> Health;
fileWrite(Health);
}
fin.close();
fout.close();
return 0;
}
void fileWrite(int Health)
{
fout << setw(10) << left << "Health" << endl;
fout << setw(10) << left << Health << endl;
}
So let's say inFile.txt
simply has 50
in the file. Like so:
50
In theory, the file should be read, health
is now set to 50
, the function fileWrite
is called and the output should look like this in the outFile.txt
Health:
50
So what I am wanting to do is make it so that say instead of reading in a file for the health, could I modify Health
multiple times during the duration of the program and call fileWrite
? Say that health pertains to the main character, and I create a variable named potion
and it does +20 towards health, could I open the file. See that the character has 50
health, output the information into the outFile.txt
then take potion
which would increase the health to 70
and reprint the file?
The reason I am trying to find a way to do this is so that the user can use the console for inputting commands, and have the .txt file show the results of the commands.
Can a function
be created to handle the printing and be called multiple times in main?
Aucun commentaire:
Enregistrer un commentaire