我的简单代码没有给出输出

My simple code doesn't give an output

#include <stdio.h>
#include <conio.h>
int main(void)
{
    int year;
    float principal, amount, inrate, period, value;
    printf ("Please enter principal");
    scanf ("%f", principal);
    amount = principal;
    printf ("Please enter interest rate");
    scanf ("%f", inrate);
    year = 0;
    printf ("Please enter period");
    scanf ("%f", period);
        while(year <=  period)
            {printf ("%d %f\n", year, amount);
            value = amount + amount*inrate;
            year = year + 1;
            amount = value;
            }
        getch();
    return 0;
}

我尝试了 运行ning 这段代码,但我没有任何输出。有 0 个警告和消息。坦率地说,我不知道代码是否能在无法 运行 的情况下达到预期目的!请帮忙。

scanf 从stdin 读取数据并根据参数格式将它们存储到附加参数指向的位置。所以如果你想用scanf在变量中保存一些东西,你应该给指针作为参数 &.

scanf() 调用的变量参数前添加 & 地址后,它起作用了。但是我没查算术。

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int year;
    float principal, amount, inrate, period, value;
    printf ("Please enter principal ");
    scanf ("%f", &principal);                   // <-- added &
    amount = principal;
    printf ("Please enter interest rate ");
    scanf ("%f", &inrate);                      // <-- added &
    year = 0;
    printf ("Please enter period ");
    scanf ("%f", &period);                      // <-- added &
    while(year <=  period) {
        printf ("%d %f\n", year, amount);
        value = amount + amount*inrate;
        year = year + 1;
        amount = value;
    }
    getch();
    return 0;

}

I tried running this code but I have no output at all

真的吗?我收到了 6 个警告和一个段错误!你用的是什么编译器?

        ||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
        main.cpp||In function 'int main()':|
        main.cpp|8|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
        main.cpp|11|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
        main.cpp|14|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
        main.cpp|8|warning: 'principal' is used uninitialized in this function [-Wuninitialized]|
        main.cpp|11|warning: 'inrate' is used uninitialized in this function [-Wuninitialized]|
        main.cpp|14|warning: 'period' is used uninitialized in this function [-Wuninitialized]|
        ||=== Build finished: 0 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|

该代码看起来像某种利息计算器 (https://en.wikipedia.org/wiki/Interest)

试试那个代码:

    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
        int year;
        float principal, amount, inrate, period, value;
        printf ("Please enter principal ");
        scanf ("%f", &principal);
        amount = principal;
        printf ("Please enter interest rate ");
        scanf ("%f", &inrate);
        year = 0;
        printf ("Please enter period ");
        scanf ("%f", &period);
            while(year <=  period)
                {
                printf ("%d %f\n", year, amount);
                value = amount + amount*inrate;
                year = year + 1;
                amount = value;
                }
            getch();
        return 0;
    }

您遇到的问题是双重的。首先,scanf 需要一个指针来存储值。 (例如 scanf ("%f", principal); 应该是 scanf ("%f", &principal);

另一个需要注意的问题是,每次按 [Enter] 时,使用 scanf 读取值都会在输入缓冲区 stdin 中留下换行符 '\n'scanf 将读取您输入的数字,但在 stdin 中保留换行符。下次调用 scanf 时,它会在 stdin 中看到换行符(值:0xa 十六进制,10)并将其读取为下一个值。

注意:在这种情况下,%f会跳过换行符,所以没有必要。但是,请注意 scanf 读取的小数或字符串将受到影响。使用 scanf.

时请始终牢记这一点

如果遇到 scanf 似乎跳过了预期的输入,一个简单的解决方案是刷新(清空)输入缓冲区。 (下面的函数 flush_stdin 中提供了如何处理此问题的示例)。只需在每次调用 scanf 之后调用 flush_stdin,这是一个潜在的问题。

#include <stdio.h>
// #include <conio.h>

void flush_stdin ()
{
    int c = 0;
    while ((c = getchar()) != '\n' && c != EOF);
}

int main(void)
{
    int year = 0;   /* Always INITIALIZE your variables */
    float principal, amount, inrate, period, value;
    principal = amount = inrate = period = value = 0;

    printf ("Please enter principal: ");
    scanf ("%f", &principal);

    amount = principal;

    printf ("Please enter interest rate: ");
    scanf ("%f", &inrate);

    year = 0;

    printf ("Please enter period: ");
    scanf ("%f", &period);

    while(year <=  period)
    {
        printf ("%3d %10.2f\n", year, amount);
        value = amount + amount*inrate;
        year = year + 1;
        amount = value;
    }

    // getch();

    return 0;
}

输出

$ ./bin/scanf_noop
Please enter principal: 123.45
Please enter interest rate: .05
Please enter period: 24
  0     123.45
  1     129.62
  2     136.10
  3     142.91
  4     150.05
  5     157.56
  6     165.43
  7     173.71
  8     182.39
  9     191.51
 10     201.09
 11     211.14
 12     221.70
 13     232.78
 14     244.42
 15     256.64
 16     269.48
 17     282.95
 18     297.10
 19     311.95
 20     327.55
 21     343.93
 22     361.12
 23     379.18
 24     398.14