2016-02-23 28 views
-1

你好我有一些麻煩,我的嵌套循環輸出用戶選擇的25個字符的行。假設每行有25個字符,用戶輸入要打印的行數。嵌套循環顯示25個字符的行C++

此外,每行需要比上一行多一個標籤。

目前我只能得到程序輸出一行字符,或者它會顯示25行選定的一個字符。

請幫忙。任何輸入讚賞。

int lines=0, count=0, amount=0, symbol, i=0, j=0; 

do { 
    cout << "\nEnter number of lines to print: "; 
    cin >> lines; 
    if (lines < 5) { 
     cout << "Please enter an integer greater than 5." << endl; 
     system("pause");    
    } 
    else if (lines >= 5) { 
     cout << "\nEnter the number corresponding to the character you would like to display: "; 
     cout << "\n 1. * \n 2. $ \n 3. # \n 4. ! \n 5. & \n "; 
     cin >> symbol; 

     for (i = 1; i <= 25; i++) { 
      cout << symbol << " " ; 
      count++; 
      for (j = 1; j <= lines; j++) { 
       cout << "" << endl; 
      } 
     } 
    } 
} while (1); 

回答

1

你的嵌套循環錯了。

嘗試:

// Iterate parent loop line by line 
for(j=1; j<=lines; j++){ 
    // Add tabs according to the line number 
    for(int k=1; k<j; k++){ 
     cout << "\t"; 
    } 

    // Print symbol 25 times each line 
    for(i=1; i<=25; i++){ 
     cout << symbol << " "; 
    } 
    cout << endl; 
}