I am pretty new to programming and have started an online course for C++. In the course I am starting to create a basic game where the user tries to guess a random number in the console. I am trying to store the best score in a text file called "best_score.txt".However, whenever I test my program I keep getting large negative numbers in the text file (e.g -858993460) . I've done a lot of research trying to fix the problem but had no luck, any help would be appreciated thank you. here is my code:
#include <iostream>
#include <cmath>
#include <float.h>
#include <climits>
#include <string>
#include <istream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <array>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::array;
using std::ifstream;
using std::ofstream;
void print_vector(vector<int> vector)
{
for (int i = 0; i < vector.size(); i++) {
std::cout << vector[i] << "\t";
}
std::cout << '\n';
}
void play_game()
{
vector<int> guesses;
int count = 0;
int random = rand() % 251;
cout << random << endl;
cout << "guess a number: ";
while (true) {
int guess;
cin >> guess;
count++;
guesses.push_back(guess);
if (guess == random) {
cout << "You Win!!!!\n";
break;
}
else if (guess < random) {
cout << "Too low\n";
}
else if (guess > random) {
cout << "Too high\n";
}
}
ifstream input("best_score.txt");
if (!input.is_open()) {
cout << "Unable to read file\n";
return;
}
int best_score;
input >> best_score;
ofstream output("best_score.txt");
if (count < best_score) {
output << count;
}
else {
output << best_score;
}
print_vector(guesses);
}
int main()
{
srand(time(NULL));
int choice;
do {
cout << "0. Quit\n1. Play Game\n";
cin >> choice;
switch (choice) {
case 0:
cout << "BYYEEEE\n";
break;
case 1:
play_game();
break;
}
} while (choice != 0);
}