gpt4 book ai didi

c - While 循环不断检查用户输入的整数

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

我知道这个问题之前已经被问过,很抱歉,但我找不到适合我的代码的解决方案。我是 C 语言的新学生,我的问题是我正在尝试创建一个 while 循环,该循环不断要求用户在输入非整数时更新数据。我研究了 isdigit() 函数,我认为这会起作用。我似乎无法让它适用于我的代码,因为每当我尝试创建一个以 isdigit 作为条件的 while 循环时,我要么得到一个无限循环,或者我的 isdigit 返回了我不期望的值。我认为问题可能是我的 scanf() 正在扫描由“/”分隔的三个整数,但我不确定这是否是问题所在。任何帮助,将不胜感激。

这是有问题的代码

#include <stdio.h>
#include <ctype.h>

int calcAge(int a, int b, int c, int d, int e, int f);
int maxHeart(int a);
int heartRangeLow(int a);
int heartRangeHigh(int a);

int main(void) {
int birthMonth = 0, birthDay = 0, birthYear = 0, targetRangeLow = 0;
int month = 0, day = 0, year = 0, age = 0, heartRate = 0, targetRangeHigh = 0;
printf("What is your birthday?(written in MM/DD/YYYY)"
"\nHit Enter after you type it in.\n");
scanf_s("%d/%d/%d", &birthMonth, &birthDay, &birthYear);
printf("What is the date today?(written as MM/DD/YYYY)"
"\nHit Enter after you type it in.\n");
scanf_s("%d/%d/%d", &month, &day, &year);
age = calcAge(birthMonth, birthYear, birthDay, month, day, year);
heartRate = maxHeart(age);
targetRangeLow = heartRangeLow(heartRate);
targetRangeHigh = heartRangeHigh(heartRate);
printf("You are %d years old.\n", age);
printf("Your maximum heart rate is %d beats per minute.\n", heartRate);
printf("Your target-heart-range is %d"
" to %d beats per minute.\n", targetRangeLow, targetRangeHigh);
system("pause");
return 0;
}

int calcAge(int a, int b, int c, int d, int e, int f)
{
int age;
age = ((f * 10000 + d * 100 + e) - (b * 10000 + a * 100 + c)) * .0001;
return age;
}

int maxHeart(int a)
{
int heartRateMax;
heartRateMax = 220 - a;
return heartRateMax;
}

int heartRangeLow(int a)
{
int lowTarget = a * .5;
return lowTarget;
}

int heartRangeHigh(int a)
{
int highTarget = a * .85;
return highTarget;
}

最佳答案

to create a while loop that continually asks the user to update the data if they enter non integers.

创建一个辅助函数。使用 fgets() 将用户输入读取到缓冲区中,然后使用 strtol()sscanf() 解析它以获取有效输入, is...() 等。根据输入返回 1, 0, EOF

// Untested code

// 1 success
// 0 bad input
// EOF end of file
#define DATE_STR_SIZE 80
int enter_date_mdy(char *prompt, int *m, int *d, int *y) {
if (prompt) {
fputs(prompt, stdout);
fflush(stdout);
}
char buffer[DATE_STR_SIZE];
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
return EOF;
}
int n = 0;
sscanf(buffer, "%d /%d /%d %n", m, d, y, &n);

// If scan incomplete or extra junk at the end ....
if (n == 0 || buffer[n]) {
return 0;
}

// Additional tests as desired ....
if (*m < 1 || *m > 12 || *d < 1 || *d > 31) {
return 0;
}

return 1;
}

使用示例

int count;
do {
count = enter_date_mdy("What is your birthday?(written in MM/DD/YYYY)",
&birthMonth, &birthDay, &birthYear);
if (count == EOF) return(EXIT_FAILURE);
} while (count < 1);

do {
count = enter_date_mdy("What is the date today?(written as MM/DD/YYYY)",
&month, &day, &year);
if (count == EOF) return(EXIT_FAILURE);
} while (count < 1);
<小时/>

提示:不要“今天是几号?”,而是研究 time()、mktime()

关于c - While 循环不断检查用户输入的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454837/

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