2017-08-15 22 views
-1

这可能是一个愚蠢的问题,我对编码仍然很陌生。对于我的CS课程,我获得了有关桌面游戏基础知识的代码。当我尝试运行代码时,它只是在我的控制台中空白,我试图在main的开头打印“check”,但仍然没有打印到控制台。没有错误拿出当程序运行时C++控制台是空的

#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; 
} 
+1

如果此代码编译。禁用您的防病毒并重试。 – drescherjm

+0

可能一切都是如此之快,你无法看到。尝试添加getch();在返回0之前;在主() – Geek

+1

*** while(!infile.eof()){*** https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm

回答

1

read_board()修改你

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); 
      } 
     } 
    } 
+0

是的,固定它谢谢你,奇怪,因为这是本书给我的代码。 – KoV

+3

我以为表达式'while(!infile.eof())'[*认为是错误的*](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-考虑-错误的)。 –

+1

无法用这样一个常见的错误来回答这个问题。 @KoV,如果你从书中得到'while(!infile.eof())',你需要一本更好的书。这里列出的书籍通常被认为是很好的:https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – user4581301

0

变化cout<<"check";cout<<"check"<<endl;

没有新行增加,在你的代码中被挂在了缓冲区没有刷新您的读取功能

相关问题