Save score to text file
Roboto123
-
Elecsupchi -
Elecsupchi -
Hello, I am creating the Tic-tac-toe game, and I would like to save the scores of the winning player and the losing player in a text file so that when I enter the menu of my game, there will be the option to view the Scores, and when we press the option, we can see the previous scores.
Example of txt file
Toto Mateo
1 0
0 1
Example of txt file
Toto Mateo
1 0
0 1
6 answers
-
The formatting of the file is not specifically a problem of writing to the file; it would be the same for outputting to the console.
But your question wasSave score to text file
, so we directed you to the file writing part...
Anyway.
I suppose you have one or more variables that store the names of the players. Well, you need to use them to write the first line.
I also assume you have one or more variables that store the results of the games; well, you need to use them for the following lines.
--
When I was little, the Dead Sea was only sick.
George Burns -
Hello,
you were able to create a game but you have a problem saving text?
You can start reading:
https://openclassrooms.com/fr/courses/1894236-programmez-avec-le-langage-c/1896398-lisez-et-modifiez-des-fichiers
It's not super complicated. -
The problem lies mainly in the structure of the text file so that it is saved in such a way.
Toto Mateo
1 0
0 1 -
Hello!
I have a program to propose to you, I'm not sure it's the most optimized possible, but it works for me!
#include <fstream> // <-- For reading and writing to the file "Score.txt"
#include <string> // <-- To declare the "string" variables
#include <iostream> // <-- To enable the console
using namespace std; // <-- IMPORTANT
int main()
{
int score1 = 1; // <-- The "int" variables containing the scores
int score2 = 0;
std::string joueur1 = "Toto"; // <-- Your "string" variables (character strings) containing the usernames
std::string joueur2 = "Mateo";
std::string Texte[100]; // <-- Creation of an array containing a certain number of values
int Prenoms = 0;
int Nbr = 0;
// <-- The code for your game
string const NomFichier = "FILE LOCATION"; //Path to the file
ifstream Score("FILE LOCATION"); //Opening the file in "read" mode
if(Score)
{
std::string ligne;
while (getline(Score, ligne))
{
if (ligne == joueur1 + " " + joueur2)
{
Prenoms = 1;
}
Nbr = Nbr + 1;
Texte[Nbr] = ligne;
}
}
else
{
std::cout << "ERROR: Unable to open file!" << std::endl;
}
ofstream ScoreE(NomFichier.c_str()); //Opening the file in "write" mode
if (ScoreE)
{
if (Prenoms == 1)
{
for (int i = 1; i <= Nbr; ++i)
{
ScoreE << Texte[i] << endl;
}
ScoreE << score1 << " " << score2;
}
else
{
for (int i = 1; i <= Nbr; ++i)
{
ScoreE << Texte[i];
if (Nbr >= 1)
{
ScoreE << endl;
}
}
ScoreE << joueur1 << " " << joueur2 << endl << score1 << " " << score2;
}
}
else
{
std::cout << "ERROR: Unable to open file!" << std::endl;
}
}
And if you want to display previous scores:
for (int i = 1; i <= Nbr; ++i)
{
std::cout << Texte[i];
if (Nbr >= 1)
{
std::cout << endl;
}
}
std::cin.ignore(); -
-