简体   繁体   English

C - 三角形程序在调用 main 时不打印任何输出

[英]C - Triangle program not printing any output on call to main

I have written a C code that is supposed to print a triangle.我写了一个应该打印三角形的 C 代码。 A width and character are defined and the code should then print out a triangle of that with a fill of that character.定义了宽度和字符,然后代码应该打印出一个三角形,并填充该字符。 I think there may be an error in main, but the code compiles fine.我认为 main 中可能有错误,但代码编译得很好。 When I run it, there is no output.当我运行它时,没有输出。

Here is my code:这是我的代码:

void triangle(int width, char x);

int main(void){
    triangle(4, c);
}

void triangle(int width, char x){
    if (width > 2){
        return;
    }
    int counter = 1;
    int direction = 1;
    do{
        int i;
        for(i = 0; i < width - counter; i++){
            printf(" ");
        }
        for (int i = 0; i < counter; i++){
            printf("%c", x);
        }
        printf("\n");
        counter += direction;
        if(counter > width){
            counter = width - 1;
            direction = -1;
        }
    }while (counter != 1);
    return;
}

You have你有

 if (width > 2){
        return;
 }

and the call is电话是

 triangle(4, c);

This explains the no output.这解释了没有输出。 There are other error as well, but you first try working them out yourself maybe?还有其他错误,但您首先尝试自己解决吧?

Sorry if this wasn't clear in the question.对不起,如果这在问题中不清楚。 The expected output should be a triangle of width (and height) of 4, with a fill of 'c'.预期的输出应该是一个宽度(和高度)为 4 的三角形,填充为“c”。

So I think I have figured it out.所以我想我已经想通了。 I must use the < operator (silly human error) and the character must have a single quote: 'c'.我必须使用 < 操作符(愚蠢的人为错误)并且字符必须有一个单引号:'c'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM