I am trying to create a simple counter that keeps the count of all the Competitive Programming Problems that I solve. The issue with it is that I am trying to save the value to a TXT so then I just keep adding to that but for some reason, it is not working. It sometimes gives me random numbers or it adds a number to the side instead of replacing. For example, I start with 0 and if I add 1 it will be 01 and so on.
#include <iostream>
#include <fstream>
using namespace std;
int score;
int change;
int main(){
ifstream input("score.txt");
input >> score;
ofstream output("score.txt");
cout << "So far you've solved " << score << " problems!\n";
cout << "Press 1 to add or 0 to substract\n";
cin >> change;
if (change == 1)
output << score++;
else
output << score--;
cout << "Your current score is: " << score << "\n";
output << score;
}
Thanks!