1

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);
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
harry lee
  • 17
  • 2
  • Simply your program logic must ensure your code doesn't overflow integer type range. If for some reasons you need very very large integers then there is a library which extends size of integer. – Marek R Feb 15 '21 at 16:57
  • 1
    This would be a great time to learn to use a debugger and go through the code line by line. You’ll see what the values are and where. You don’t do any error checking, so if reading to `best_score` doesn’t work it may have whatever value, possibly a very negative one which would cause it to be smaller than `count`, for example. – Sami Kuhmonen Feb 15 '21 at 16:57
  • Please fix the indentation, it is so misleading as to be analysis-sabotage. – Yunnosch Feb 15 '21 at 16:57
  • 2
    -858993460 == 0xcccccccc, used in MSVC++ to diagnose bugs. You forgot to assign a value to the variable. – Hans Passant Feb 15 '21 at 16:58
  • 1
    Convert the numbers to hex, it is 0xcccccccc. Then read https://stackoverflow.com/questions/17644418/why-is-the-stack-filled-with-0xcccccccc, it might help you find the bug. – Werner Henze Feb 15 '21 at 16:59

2 Answers2

1

You didn't handled errors well, so when file was none existing or had invalid content your variable best_score ends with undefined value. If this is negative then your logic is unable to fix that.

You should extract a functions to handle that:

int load_score()
{
    std::ifstream in("best_score.txt");
    int score;
    if (in >> score) return score;
    return 99999;
}

void save_score(int score)
{
    std::ofstream output("best_score.txt");
    output << score;
}

void update_score(int newScore)
{
    auto oldScore = load_score();
    if (oldScore > newScore) {
        save_score(newScore);
    }
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
0

I suspect you're starting off with the file best_score.txt being empty. That means these lines:

int best_score;
input >> best_score;

won't be able to read anything. In C++11 it's supposed to write 0 to the variable, but prior to that it just left the variable alone. If that's what's happening, you'll just get whatever happens to be in best_score.

Even if it does write 0 to the variable, that's still a problem because no one will be able to find the number in less than 0 guesses.

Try initializing best_score.txt with a number and see if that fixes the problem. Either way you should handle the case of best_score.txt being empty by always writing the new score to the file in that situation.

Willis Blackburn
  • 8,068
  • 19
  • 36