2016-02-23 27 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; 
}