2016-11-13 30 views
0

我知道已經有幾個關於這個主題的問題回答了,但是他們都不能幫我解決我的問題。請記住,我剛剛開始學習C++編程。C++顯示一個字符串(與空格)到一個二維數組?

我正在創建一個程序來讀取文本,然後將其顯示在N行x M列(由用戶輸入)中。因此,如果用戶寫哈利·波特並希望它顯示在3×4陣列就應該是這個樣子:

H a r r 
y P o t 
t e r 

我已經管理這個代碼可以這樣做:

cout << "Number of rows: "; 
cin >> nr; 
cout << "Number of columns: "; 
cin >> nc; 
cout << "Type in text: "; 
cin >> text; 

char letters[100][100]; 

for (int row = 0; row < nr; row++) 
{ 
    for (int col = 0; col < nc; col++) 
    { 
     letters[row][col]= text [i]; 
     i++; 
    } 
} 

cout << "Print array: " << endl; 
for (int row = 0; row < nr; row++) 
    { 
     for (int col = 0; col < nc; col++) 
     { 
      cout << letters[row][col]; 
     } 

    cout << "\n"; 
    } 

它工作正常,直到用戶寫入多個單詞。例如,而不是HarryPotter他寫道哈利波特(我認爲這些詞之間的空格是造成問題的原因)。你知道爲什麼發生這種情況,我該如何解決它?非常感謝你。

+1

如何定義可變文本?如果iif二維數組的總元素大於文本中的字符數,該怎麼辦? –

+0

你也應該添加null終止到輸入文本的末尾 – Raindrop7

+0

我懷疑['std :: getline'](http://en.cppreference.com/w/cpp/string/basic_string/getline)很快會在你的未來。 – WhozCraig

回答

1

問題是operator >>在流中遇到空格字符時停止輸入字符串。您應該使用標準功能std::getline

考慮到要顯示一個字符串,不需要定義一個數組。該任務可以在不使用數組的情況下完成。

這是一個示範程序。

#include <iostream> 
#include <string> 
#include <limits> 
#include <algorithm> 

int main() 
{ 
    while (true) 
    { 
     std::cout << "Number of rows (0 - exit): "; 

     unsigned int rows; 
     if (not (std::cin >> rows) or (rows == 0)) break; 

     std::cout << "Number of columns (0 - exit): "; 

     unsigned int cols; 
     if (not (std::cin >> cols) or (cols == 0)) break; 

     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

     std::cout << "Type in text: (Enter - exit): "; 

     std::string text; 
     std::getline(std::cin, text); 
     if (text.empty()) break; 

     std::cout << std::endl; 

     std::string::size_type n = text.size(); 

     n = std::min<std::string::size_type>(n, cols * rows); 

     for (std::string:: size_type i = 0; i < n; i++) 
     { 
      std::cout << text[i]; 

      std::cout << ((i + 1) % cols == 0 ? '\n' : ' '); 
     } 

     std::cout << std::endl; 
    } 

    return 0; 
} 

它的輸出可能看起來像

Number of rows (0 - exit): 3 
Number of columns (0 - exit): 4 
Type in text: (Enter - exit): HarryPotter 

H a r r 
y P o t 
t e r 

Number of rows (0 - exit): 2 
Number of columns (0 - exit): 6 
Type in text: (Enter - exit): HarryPotter 

H a r r y P 
o t t e r 

Number of rows (0 - exit): 6 
Number of columns (0 - exit): 2 
Type in text: (Enter - exit): HarryPotter 

H a 
r r 
y P 
o t 
t e 
r 

Number of rows (0 - exit): 4 
Number of columns (0 - exit): 4 
Type in text: (Enter - exit): Antonella Masini 

A n t o 
n e l l 
a M a 
s i n i 

Number of rows (0 - exit): 0 

您可以替代這種說法

if (not (std::cin >> rows) or (rows == 0)) break; 

對於這一說法

if (!(std::cin >> rows) || (rows == 0)) break; 

如果編譯器不編譯的第一個語句。如果您使用MS VC++,那麼在項目的屬性中,您應該關閉使用語言擴展(C++,language),並且語句將被編譯。

+0

嗨,謝謝你的迴應,但是當我把它在我的代碼中說,它不能識別變量不... –

+0

@AntonellaMasini你使用什麼編譯器? –

+0

@AntonellaMasini在任何情況下,你可以只寫(if(!(std :: cin >> rows)||(rows == 0))break; –

1

cin會考慮HarryPotter之間的空間作爲分隔符,只保存在Harrytext

使用cin.getline,將讀取整行,直到按下Enter鍵。因此,cin.getline(text, sizeof(text)),Harry Potter將保存在text

+0

謝謝你的回答,但是我只需要替換我的「cin >>文本;」與「cin.getline(text,sizeof(text));」; ?因爲我試過,它不讓我運行程序,因爲它說「沒有重載函數的實例...匹配參數列表」...... –

+0

您也可以使用'getline(cin,string)'。 – MarcD

+0

你用過的cin.getline(czTmp,row * col)應該沒問題。理想情況下應該分配czTmp(row * col)字節。 – Vivek

0

您可以使用字符的動態數組所以它在運行時的大小爲行和列的用戶輸入:

#include <iostream> 
using namespace std; 

int main() 
{ 
    int row, col; 

    cout << "Row: "; 
    cin >> row; 
    cout << "Col: "; 
    cin >> col; 

    char** cText = new char*[row]; 
    for(int i(0); i < row; i++) 
     cText[i] = new char[col]; 

    cin.sync(); // clearing the input buffer 
    cout << "Text: "; 

    char czTmp[100]; 
    cin.getline(czTmp, row * col); // getting user input text which fits to our 2d array of chars 

    int k = 0; 
    for(int i = 0; i < row; i++) 
    { 
     for(int j = 0; j < col; j++) 
     { 
      cText[i][j] = czTmp[k]; // inputting our array 
      k++; 
     } 
    } 

    cText[row - 1][col - 1] = '\0'; // adding a null-terminator because it is not added automatically 

    // printing our array 
    for(int i = 0; i < row; i++) 
    { 
     for(int j(0); j < col; j++) 
      cout << cText[i][j]; 
     cout << endl; 
    } 

    // don't forget to free memory created by new 

    for(int i(0); i < row; i++) 
     delete[] cText[i]; 
    delete[] cText; 
    cout << endl << endl; 
    return 0; 
} 
+0

謝謝你的答案,但是當我使用你的代碼時,它在輸入列數後崩潰了,你知道這是爲什麼嗎? –

相關問題