【问题标题】:A couple of chars are not printing几个字符没有打印
【发布时间】: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_insurn_in 之外的所有内容。我找不到解决办法,你能帮帮我吗?

附言。我在 Visual Studio 中编码

【问题讨论】:

  • 为什么这些getchar();s 介于scanfs 之间?他们才是问题所在。
  • fflush(stdin); 是UB。
  • 我曾经在它们之间使用 fflush(stdin),但是发生了一些错误,所以我随机放置了它们并且它起作用了:p
  • 通过实验进行编程在大多数情况下是一个的想法。您应该了解您的代码的作用。
  • 您是否调试过代码,逐步跟踪并检查所有相关变量?

标签: c visual-studio char scanf


【解决方案1】:

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

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

scanf 格式解析它们%lld

fflush(stdin); 调用未定义的行为。您可能希望摆脱用户前面的任何类型:这不能在 C 中可移植地完成,并且无论如何具有可疑的价值。

tax = (eisodhma - 10000) * 20 / 100; 指的是未定义的变量。你是说tax = (income - 10000) * 20 / 100;吗?

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

scanf(" %c", &name_in);

最重要的是:

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

总是正确的。你真的是这个意思:

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

【讨论】:

  • 我理解错误,但为什么它仍然有效?
  • 它可能工作,因为这就是你的平台实现所做的。标准说它调用未定义的行为,所以任何事情都会发生。请注意,它可能会在不同的操作系统上崩溃。
  • 是的,这是收入 :p 变量应该用希腊语写成英文字母 :p
  • 多么奇怪的要求。我在法国工作,但我总是要求所有标识符都是英文的,以保持国际读者之间的一致性和便于交流。带有拉丁字母的希腊语对我来说仍然是希腊语;-)
  • 建议while (scanf("%c", name_in) == 1 &amp;&amp; isspace((unsigned char)name_in)) continue;,而不是scanf(" %c", name_in);(加空格)
【解决方案2】:

在扫描name_in 之前,您错过了必需的getchar()。否则,它将读取最后一个换行符。效果也会传播到surn_in

FWIW,fflush(stdin) 是未定义的行为。摆脱它。

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 2018-11-19
    • 1970-01-01
    • 2011-05-17
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多