gpt4 book ai didi

c - int 函数返回 ASCII 特殊字符?

转载 作者:行者123 更新时间:2023-11-30 16:33:59 25 4
gpt4 key购买 nike

我是 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 字母。预先感谢您

最佳答案

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

谢谢大家。

关于c - int 函数返回 ASCII 特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49461067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com