多嵌套for循环C ++没有完成

时间:2017-02-03 00:29:10

标签: c++ for-loop

我目前为我的c ++大学课程做了一个小作业。我目前遇到一个错误,它运行前两个循环,但然后冻结,并没有完成其余的循环。重点是打印出一个*钻石

如果输入数字7,示例将如下所示:

     *
    ***
   *****
  *******
   *****
    ***
     * 

这就是代码目前的样子:

#include <iostream>
using namespace std;



int main(){
cout<<"How many lines do you want?";
int num_rows;
cin>>num_rows;
int row_average = (num_rows/2)+1;
for(int count=0; count<num_rows; ++count){
    int midpoint = row_average - count;
    int absolute = abs(midpoint);
    int spaces = absolute;


    for (int count_a = 0; count_a<spaces; ++count_a){
        cout<<" ";
    }
    for (int count_b = row_average; count_b<num_rows; ++count){
        int stars = count_b - spaces;
        for(int count_c = 0; count_c = stars; ++count_c){
            cout<<"*";
        }
    }
    }
}

任何答案或帮助将不胜感激! 谢谢!

2 个答案:

答案 0 :(得分:2)

这是一个错字吗?

for (int count_b = row_average; count_b<num_rows; ++count){
       ---------------------------------------------^^^^^

不应该是++count_b吗? 你也没有输出任何换行符?

答案 1 :(得分:1)

一些事情:

1)在第二个嵌套的for循环中,你应该增加count_b。所以

self.problem

2)在最后一个嵌套for循环中,你需要使条件count_c小于星数,不等于它。所以

for(int count_b = row_average; count_b < num_rows; ++count_b) {

3)最后,你需要一个换行符,否则所有这些星星都会在同一行打印