gpt4 book ai didi

c - 几个字符没有打印

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:24 24 4
gpt4 key购买 nike

我正在学习 C 编程!我正在尝试编写一个程序但有些事情不能正常工作!这是代码,我将在下面解释!

 #include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS


int main(void)
{
int afm, date, month, year,code;
char name_in, surn_in, slash, category;
float income, tax;
do {
printf("Give afm : "); /*Give a 10 digit number*/
scanf("%d", &afm);
if (afm < 1000000000 || afm > 9999999999) {
printf("Incorrect!\n");
}
} while (afm < 1000000000 || afm > 9999999999);
fflush(stdin);
printf("Give your name's first letter: ");
scanf("%c", &name_in);
getchar();
printf("Give surname's first letter: ");
scanf("%c", &surn_in);
getchar();

do
{
printf("Date of birth(must be at least 18) : ");
scanf("%d%c%d%c%d", &date, &slash, &mhnas, &slash, &etos); /*just write 20/10/1987 */
if (month < 1 || month>12) {
printf("Incorrect month. \n");
}
if (year > 1997) {
printf("Year incorrect \n");
if (2015 - year == 18 && month==12 ) {
printf("Incorrect date of birth.\n");

}
}
} while ((month < 1 || month>12) || (year > 1997) || (2015 - year == 18 && mhnas == 12));
printf("Add your income ");
scanf("%f", &income);

code = afm % 10; /*need to find the afm's last digit*/

if (code == 1 || code == 2 || code == 3) {
category = "Misthwtos";
if (income <= 10000) {
tax = 0;
}
if (income > 10000 && income <= 30000) {
tax = (eisodhma - 10000) * 20 / 100;
}
if (income > 30000)
tax = (20000 * 20 / 100) + ((eisodhma - 30000) * 30 / 100);
}

if (code != 1 || code != 2 || code != 3) {
tax = income * 30 / 100;
}

printf("Info: \n");
printf("%d %c.%c. &d/%d/%d",afm, name_in, surn_in, date, month, year);


system("pause");
return 0;
}

所以,问题在于,当程序打印我在代码末尾询问的内容时,它会打印除字符 name_insurn_in 之外的所有内容。我找不到解决方案,你能帮我吗?

附言。我在 Visual Studio 中编码

最佳答案

afm > 9999999999 对于 int afm 始终为假。在您的平台上,intlong 是 32 位长,因此限制为小于 2147483647 的值。

您应该为这些变量使用 long long 类型。

scanf格式%lld解析它们

fflush(stdin); 调用未定义的行为。您可能想摆脱用户前面的任何类型:这在 C 中无法移植,而且无论如何都具有可疑的值(value)。

tax = (eisodhma - 10000) * 20/100; 指的是一个 undefined variable 。您是说 tax = (income - 10000) * 20/100; 吗?

scanf("%c", &name_in); 不读取下一个字符,它读取在标准输入中缓冲的 '\n'。要跳过空格,chux 建议采用以下简单修复方法:

scanf(" %c", &name_in);

最重要的是:

if (code != 1 || code != 2 || code != 3)

始终为真。你真的是这个意思:

if (code != 1 && code != 2 && code != 3)

关于c - 几个字符没有打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33507925/

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