Gianni
Gianni

Reputation: 11

2D array printing

So, I am creating a program that reads in a file called "Bankfile.txt" and the the first number "3" is to indicate how many account we are working with. The numbers "123" "467" and "499" are the bank account numbers and the numbers next to each is their original balances. In my code I am using a 2D array to scan them in. I think I have everything correct in scanning them in but when I run the program the account numbers are being printed very weird as seen here. Any ideas on why it is being printed like this?

Thank you!

#include <stdio.h>


int main(){
FILE* file;
file = fopen("bankfile.txt","r");
int accounts,b=1,w=2,d=3,u=4,i,j,accNum,origBal;
float interest = .0185; 
float test;
float accountInfo[accNum][origBal];

fscanf(file, "%d", &accounts);

for(i = 0; i < accounts; i++)
      {
      fscanf(file, "%d", &accountInfo[i]);
      for(j = 0; j < 1; j++)
            {
            fscanf(file, "%f", &accountInfo[i][j]);
             printf("Account %d has a balance of $%.2f\n", accountInfo[i], accountInfo[i][j]);
            }

      } 

system("pause");
return 0;
}

Upvotes: 0

Views: 51

Answers (2)

user2371524
user2371524

Reputation:

Ok what you have here is not a 2-dimensional array -- this is conceptually wrong. An account number only has one balance associated with it. So what you have is only a single dimension, but your data has several fields ... that's where you use a struct in C. Here's some example code that would produce the output you expect:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* data structure holding a bank account */
typedef struct account
{
    int id;
    double balance;
} account;

int main(void)
{
    int cap = 1024; /* initial capacity of account array */
    int count = 0;  /* number of accounts */
    char buf[1024]; /* buffer for a line of text */
    char *tok;      /* token from text line */
    FILE *bankfile;
    int i;

    account *accounts = malloc(cap * sizeof(account));

    bankfile = fopen("bankfile.txt", "r");

    while (fgets(buf, 1024, bankfile))
    {
        int accId;
        tok = strtok(buf, " \t\r\n");
        if (!tok) continue;

        accId = atoi(tok);
        if (accId > 0)
        {
            /* first token in line was a positive integer */

            tok = strtok(0, " \t\r\n");
            if (tok)
            {
                /* there was a second token in the same line, then we found
                 * an account with a balance */

                accounts[count].id = accId;
                accounts[count].balance = atof(tok);

                if (++count == cap)
                {
                    cap *= 2;
                    accounts = realloc(accounts, cap * sizeof(account));
                }
            }
        }
    }

    fclose(bankfile);

    for (i = 0; i < count; ++i)
    {
        printf("Account %d has a balance of $%.2f\n",
                accounts[i].id, accounts[i].balance);

    }

    free(accounts);
    return 0;
}

This can be simplified by first reading the first line and then only allocating as many elements of account as needed.

of course, for production, add error checking to fopen() and malloc() and friends ...

Upvotes: 1

Linus
Linus

Reputation: 1518

First of all I think you don't understand the concept of two-dimensional arrays. When you have a 2D array (foo) and add two arrays into that it will look something like this.

int foo[][] = { {1,2,3} {4,5,6} }; 

And when you call foo[0] it will actually refer to the first array you added (i.e [1,2,3]) and foo[1] will refer to the second array. You should instead use two seperate arrays called accountNum and accountBal.

Finally you never give values to accNum or origBal which makes your 2D array an empty array. So you could instead dynamically allocate memory for them based on the accounts variable.

Keeping this in mind you should change your main logic to solve these issues.

fscanf(file, "%d", &accounts);
accountNum = malloc(sizeof(int) * accounts);
accountBal = malloc(sizeof(float) * accounts);

for(i = 0; i < accounts; i++) {
  fscanf(file, "%d", &accountNum[i]);
  fscanf(file, "%f", &accountBal[i]);
  printf("Account %d has a balance of $%.2f\n", accountNum[i], accountBal[i]);
}

free(accountBal);
free(accountNum);

Upvotes: 0

Related Questions