2015-09-20 102 views
-2

我打印在6x6網格中的ascii框中隨機數字。在打印網格時遇到問題。C++網格問題

而不是有6列,我的所有框和數字正在打印出1列。經過故障排除,但似乎無法找到問題。以下是代碼。感謝您的幫助。

int main(void) 
{ 

    cout << "Magic Grid\n" << endl; 

    int arrayxy [6][6]; 
    srand((unsigned)time(0)); 
    int lowest=1111, highest=9999; 
    int range=(highest-lowest)+1; 

// Fill array with random values 
for (int i = 0; i < 6; ++i) 
{ 
    for(int j = 0; j < 6; ++j) 
    { 
     arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 
} 

// Print array as grid 
for (int i = 0; i < 6; ++i) 
{ 
    for(int j = 0; j < 6; ++j) 
    { 
      cout << char(218); 
      for (int y=0; y< 4; y++) 
      { 
       cout << char(196); 
      } 
      cout << char(191) <<endl; 
      cout << char(179) << arrayxy[i][j] << char(179) << endl; 
      cout << char(192); 
      for (int z=0; z< 4; z++) 
      { 
       cout << char(196); 
      } 
      cout << char(217) <<endl; 
    } 
    cout << endl; 
} 

cout << endl; 

} 
+1

你爲什麼不通過您的代碼步與調試,獲得抓地力是怎麼回事? –

+0

調試器不會返回任何錯誤。沒有問題,只是輸出只給了我1列。 – eddyiction

+1

您可能會將調試器與編譯器混淆。 –

回答

0

你應該endl所有列打印出來後,而不是之前。

這就是我如何修復你的代碼,希望得到你想要的。

爲了記錄在案,我特別更改那些ASCII*&,使控制檯結果更具可讀性。你可以將它們改回到你想讓這些角色再次出現。

void WriteFrontLine(std::size_t count) 
{ 
    for (int i = 0; i < count; i++) 
    { 
     cout << '*'; 
     cout << '&' << '&' << '&' << '&'; 
     cout << '*'; 
    } 

    cout << endl; 
} 

void WriteEndLine(std::size_t count) 
{ 
    for (int i = 0; i < count; i++) 
    { 
     cout << '*'; 
     cout << '&' << '&' << '&' << '&'; 
     cout << '*'; 
    } 

    cout << endl; 
} 


int main(void) 
{ 
    cout << "Magic Grid\n" << endl; 

    int arrayxy[6][6]; 
    srand((unsigned)time(0)); 
    int lowest = 1111, highest = 9999; 
    int range = (highest - lowest) + 1; 

    // Fill array with random values 
    for (int i = 0; i < 6; ++i) 
    { 
     for (int j = 0; j < 6; ++j) 
     { 
      arrayxy[i][j] = lowest + int(range*rand()/(RAND_MAX + 1.0)); 
     } 
    } 

    // Print array as grid 
    for (int i = 0; i < 6; ++i) 
    { 
     WriteFrontLine(6); 

     for (int j = 0; j < 6; ++j) 
     { 
      cout << '*' << arrayxy[i][j] << '*'; 
     } 

     cout << endl; 

     WriteEndLine(6); 

     cout << endl; 
    } 
    cout << endl; 
} 

enter image description here