c - 变量和/或数组的问题

标签 c arrays variables

所以我正在开发一个 C 代码,它将接受用户输入(以存款金额的形式[像银行]和存款保留的时间。)并给出一个“利率”,或者更简单地说,只是一个十进制数。我不知道为什么它不起作用。它应该将我的文件读入数组,并根据该数组进行计算。当我说它不起作用时,它会读取表,要求用户输入,但无论用户输入如何,它都会输出 0。它唯一能做的其他部分是当用户输入“none”时退出。 代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
FILE *unit2;

int main()
{
    int month[14];
    float num1[14];
    float num2[14];
    float num3[14];
    float numin1, numin2, numin3;
    char input[100];
    int time;
    float amount;
    float output = 0;
    int numin;
    int i, j;
    int nret;

    unit2 = fopen("units2.txt", "r");

    printf("Months\t\t\t     Interest\n");

    while(!(feof(unit2)))
    {
        i = 0;
        nret = fscanf(unit2, "%d %f %f %f", &numin, &numin1, &numin2, &numin3);
        if(nret == EOF)
        break;
        month[i] = numin;
        num1[i] = numin1;
        num2[i] = numin2;
        num3[i] = numin3;
        printf("  %d\t\t%1.2f %1.2f   %1.2f %1.2f   %1.2f %1.2f\n", numin, numin1, numin1, numin2, numin2, numin3, numin3);
        i++;
    }

    printf("\nEnter your deposit amount, followed by the duration in months. Or, type 'none'  to exit.\n");
    gets(input);
    sscanf(input, "%f %d", &amount, &time);

    if(!strcmp(input, "none")){
        printf("\nCome back later!\n");
        exit(0);
    }

    if(time <= 0){
        printf("Please enter a larger amount of months.\n");
    }
    if(time >= 120){
        printf("Please enter a smaller amount of months\n");
    }
    if(amount <= 999){
        printf("Please enter a larger deposit.\n");
    }

    j = 0;
    while(time != month[j]){
        j++;
    }

    if(amount >= 1000 && amount <= 9999){
        output = num1[j];
    }
    else if(amount >= 10000 && amount <= 99999){
        output = num2[j];
    }
    else if(amount >= 100000){
        output = num3[j];
    }

    printf("Your interest rate will be %f.\n", output);
}

程序正在读取的文件具有以下图表:

1  - 0.02  0.03  0.05
2  - 0.02  0.03  0.05
3  - 0.02  0.05  0.10
6  - 0.05  0.10  0.15
9  - 0.05  0.10  0.15
12 - 0.05  0.15  0.20
18 - 0.15  0.25  0.30
21 - 0.15  0.25  0.30
24 - 0.15  0.25  0.30
30 - 0.15  0.25  0.30
36 - 0.15  0.35  0.40
48 - 0.25  0.45  0.50
60 - 0.35  0.55  0.60
84 - 0.35  0.55  0.60
<小时/>

好吧,我明白了。首先,i = 0 在循环中,导致它被重复(感谢 rmartinjak)。另一部分是我的:

while(time < month[j]){
j++;
}

这使得 j 会不断增加,即使已经过去了。 所以我只是将 '<' 更改为 '>':

while(time > month[j]{ \\so it will stop when at time.
j++;
}

我从这样的行中删除了“=”:

if(amount >= 1000 && amount <= 9999){

对此:

if(amount > 1000 && amount < 9999){

现在一切看起来都很好。再次感谢大家的帮助!

最佳答案

问题在于您在读取文件的循环的每次迭代中将 i 设置为 0

while(!(feof(unit2)))
{
    i = 0;   // <-- HERE
    nret = fscanf(unit2, "%d %f %f %f", &numin, &numin1, &numin2, &numin3);
    if(nret == EOF)
        break;
    month[i] = numin;
    num1[i] = numin1;
    num2[i] = numin2;
    num3[i] = numin3;
    printf("  %d\t\t%1.2f %1.2f   %1.2f %1.2f   %1.2f %1.2f\n", numin, numin1, numin1, numin2, numin2, numin3, numin3);
    i++;
}

所以只有month[0]会被修改。这可能会导致循环

while(time != month[j]){
    j++;
}

如果您输入的月份数不是 84,则将 j 增加到超出 的范围。无论如何,您应该检查月份的范围,以防有人输入列表中没有的月份数,例如4

关于c - 变量和/或数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647751/

相关文章:

c - 作为参数传递时数组发生变化

javascript - 条件证明令人困惑

javascript - 如何检查 if 语句的条件以查看其是否正常工作?

c - 调用者看不到函数中的变量更改?

ms-access - VBA MS Access : Function in a Private Sub to access Sub variables

union 访问成本与使用基本类型

c - 用 C 语言打印和显示单位矩阵的程序

c++ - 从命令行读取文件作为输入

c - 矩阵 C 中的运行时错误

c - 如何在函数体内引用与局部变量同名的全局变量?