我在学习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。我正在中编码
发布于 2015-11-03 20:03:32
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)发布于 2015-11-03 20:03:21
在扫描getchar()之前,您忽略了所需的name_in。否则,它将读取最后一行换行符。surn_in的效果也会传播。
fflush(stdin)是一种未定义的行为。把它处理掉。
https://stackoverflow.com/questions/33507925
复制相似问题