简体   繁体   English

C编程传递字符功能

[英]C Programming Passing Character Function

 #include <stdio.h>
 #include <string.h>
 #define SIZE 200

 void check(char str[]);

 void check(char str[]){
        /* checking if the characters entered are digits */
        int i;
        int j=0;
        char tel[10];

        for(i = 0; str[i] != '\0'; i++){
            if(str[i] >= '0' && str[i] <= '9'){
                tel[j++] = str[i];
            }
        }
        tel[j] = '\0';

    /* checking if length of zip code  is less than 5 digits */
    if(strlen(tel) < 5)
            printf("Not enough digits on input!");


    /* if length of zip code is 5 digits */
    else if (strlen(tel) == 5){
        printf("\nProgram Output: ");
        printf("(");

        /* formating zip code with parenthesis around zip */
        for(i = 0; tel[i] != '\0' && i < 5; i++){
            printf("%c", tel[i]);
        }
        printf(")");
    };
}

int main(){

    /* variable declaration */
    char str[SIZE];

    printf("Enter a zip code: ");
    scanf("%s", str);

    check(str);

    return 0;
}

I am writing a simple C code that validates a 5 character zip code string from the user. 我正在编写一个简单的C代码,用于验证用户的5个字符的邮政编码字符串。 If less than 5 characters, program outputs error message. 如果少于5个字符,程序将输出错误消息。 If exactly 5 characters, places parenthesis around zip code. 如果恰好是5个字符,请在邮政编码周围加上括号。

When I test my code, only my error message works. 当我测试代码时,只有我的错误消息有效。 My 'else if' parameter isn't being executed when I input 5 characters. 当我输入5个字符时,“ else if”参数未执行。 Is it the way I'm passing the char input to my function? 这是将char输入传递给函数的方式吗?

looks like your input contains characters and characters are not taken care in your code. 看起来您的输入中包含字符,而代码中的字符却不小心。

1234a
output: Enter a zip code: Not enough digits on input!

12345
output:Program Output: (12345)

Take a look at your for loop inside check function 看看您的for循环内部检查功能

您的问题是您可能没有传递5个数字,请执行以下操作,添加其他对象以调试else printf ("%s %d", tel, strlen (tel));

一切正常 So please try to use GCC compiler if not ! 因此,如果没有,请尝试使用GCC编译器! The other case of error will be wrong input! 另一个错误的情况是输入错误! Program is working fine according to your expectation ! 程序运行正常,符合您的期望! 在此处输入图片说明

When I test my code, only my error message works. 当我测试代码时,只有我的错误消息有效。 My 'else if' parameter isn't being executed when I input 5 characters. 当我输入5个字符时,“ else if”参数未执行。 Is it the way I'm passing the char input to my function? 这是将char输入传递给函数的方式吗?

No. It's because you are using buffered output. 不。这是因为您正在使用缓冲输出。 printf stores its output in an internal buffer until either the buffer is full or it has to print a new line \\n . printf将其输出存储在内部缓冲区中,直到缓冲区已满或必须打印新行\\n为止。 Your program is exiting before printf has a chance to flush the buffer to the output. printf有机会将缓冲区刷新到输出之前,程序将退出。 You need to terminate your final printf with a \\n . 您需要使用\\n终止最终的printf

printf(")\n");

You do want a new line there, anyway, otherwise, if it did print, the next shell prompt will be on the same line. 无论如何,您确实都希望在那里有新行,否则,如果它确实打印了,则下一个shell提示将在同一行上。

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

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