简体   繁体   中英

Issue with variables and/or arrays

So I am working on a C code that will take user input (in the form of a deposit amount[like a bank] and the time the deposit will remain.) and put out a "interest rate", or more simply, just a decimal number. I am not sure why it wont work. It's supposed to read a file I have into an array, and do its calculations based off of that array. When I say it doesn't work, It will read in the table, ask for user input, but it will put out 0, regardless of user input. The only other part it can do is exit when the user enter 'none'. Here's the code:

#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);
}

And the file the program is reading from has this chart:

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

Ok, I figured it out. Firstly, the i = 0 was in the loop, causing it to be repeated (thanks rmartinjak). The other part was I had:

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

This made it so it would keep increasing j, even when it was past time. So I simply changed the '<' to '>':

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

and I removed the '=' from the lines like this:

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

to this:

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

Everything seems fine now. Thanks again for everyone's help!

The problem is that you set i to 0 in every iteration of the loop reading the file.

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++;
}

So only to month[0] will be modified. This will likely cause the loop

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

to increment j beyond the bounds of month if you enter something else than 84 as the amount of months. You should check the bounds of month anyway, in case someone enters a number of months not in the list, eg 4 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM