Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 两个相同的循环,但第二个循环无限继续_C_Loops - Fatal编程技术网

C 两个相同的循环,但第二个循环无限继续

C 两个相同的循环,但第二个循环无限继续,c,loops,C,Loops,程序的预期输出是围绕用户输入的句子打印指定字符的边框 //program: border.c #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { int argu = argc - 1; if(argu < 1){ printf("usage: border arguments a

程序的预期输出是围绕用户输入的句子打印指定字符的边框

//program: border.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>



int main(int argc, char *argv[]) {
    int argu = argc - 1;

    if(argu < 1){
        printf("usage: border arguments are the text to border.\n");
    }

    else{
        printf("Enter the character for the border: ");
        char in = getchar();//get the character input by user
        int size;
        for(int i = 1; i <= argu; i++){// gets the count of the characters in the string
            size += strlen(argv[i]);
            size += 1; //to compensate for spaces
        }

        printf("%d", size);
        printf("\n");
        size += 2;

        for( int a = 0; a <= size ; a++){
            printf("%c", in); // prints the first border line
        }
        printf("\n");

        printf("%c%*c\n", in, size, in);//prints second line of border line.

        printf("%c", in);
        printf(" ");
        for( int i = 1; i <= argu; i++){//prints the sentence that was typed.
            printf("%s " , argv[i]);
        }
        printf("%c", in);
        printf("\n");

        printf("%c%*c\n", in, size, in);// same as the second line.
        printf("%d", size);



        for( int b = 0; b <= size ; b++){    //causing the infinite loop
            printf("%c", in);
        }

        printf("\n");
    }
    return 0;
//程序:border.c
#包括
#包括
#包括
int main(int argc,char*argv[]){
int argu=argc-1;
如果(argu<1){
printf(“用法:边框参数是指向边框的文本。\n”);
}
否则{
printf(“输入边框字符:”);
char in=getchar();//获取用户输入的字符
整数大小;

对于注释中提到的(int i=1;i),您从不初始化
size
,这意味着它有一些随机值。因此,您最终打印的边框字符数等于该随机值加上预期的边框长度

如果您这样做:

int size = 0;
这将解决问题。示例输入/输出:

[dbush@db-centos tmp]$ ./x1 test text
Enter the character for the border: x
10
xxxxxxxxxxxxx
x           x
x test text x
x           x
12xxxxxxxxxxxxx
现在我们看到底部边框上有一个数字。这是因为您在底部边框之前打印
size
,所以您应该将该
printf
移动到循环之后,以打印该边框

因此,改变这一点:

    printf("%c%*c\n", in, size, in);// same as the second line.
    printf("%d", size);

    for( int b = 0; b <= size ; b++){    //causing the infinite loop
        printf("%c", in);
    }
printf(“%c%*c\n”,in,size,in);//与第二行相同。
printf(“%d”,大小);

对于(intb=0;b注:
getchar()
返回
int
,而不是
char
,原因很充分。始终检查
EOF
大小
未初始化。行为未定义。可能只是巧合,它第一次工作。@AlexD是正确的…最终大小是问题…问题不是两个循环…问题是这些行:printf(“%c%*c\n”,in,size,in);…它们使用大小作为要打印的字符数的计数。请阅读printf处理负值的手册页。由于要通过添加来计算
size
,请将
int size;
更改为
int size=0;
[dbush@db-centos tmp]$ ./x1 test text
Enter the character for the border: x
10
xxxxxxxxxxxxx
x           x
x test text x
x           x
12xxxxxxxxxxxxx
    printf("%c%*c\n", in, size, in);// same as the second line.
    printf("%d", size);

    for( int b = 0; b <= size ; b++){    //causing the infinite loop
        printf("%c", in);
    }
    printf("%c%*c\n", in, size, in);// same as the second line.

    for( int b = 0; b <= size ; b++){    //causing the infinite loop
        printf("%c", in);
    }
    printf("%d", size);  // this line moved down