display scores on notepad from c++

Hi. I'm new here and I have a problem on my program. I want to display the scores on notepad. I can display it but the problem is every time I play the game the previous score got erased and replaced by the new one. Thanks in advance :))

Here's my code.
cout<<" Enter your name: ";
cin>>a;
cout<<endl<<endl;
cout<<" Your name is "<<a<<" and your score is "<<right<<endl<<endl;
ofstream MyFile ("score.txt");
MyFile << a <<" "<<right;
MyFile.close ();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

int main()
{
    const std::string score_file = "score.txt" ;

    // ideally, put the full paths here (prevent spoofing etc)
    const std::string path_to_notepad = "notepad.exe" ;

    std::string name = "Cassie" ;
    int score = 87 ;

    // ....

    { 
        // open file to append the score at the end
        // https://stdcxx.apache.org/doc/stdlibug/30-3.html#3031
        std::ofstream( score_file, std::ios::app ) << name << ": " << score << '\n' ; 
    }

    // http://www.cplusplus.com/reference/cstdlib/system/
    const std::string command = '"' + path_to_notepad + "\" " + score_file ;
    std::system( command.c_str() ) ;
}
Thanks but what if I want the player to input his/her name and the player score will be counted by my program? Thanks. :))
> what if I want the player to input his/her name

You already had that code in your original post.
1
2
> cout<<" Enter your name: ";
> cin>>a;


The name can contain white space, so you should use std::getline() instead.
1
2
3
4
5
6
7
std::cout << "please enter your name: " ;
std::string name  ;

// http://www.cplusplus.com/reference/string/string/getline/
std::getline( std::cin, name ) ;

// ... 
Thank you very much :))
Topic archived. No new replies allowed.