【发布时间】:2015-11-03 19:56:50
【问题描述】:
我正在学习 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 之外的所有内容。我找不到解决办法,你能帮帮我吗?
附言。我在 Visual Studio 中编码
【问题讨论】:
-
为什么这些
getchar();s 介于scanfs 之间?他们才是问题所在。 -
和
fflush(stdin);是UB。 -
我曾经在它们之间使用 fflush(stdin),但是发生了一些错误,所以我随机放置了它们并且它起作用了:p
-
通过实验进行编程在大多数情况下是一个坏的想法。您应该了解您的代码的作用。
-
您是否调试过代码,逐步跟踪并检查所有相关变量?
标签: c visual-studio char scanf