我的简单代码没有';我不能给出结果 #包括 #包括 内部主(空) { 国际年; 浮动本金、金额、利率、期限、价值; printf(“请输入委托人”); scanf(“%f”,主体); 金额=本金; printf(“请输入利率”); scanf(“%f”,单位为英寸); 年份=0; printf(“请输入期间”); scanf(“%f”,期间); 虽然(year

我的简单代码没有';我不能给出结果 #包括 #包括 内部主(空) { 国际年; 浮动本金、金额、利率、期限、价值; printf(“请输入委托人”); scanf(“%f”,主体); 金额=本金; printf(“请输入利率”); scanf(“%f”,单位为英寸); 年份=0; printf(“请输入期间”); scanf(“%f”,期间); 虽然(year,c,output,C,Output,scanf从stdin读取数据,并根据参数格式将其存储到附加参数所指向的位置。因此,如果要使用scanf保存变量中的某些内容,应在scanf()中变量参数之前添加的&地址后,使用&.将指针作为参数调用,它可以工作。但我没有检查算法 #include <stdio.h> #include <conio.h> int main(void) { int year; float principal, amount, inrate, period, value;

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);
    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;
}
#包括
#包括
内部主(空)
{
国际年;
浮动本金、金额、利率、期限、价值;
printf(“请输入委托人”);
scanf(“%f”,&principal);//
我试着运行这段代码,但根本没有输出

真的吗?我得到了6个警告和一个segfault!你在用什么编译器

#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;
代码看起来像某种兴趣计算器()

请尝试以下代码:

        ||=== 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)) ===|
#包括
#包括
内部主(空)
{
国际年;
浮动本金、金额、利率、期限、价值;
printf(“请输入委托人”);
scanf(“%f”和“主体”);
金额=本金;
printf(“请输入利率”);
scanf(“%f”、&inrate);
年份=0;
printf(“请输入期间”);
scanf(“%f”、&period);

而(年您遇到的问题有两个。第一个,
scanf
需要一个指向存储值的指针。(例如,
scanf(“%f”,principal);
应该是
scanf(“%f”,&principal);

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

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

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

    #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
一起使用。使用
-Wall
选项进行编译。使用调试器一次一行地遍历代码。
%f
格式跳过诸如换行符、空格和制表符之类的空白,而没有任何问题。
刷新\u stdin()
call在这里是过分的,特别是因为你没有检查输入是否有效。作为错误处理策略的一部分,这是有意义的。当然。在连续调用中混合字符串和数字会让你丧命。但是当提供示例帮助新手时,过分的做法并不都是坏事。我要提醒你。谢谢。
#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