嘗試更多的東西是這樣的:
#include <iostream>
#include <iomanip>
#include <limits>
char ** board = new char *[row];
for (int r = 0; r < row; r++) {
board[r] = new char[col];
}
for (int r = 0; r < row; r++) {
std::cout << "Enter input: " << std::endl;
std::cin >> std::noskipws >> std::setw(col) >> board[r];
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
然而,正如上面所提到的意見,你真的應該使用std::string
和std::getline()
代替。如果可以的話,你的陣列更改std::vector<std::string>
:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> board(row);
for (int r = 0; r < row; r++) {
std::cout << "Enter input: " << std::endl;
std:getline(std::cin, board[r]);
}
如果您不能使用std::vector
,你至少可以使用std::string
讀取用戶的輸入,那麼它的數據複製到您的char[][]
陣列:
#include <iostream>
#include <string>
#include <cstring>
char ** board = new char *[row];
for (int r = 0; r < row; r++) {
board[r] = new char[col];
}
for (int r = 0; r < row; r++) {
std::cout << "Enter input: " << std::endl;
std::string input;
std::getline(std::cin, input);
std::strncpy(board[r], input.c_str(), col-1);
board[r][col-1] = '\0';
}
如果你想要整個*行然後使用['std :: string'](http://en.cppreference.com/w/cpp/string/basic_string)和['std :: getline'](http:取而代之的是//en.cppreference.com/w/cpp/string/basic_string/getline)。 –
這不是[mcve]。 'board'的類型是什麼? – Xirema
重新打開,因爲我認爲你需要get()而不是getline(),但是這個問題需要更多的細節。 – NathanOliver