从文本文件中读取空白
Reading white spaces from a text file
我试图从某个文件中读取空白字符并将它们存储在数组位置,但是当我运行代码时,我一直得到这个线程:线程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 << "Othellonn"
<< "1.New Gamen2.Load Gamen3.Helpn4.QuitnYour Choice: ";
cin >> answer;
system("clear");
return answer;
}
// Function to display the help window
char displayHelp(){
char answer = ' ';
cout << "How to play OthellonnThe object of the game is to have the majority of your colour discs on the board at the end of the game.nnInput the cell where you want to place your disc in the form of (rowNumber columnNumber) without the bracket and includng the space.nnThe one with the most discs wins!!!!nnSo, are you ready to play? (y or n)nnYour 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 << "nnScore: 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';
}
你的问题是在for循环上,所有的循环都有"i <= ROWS"所以它会超出数组所以它会抛出异常
for (int i = 0; i < ROWS; i++)
你完成了
相关文章:
- 如何在c++中读取do-while循环中的空白
- C++忽略使用字符串流进行读取的文本文件中的空白
- C 中的空白功能未读取
- C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
- 读取空白*指向字符作为整数的安全性
- C++读取元数据文件,并处理由空白和:混合分隔的字段
- 如何读取文件内容并将其保存到字符串类型变量?为什么有空白空间
- 为什么从文件输入中读取 std::string 会返回空白
- 从boost::p roperty_tree读取数组显示为空白
- 从C++文件中读取,处理空白
- 读取字符数组的空白位数
- 如何在C++中只从stdin/cin中读取一个空白
- 需要读取空白和其他字符C++
- 如何在文件中读取,其中包含char, int和空白到链表
- 如何读取名称的字符数组,而不读取空白
- 在c++中使用regex读取空白,新行和制表符
- 从文件中读取,只读取文本,直到它变成空白
- 从文本文件中读取空白
- 如何在c++中使用字符串getline从文本文件中读取时忽略/跳过空白
- Boost::Asio服务器从客户端读取空白消息,即使在写句柄完成之后