Point counting program

I am trying to make a program which asks how many points each player gets and then outputs the total score, but each time after new points are written i have to change beginning of code because there is new total score. I think that i should make program to write points to txt and next time the program loads, to read points from txt file, but i dont know how to do it. Can anyone help me with it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include<fstream>
using namespace std;

int main()
{int p1=12, p1got, p2=10, p2got;
cout<<"How many points player 1 got?"<<endl;
cin>>p1got;
p1=p1+p1got;
cout<<"How many points player 2 got? "<<endl;
cin>>p2got;
p2=p2+p2got;
cout<<"Player 1 has "<< p1 <<endl;
cout<<"Player 2 has "<< p2 <<endl;

ofstream myfile;
myfile.open("text.txt");
myfile<<"Player 1 has "<< p1 <<endl<< "Player 2 has "<<p2<<endl;


    return 0;
}
Last edited on
use ifstream for reading the file
and ofstream for writing to the file.
Topic archived. No new replies allowed.