从文本文件中读取空格

时间:2015-08-24 16:40:01

标签: c++ xcode6 fstream

我正在尝试从某个文件中读取空格字符并将它们存储在数组位置,但是当我运行代码时,我一直得到这个帖子:thread 1 exc_bad_access(code = exc_i386_gpflt)。我不知道自己错了什么。使用Xcode 6.4 btw。

#include <iostream>
#include <string>
#include<fstream>
#include <ctype.h>
using namespace std;
const int COLS = 8;
const int ROWS = 8;
//Function Prototypes
void displayTop();
void displayNumColumn();
void displayNumRow();
int displayMenu();
void displayBoard();
char displayHelp();
void playGame(char player, int plB, int plW);
void goToMenu(char x);
int saveFile(char x, char board[][COLS], char player, int plB, int plW);
void loadGame();
void clearBoard();

//Global Variables
int num = 1;
char board [COLS][ROWS] = {

    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ','W','B',' ',' ',' ',
    ' ',' ',' ','B','W',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',
    ' ',' ',' ',' ',' ',' ',' ',' ',

};

int main() {

    int choice = displayMenu();

    switch (choice) {
        case 1:
            system("clear");
            clearBoard();
            displayBoard();
            break;
        case 2:
            loadGame();
            break;
        case 3:
            displayHelp();
            break;
        case 4:
            return 0;
            break;
        default:
            cout << "Please enter a valid choice." << endl;
            break;
    }
    playGame('B', 2, 2);

}
//Function to draw and display the board
void displayBoard(){

    displayTop();
    num = 1;
    for (int row = 0; row < ROWS; row++){

        displayNumRow();
        cout << "   |";

        for (int column = 0; column < COLS; column++){
            cout << board[row][column] << "   |";
        }
        cout << endl;
        displayTop();

    }

    displayNumColumn();

}
// Function to display a horizontal line
void displayTop(){

    cout << "    ";

    for (int i = 0; i < ROWS; i++){
        cout << "+----";
    }

    cout << endl;

}
//Function to display the alphabets column
void displayNumColumn(){

    cout << "   ";

    for( int i = 1; i <= COLS; i++ ) {
        cout << "    " << i ;
    }
}
//Function to display the numbers row
void displayNumRow(){

    if (num <= ROWS) {
        cout << num;
        num++;

    }
}
// Function to display the menu
int displayMenu(){

    int answer = 0;
    cout << "Othello\n\n"
    << "1.New Game\n2.Load Game\n3.Help\n4.Quit\nYour Choice: ";
    cin >> answer;
    system("clear");
    return answer;

}
// Function to display the help window
char displayHelp(){

    char answer = ' ';

    cout << "How to play Othello\n\nThe object of the game is to have the majority of your colour discs on the board at the end of the game.\n\nInput the cell where you want to place your disc in the form of (rowNumber columnNumber) without the bracket and includng the space.\n\nThe one with the most discs wins!!!!\n\nSo, are you ready to play? (y or n)\n\nYour Choice: ";
    cin >> answer;

    if (answer == 'y')
        displayBoard();
    else if (answer == 'n')
        displayHelp();
    else
        cout << "Invalid input" << endl << endl;;
    displayHelp();

    return answer;
}
// Function to act as the game engine
void playGame(char player, int plB, int plW){
    char x = 0;
    int y = 0;

    // Infinite for loop to keep switching between players and asking for moves
    for(;;){
        cout << "\n\nScore: W = " << plW << " B = " << plB;
        cout << "\nPlayer: " << player;
        cout << "\nPlease make your move : ";
        cin >> x;
        cin >> y;
        cout << endl;
        goToMenu(x);
        int xi = x -'0';
        // If condition to check for the validity of the input
        if (xi <= ROWS && y <= COLS && board[xi-1][y-1] == ' ' && cin.good()) {
            board[xi-1][y-1] = player;
            displayBoard();
        } else {
            cin.clear();
            cin.ignore(100,'\n');
            if (saveFile(x, board, player, plB, plW)) {
                cout << "Game Saved" << endl;
                if (player == 'B') {
                    plB--;
                } else {
                    plW--;
                }
            } else {
                cout << "Invalid Input" << endl;
                if (player == 'B') {
                    plB--;
                } else {
                    plW--;
                }

            }

        }

        // If condition to always change the players and add one to their score
        if (player == 'B') {
            plB++;
            player = 'W';

        } else {
            plW++;
            player = 'B';
        }
    }

}

void goToMenu(char x){
    if (x == 'm') {
        main();
    }

}

int saveFile(char x, char board[][COLS], char player, int plB, int plW){
    ofstream savedGame("savedGame.txt");

    if (x == 's') {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                savedGame << board[i][j];

            }
        }

        savedGame << player;
        savedGame << plB;
        savedGame << plW;
    }else {
        return 0;
    }
    return 1;
}
//Again the same thread: thread 1 exc_bad_access (code=exc_i386_gpflt)
void loadGame(){
    char player;
    char plB;
    char plW;
    int blck;
    int wht;
    ifstream savedGame("savedGame.txt");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            savedGame >> noskipws;
            savedGame >> board[i][j];
        }
    }
    savedGame >> player;
    savedGame >> plW;
    savedGame >> plB;
    blck = plB - '0';
    wht = plW - '0';
    displayBoard();
    playGame(player, blck, wht);

}

void clearBoard(){
    for (int i = 0; i <= ROWS; i++) {
        for (int j = 0; j <= COLS; j++) {
            board[i][j] = ' ';
        }
    }
    board[3][3] = 'W';
    board[3][4] = 'B';
    board[4][3] = 'B';
    board[4][4] = 'W';
}

1 个答案:

答案 0 :(得分:2)

你的问题出现在for循环中,所有这些都有&#34; i&lt; = ROWS&#34;所以它将超出数组,因此抛出异常

for (int i = 0; i < ROWS; i++) 

你已经完成了