小编Kar*_*ann的帖子

用不同的编程结构替换`goto`

我试图用防御性编程来做这个小程序,但是我很难处理这个避免Loop-Goto,因为我知道这是BAD编程.我曾尝试过,并且做...而循环,但在一个案例中,我没有问题.当我要做另一个时,问题开始...而对于第二种情况("不插入空格或点击输入按钮").我尝试和嵌套做...虽然但在这里结果更复杂.

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int i;
    int length;
    char giventext [25];        
    Loop:

    printf("String must have 25 chars lenght:\n");
    gets(giventext);

    length = strlen(giventext);

    if (length > 25) {
        printf("\nString has over %d chars.\nMust give a shorter string\n", length);
        goto Loop;
    }
    /* Here i trying to not give space or nothing*/
    if (length < 1) {
        printf("You dont give anything as a string.\n");
        goto Loop;
    } else {
        printf("Your string has %d\n",length);
        printf("Letter in lower …
Run Code Online (Sandbox Code Playgroud)

c defensive-programming repeat

2
推荐指数
1
解决办法
125
查看次数

标签 统计

c ×1

defensive-programming ×1

repeat ×1