int函数是返回ASCII特殊字符?

时间:2018-03-24 03:57:59

标签: c int

所以我是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 个答案:

答案 0 :(得分:0)

好的,所以现在看看@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函数不适合定义。我不知道如何解释它,但这有效。

谢谢大家。