我的简单代码没有输出

时间:2015-04-04 19:27:43

标签: c 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;
}

我尝试运行此代码,但根本没有输出。有0个警告和消息。坦率地说,我不知道代码是否能够达到预期目的而无法运行它!请帮忙。

4 个答案:

答案 0 :(得分:2)

  

我尝试运行此代码,但我根本没有输出

真的?我有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;
    }

答案 1 :(得分:1)

scanf从stdin读取数据并根据参数格式将它们存储到附加参数指向的位置。因此,如果您想使用scanf在变量中保存某些内容,则应将指针作为参数与&amp;。

一起使用

答案 2 :(得分:1)

&调用中的变量参数之前添加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;

}

答案 3 :(得分:0)

你遇到的问题是双重的。第一个,scanf需要一个指向存储值的指针。 (例如scanf ("%f", principal);应为scanf ("%f", &principal);

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

注意:在这种情况下,%f会跳过换行符,因此没有必要。但请注意,scanf读取的小数或字符串将受到影响。使用scanf时始终牢记这一点。

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

#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