C++ C++;程序运行时,控制台为空

C++ C++;程序运行时,控制台为空,c++,C++,这可能是一个愚蠢的问题,我对编码还是很陌生的。在我的CS课程中,我得到了一个基本的棋盘游戏代码。当我尝试运行代码时,它在我的控制台中显示为空白,我尝试在main的最开始处打印“check”,但仍然没有打印到控制台。没有出现错误 #include <iostream> #include <string> #include <fstream> #include <ctime> #include <cstdlib> using namespa

这可能是一个愚蠢的问题,我对编码还是很陌生的。在我的CS课程中,我得到了一个基本的棋盘游戏代码。当我尝试运行代码时,它在我的控制台中显示为空白,我尝试在main的最开始处打印“check”,但仍然没有打印到控制台。没有出现错误

#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
阶级广场{
私人:
int-move;
字符串消息;
字符符号;
公众:
正方形();
作废打印();
int action();
无效集(int、char、string);
};
无效打印板(正方形[],整数,整数);
无效读板(方形[]);
无效检查位置(int&);
const int BOUND_长度=20;
int main(){
无法将您的读板()修改为

void read_板(方形b[]){
河流充填;
infle.open(“game.txt”);
整数平方数,平方移动;
串方消息;
字符方格符号;
同时(填充>>方形编号>>方形移动>>方形符号){
getline(内嵌、方形消息);
if(方号<板长){
b[方形编号]。设置(方形移动、方形符号、方形消息);
}
}
}

更改
如果此代码已编译。请禁用您的防病毒软件,然后重试。可能是因为您看不到的速度太快了。请尝试在main()中添加getch();然后返回0;,而(!infle.eof()){您的read_board()函数存在问题。我删除了read_board()函数,它现在可以正确编译了,而(!infle.eof()){似乎是问题所在。是的,修复了它,谢谢,奇怪的是,这是书中给我的代码。我认为表达式
,而(!infle.eof())
是。如果你有
而(!infle.eof())
的话,这个答案中有这么一个常见的错误,那就不能升级了。@KoV,如果你有
而(!inflee.eof())
从一本书中,你需要一本更好的书。下面是一个公认为好的书的列表:也需要将
getline
移动到
while
中。
   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);
            }
        }
    }