几个字符没有打印
A couple of chars are not printing
我正在学习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_in
和 surn_in
之外的所有内容。我找不到解决办法,你能帮我吗?
PS。我在 Visual Studio
编码
您在扫描 name_in
之前错过了必需的 getchar()
。否则,它读取最后一个换行符。效果也会传播 surn_in
。
FWIW,fflush(stdin)
是未定义的行为。摆脱那个。
afm > 9999999999
对于 int afm
始终为假。在您的平台上,int
和 long
的长度为 32 位,因此限制为小于 2147483647
.
的值
您应该为这些变量使用类型 long long
。
用 scanf
格式解析它们 %lld
fflush(stdin);
调用未定义的行为。您可能想摆脱用户前面的任何类型:这不能在 C 中移植,而且无论如何都具有可疑的价值。
tax = (eisodhma - 10000) * 20 / 100;
引用了一个未定义的变量。您是说 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编程!我正在尝试编写一个程序 但有些事情不能正常工作! 这是代码,我将在下面解释!
#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_in
和 surn_in
之外的所有内容。我找不到解决办法,你能帮我吗?
PS。我在 Visual Studio
编码您在扫描 name_in
之前错过了必需的 getchar()
。否则,它读取最后一个换行符。效果也会传播 surn_in
。
FWIW,fflush(stdin)
是未定义的行为。摆脱那个。
afm > 9999999999
对于 int afm
始终为假。在您的平台上,int
和 long
的长度为 32 位,因此限制为小于 2147483647
.
您应该为这些变量使用类型 long long
。
用 scanf
格式解析它们 %lld
fflush(stdin);
调用未定义的行为。您可能想摆脱用户前面的任何类型:这不能在 C 中移植,而且无论如何都具有可疑的价值。
tax = (eisodhma - 10000) * 20 / 100;
引用了一个未定义的变量。您是说 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)