代码之家  ›  专栏  ›  技术社区  ›  C. Cunanan

int函数返回ASCII特殊字符?

int c
  •  0
  • C. Cunanan  · 技术社区  · 6 年前

    所以我是C语言的新手,我有一个程序可以询问电话号码

    getInt函数如下所示:

    int getInt(void) {
        int num;
        char newline = 0;
        do {
            scanf("%d%c", &num, &newline);
            if (newline != '\n') {
                printf("*** Invalid Integer *** <Please enter an integer>: ");
                clearKeyboard();
            }
        } while (newline != '\n');
        return num;
    }
    

    定义如下:

    void getNumber(struct Number *num) {
        int response;
    
        printf("Please enter the contact's cell phone number: ");
        *num->cell = getInt();
    
        printf("Do you want to enter a home phone number? (y or n): ");
        response = yes();
    
        if (response == 1) {
            printf("Please enter the contact's home phone number: ");
            *num->home = getInt();
        }
    
        printf("Do you want to enter a business phone number? (y or n): ");
        response = yes();
    
        if (response == 1) {
            printf("Please enter the contact's business phone number: ");
            *num->business = getInt();
        }
    }
    

    主要内容如下:

    printf("Numbers: %s|%s|%s\n", contact.number.cell, contact.number.home, contact.number.business);
    

    但出于某种原因,当我输入电话号码时,输出如下: error ascii?

    抱歉,我知道图像中的文本,但我无法从调试窗口复制粘贴。

    我只想知道我的代码中可能有什么错误,它会返回ascii字母。提前谢谢你

    1 回复  |  直到 6 年前
        1
  •  0
  •   C. Cunanan    6 年前

    好的,在看了@Pablo评论的类似问题后,它现在起作用了。 答案中有以下代码:

    void getNumbers(struct Numbers *numbers) // getNumbers function definition
    {
        int result;
    
        printf("Please enter the contact's cell phone number: ");
        scanf("%s", numbers->cell);
        clear_buffer(stdin);
    
    
        if(yes("Do you want to enter a home phone number? (y or n)"))
        {
            printf("Please enter the contact's home phone number: ");
            scanf("%s", numbers->home);
            clear_buffer(stdin);
        }
    
        if(yes("Do you want to enter a business phone number? (y or n)"))
        {
            printf("Please enter the contact's business phone number: ");
            scanf("%s", numbers->business);
            clear_buffer(stdin);
        }
    }
    

    于是我继续复制同样的想法:

    void getNumber(struct Number *num) {
        int response;
        printf("Please enter the contact's cell phone number: ");
        scanf("%10s", num->cell); // I changed this line from:
    //  *num->cell = getInt();
    
    
        printf("Do you want to enter a home phone number? (y or n): ");
        response = yes();
    
        if (response == 1) {
            printf("Please enter the contact's home phone number: ");
            scanf("%10s", num->home); // I changed this line from:
    //  *num->home = getInt();
        }
    
        printf("Do you want to enter a business phone number? (y or n): ");
        response = yes();
    
        if (response == 1) {
            printf("Please enter the contact's business phone number: ");
            scanf("%10s", num->business); // I changed this line from:
    //  *num->business = getInt();
        }
    }
    

    可能getInt函数不适合定义。我不知道该如何解释,但这是可行的。

    谢谢大家。