C -INT功能正在返回ASCII特殊字符



所以我是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|%sn", contact.number.cell, contact.number.home, contact.number.business);

但是由于某种原因,当我输入电话号码时,输出会这样:错误ascii?

对不起,我知道图像中的文字,但我无法从调试窗口复制粘贴。

我想知道我的代码中可能出了什么问题,然后返回ASCII字母。预先感谢

好吧,现在它在查看@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功能不适合定义。我不知道如何解释,但这有效。

谢谢大家。

最新更新