2017-08-15 41 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;

沒有新行增加,在你的代碼中被掛在了緩衝區沒有刷新您的讀取功能

相關問題