0

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!

  • "Doesn't work" is not a good error description. – Brave Sir Robin Feb 08 '14 at 16:06
  • Ok, so when I say it doesnt work, It will read in the table, ask for user input, but it will put out 0, reguardless of user input. The only other part it can do is exit when the user enter 'none'. – user3287431 Feb 08 '14 at 16:44
  • 1
    Please read [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). Note that `scanf()` will return a number smaller than 4 when it fails to read all the fields. One problem is that you do not actually read the data because your `scanf()` string does not handle the `-` surrounded by spaces. You should check `if (scanf(...) != 4)` to cover the error cases better. When debugging, the most basic technique is to print the data you read to make sure you actually read what you think you are reading. (The `i=0` in the loop is also wrong.) – Jonathan Leffler Feb 08 '14 at 16:47
  • The file that is being read from does not have those hyphens. It is just spacing. I have it like that so the format stays right in the post. – user3287431 Feb 08 '14 at 17:00

1 Answers1

1

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, e.g. 4.

Brave Sir Robin
  • 1,046
  • 6
  • 9
  • Ah, I didn't see that in the loop. So I fixed that, and still having issues. :\ Also, the program is supposed to account for numbers in between. Ex: I enter 4 and the code is supposed to use the info under three. Its all ranged. so it's 1, 2, 3-5, 6-8. 9-11 etc – user3287431 Feb 08 '14 at 16:32
  • 1
    This is one of the problems — it is not the only problem. – Jonathan Leffler Feb 08 '14 at 16:50