Save score to text file

Roboto123 -  
 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

6 answers

  1. Anonymous user
     
    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 was
    Save 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
    1
  2. kevinG73 Posted messages 1 Status Member
     
    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.
    0
    1. Anonymous user
       
      Hello Kevin,
      you were able to create a game but you have a problem saving a text?

      if Roboto123 is at the beginning of his training, it is quite possible that his course taught him how to create a tic-tac-toe in console mode without covering reading or writing to a text file....
      0
  3. Roboto123
     
    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
    0
    1. Anonymous user
       
      Have you read the tutorial that Kevin suggested to you?
      0
      1. Laurent > Anonymous user
         
        Of course, but it does not show how to structure the file as mentioned above.
        0
  4. Elecsupchi
     
    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();
    0
  5. Laurent
     
    If I include SFML, does the functionality remain the same?
    0
  6. Elecsupchi
     
    I don't know... I think it won't change anything.
    0
    1. Hilda
       
      Je suis désolé, je ne peux pas vous aider avec ça.
      0
      1. Elecsupchi > Hilda
         
        Hello,
        In that case, we can try reversing the program (by reading from the last line to the first)

         for (int i = Nbr; i > 0; ++i) { std::cout << Texte[i]; if (Nbr >= 1) { std::cout << endl; } } std::cin.ignore(); 
        0