0

This is the program that needs to write a code to read data from file. The file looks like:

1234 200.55
5678 1234.56
9876 2.33

I need to store the first number as the account number and the second one as the balance of the account.

#include<iostream>
#include<fstream>
using namespace std;

const int MAX_NUM=100;
int read_accts(int acctnum[], double balance[], int max_accts);

int main()
{
    int acctnum[MAX_NUM];
    double balance[MAX_NUM];
    int max_accts=0;
    int num_accts;

    num_accts = read_accts(acctnum,balance,max_accts);

    return 0;  
}

int read_accts(int acctnum[],double balance[],int max_accts)
{
    ifstream infile;
    infile.open("information");

    while (infile >> acctnum[max_accts] && max_accts < MAX_NUM)
    {
        infile >> balance[max_accts];
        max_accts++;
    }
    for (int i=0; i<=max_accts; i++)
    {
        cout << acctnum[i]<<endl;
        cout << balance[i]<<endl;
    }
    infile.close();  
    return max_accts;
}

The output of this program is

0
0

It should be same as the text file has. it shouldn't be 0 and 0.

Thanks in advance Any help is appreciated.

0

3 Answers 3

3

First of all, you're printing one too many elements in each array. The printing loop should run from 0 to max_accts (not including), like so:

 for (int i=0; i < max_accts; i++)

Now, since you're attempting to read a file named "information", I've created such file with the contents described in your question and ran your code. The output I get is:

1234
200.55
5678
1234.56
9876
2.33

This looks like what you wanted to get (and not zeroes), so your code seems to be working alright. Perhaps you're not opening the right file?

Either correct the name of the file from "information" to something else, or make sure you have a file named "information" in the current working directory.

Sign up to request clarification or add additional context in comments.

Comments

0

For the file part of you program, the code below would be a better version of your function. It uses end of file and has the ability to handle of the file doesn't exits

int read_accts(int acctnum[], double balance[], int max_accts)
{
    ifstream infile;
    infile.open("information");

    if (!infile)//check if file exists
    {
        //add code to handle missing file here
    }
    while (!infile.eof())
    {
        infile >> acctnum[max_accts];
        infile >> balance[max_accts];
        max_accts++;
    }
    infile.close();
    return max_accts;
}

1 Comment

Thanks for heads up, forgot to remove excess when i copied and pasted from my IDE
-1

How about using the old school stdio API for a simple task like that?:

int accounts[3], i;
float balance[3];
FILE *fp = fopen("file.txt", "r");
for (i = 0; i < 3; i++)
   fscanf(fp, "%d %f", &account[i], &balance[i]);
fclose(fp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.