-1

This might be a stupid question I'm still very new to coding. For my CS class I was given code for the basics of a boardgame. When I try to run the code it just comes up blank in my console, I tried to print "check" at the very beginning of main but still nothing prints to the console. No errors come up

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;

class square {
private:
    int move;
    string message;
    char symbol;
public:
    square();
    void print();
    int action();
    void set(int, char, string);
};

void print_board(square[], int, int);
void read_board(square[]);
void check_position(int &);

const int board_length = 20;

int main() {
    cout << "check";
    int current_player = 1, roll;
    int player1_position = 0, player2_position = 0;
    square the_board[board_length];
    srand(time(NULL));
    read_board(the_board);
    print_board(the_board, player1_position, 1);
    print_board(the_board, player2_position, 2);
    do {
        cout << "\n\n\nPlayer " << current_player << " type enter to roll.\n";
        cin.ignore();
        roll = 1 + (rand() % 5);
        cout << "Player " << current_player << " rolled a " << roll << ".\n";
        if (current_player == 1) {
            player1_position += roll;
            check_position(player1_position);
            player1_position += the_board[player1_position].action();
            check_position(player1_position);
        } else {
            player2_position += roll;
            check_position(player2_position);
            player2_position += the_board[player2_position].action();
            check_position(player2_position);
        }
        print_board(the_board, player1_position, 1);
        print_board(the_board, player2_position, 2);
        current_player = (current_player % 2) + 1;
    } while ((player1_position < board_length-1) && (player2_position < board_length - 1));
    current_player = (current_player % 2) + 1;
    cout << "\nPlayer " << current_player << " Wins!!!\n";
    cin.ignore();
    return 0;
}

void read_board(square b[]) {
    ifstream infile;
    infile.open("game.txt");
    int square_number, square_move;
    string square_message;
    char square_symbol;
    while (!infile.eof()) {
        infile >> square_number >> square_move >> square_symbol;
        getline(infile, square_message);
        if (square_number < board_length) {
            b[square_number].set(square_move, square_symbol, square_message);
        }
    }
}

void print_board(square b[], int player_position, int player_number) {
    for (int i=0; i < board_length; i++) {
        if (i != player_position) {
            b[i].print();
        } else {
            cout << player_number;
        }
    }
    cout << "Goal\n";
    for (int i=0; i < board_length; i++) {
        cout << "-";
    }
    cout << "\n";
}

void check_position(int &p) {
    if (p < 0) {
        p = 0;
    }
    if (p >= board_length) {
        p = board_length - 1;
    }
}

square::square() {
    symbol = ' ';
    move = 0;
    message = "";
}

int square::action() {
    cout << message << endl;
    return move;
}

void square::print() {
    cout << symbol;
}

void square::set (int m, char s, string a_message) {
    move = m;
    symbol = s;
    message = a_message;
}
KoV
  • 33
  • 4
  • 1
    If this code compiles. Disable your antivirus and try again. – drescherjm Aug 15 '17 at 21:53
  • May be everything is so fast you are not able to see. try adding getch(); before return 0; in main() – Geek Aug 15 '17 at 21:55
  • 1
    ***while (!infile.eof()) {*** https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Aug 15 '17 at 21:56
  • Problems with your read_board() function would be one obvious issue. –  Aug 15 '17 at 21:56
  • I removed the read_board() function and it compiles correctly now it seems while (!infile.eof()) { was the issue. – KoV Aug 15 '17 at 22:04

2 Answers2

1

Modify you read_board() to

   void read_board(square b[]) {
        ifstream infile;
        infile.open("game.txt");
        int square_number, square_move;
        string square_message;
        char square_symbol;
        while (infile >> square_number >> square_move >> square_symbol) {
            getline(infile, square_message);
            if (square_number < board_length) {
                b[square_number].set(square_move, square_symbol, quare_message);
            }
        }
    }
Geek
  • 273
  • 5
  • 19
  • Yep that fixed it thank you, odd since this was the code given to me by the book. – KoV Aug 15 '17 at 22:06
  • 3
    I thought the expression `while (!infile.eof())` was [*considered wrong*](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Thomas Matthews Aug 15 '17 at 22:08
  • 1
    Can't upvote this answer with such a common bug in it. @KoV , if you got `while (!infile.eof())` out of a book, you need a better book. Here's a list of books generally accepted as good: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – user4581301 Aug 15 '17 at 22:58
  • Need to move the `getline` into the `while` as well. – user4581301 Aug 16 '17 at 15:56
0

Change cout<<"check"; to cout<<"check"<<endl;

Without the new line added, the out buffer is not flushed before your code gets hung in your read_board function

Austin Yates
  • 108
  • 9