read from file and display

Hi all,

I'm taking a C++ programming course and I'm having a problem completing another one of the assignments...

Requirements: Create a file called accounts.dat that has account numbers (int) and balances (double) and program to display the following information from accounts.dat created above:
a. All accounts with balances less than zero
b. All accounts with balances greater than zero

Here's what I've written but it doesn't neatly group the accounts with balances less than zero and balances greater than zero.

Should I go about creating two files (accounts_pos.dat) and (accounts_neg.dat) to display the data? Or is there a way to neatly organize the data read from file and display as required above?

As always, thank you for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
using namespace std;
int main()

{
    int acct_num = 0;
    double balances;
    //open the file for reading
    fstream inp;
    inp.open("accounts.dat");
    //check to see if file is readable
    if(!inp.good())
    {
        cout << "Unable to read file\n";
        return 0;
    }
    while(!inp.eof())
    {
        inp >> acct_num >> balances;
                if (balances<=0)
        {
           cout << "Account Number\tBalance" << endl;
           cout << acct_num <<"\t" << balances << endl;
        }
        else
        {
            cout << "Account Number\tBalance" << endl;
            cout << acct_num <<"\t" << balances << endl;
        }
    }
    inp.close(); //close file handle
    return 0;
}


When I run this, I get the account number and balance for all the accounts but in no particular order.

Here is what is displayed:


Account Number  Balance
11112   1000
Account Number  Balance
11113   5000
Account Number  Balance
11114   6000
Account Number  Balance
11115   -500
Account Number  Balance
11116   -900
Account Number  Balance
11117   700
Account Number  Balance
11118   10
Account Number  Balance
11119   -5
Account Number  Balance
11120   0
Last edited on
please post an example of the text in the accounts.dat


to convert string to int use atoi
to convert string to float use atof
Last edited on
Try defining a struct that holds the details:

1
2
3
4
struct account {
    int acct_num;
    double balance;
};


Then declare two containers to hold copies of that struct - one for balances greater than or equal to zero, and one for balances less than zero, as well as an individual struct to read into on each iteration of the loop. Once you've read into the struct, you can check the balance and decide which container to add it to. Once you've read the whole file, you'll have two containers with the accounts grouped, which you can use as you like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 #include <iostream>
#include <fstream>
#include <string>
#include <deque>
using namespace std;


struct account
{
	int         iD;
	double balance;
};

	int main()
	{
		deque<account>    buffer_account;

		string  buffer_str;
		string  buffer_substr;
		string  buffer_strNr;

		ifstream myFile;
		myFile.open("accounts.dat",ifstream::in);//deschide fila 

		if(!myFile)
		{
			cout<<"File not found!!"<<endl;
			system("pause");
			return 0;
		}

		//insert data in buffer_str;
		buffer_str += myFile.get();
		while (myFile.good())
		{
			buffer_str+= myFile.get();
		}
		//////////////////////////////////////////
		myFile.close();//close file
		


		for(int i=0; i<buffer_str.size(); i++)
		{
			if (buffer_str[i] == '\n' || i == (buffer_str.size()-1) ) 
			{
			if (buffer_substr.substr(0,2) == "a ")
				{
					buffer_account.resize(buffer_account.size() + 1) ;

					for( int j=2; j<buffer_substr.size(); j++ )
					{
					    buffer_strNr += buffer_substr[j];
						
						//Set id
						if( buffer_substr[j] == ' ' &&
							buffer_substr[j-1] != ' ')
						{
							buffer_account[buffer_account.size() -1 ].iD=atoi(&buffer_strNr[0]);
							buffer_strNr.clear();
							///<<<<<<<< id >>>>>>>>>>>>///
							
							for( int k=j; k<buffer_substr.size(); k++ )
							{
								buffer_strNr += buffer_substr[k];
								
								//Set Balance
								if( k == buffer_substr.size()-1 )
								{
									buffer_account[buffer_account.size() -1 ].balance=atof(&buffer_strNr[0]);
									buffer_strNr.clear();
								}
								///<<<<<<<< Balance >>>>>>>>>>>>///
								j=k;
							}
						}
					}
				}
            #pragma endregion
			buffer_substr.clear();
			}

			else if(buffer_str[i] != '\n' )
			{
				buffer_substr +=buffer_str[i];
			}

		}

		cout<<"# Total number of accounts :  "<<buffer_account.size()<<endl;
		
		cout<<'\n'<<"accounts grater than 0 :"<<'\n';
		for(int i=0 ; i< buffer_account.size(); i++)
		{
			if(buffer_account[i].balance>0)
				cout<<"a"<<"  id:"<<buffer_account[i].iD<<"   balance:"<<buffer_account[i].balance<<endl;
		}


		cout<<'\n'<<"accounts less than 0 :"<<'\n';
		for(int i=0 ; i< buffer_account.size(); i++)
		{
			if(buffer_account[i].balance<0)
				cout<<"a"<<"  id:"<<buffer_account[i].iD<<"   balance:"<<buffer_account[i].balance<<endl;
		}

		buffer_account.clear();
		system("pause");
		return 0;
	}



accounts.dat :
1
2
3
4
5
6
7
8
9
10
#accounts    #ID     #Balance
a              1         22.5
a              2       -33.79
a              3       543.32
a              4         0.00
a              5      675.599
a              6        -89.1
a              7         0.00
a              8          4.0
a              9       -435.0



IDE used Microsoft Visual C++ 2010 Express Edition
Last edited on
Here is what I have inside accounts.dat

1
2
3
4
5
6
7
8
9
11112 1000
11113 5000
11114 6000
11115 -500
11116 -900
11117 700
11118 10
11119 -5
11120 0
So in the end I got what I wanted by closing the file I'm reading from then re-opening it.

Thank you all for your time.
Topic archived. No new replies allowed.