多嵌套的循环C++未完成

multi-nested for loop C++ not finishing

本文关键字:C++ 未完成 循环 嵌套      更新时间:2023-10-16

我目前有一个关于我的 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<<"*";
        }
    }
    }
}

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

这是错别字吗?

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

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

几件事:

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

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

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

for(int count_c = 0; count_c < stars; ++count_c) {

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