Loop not working



The idea behind this assignment was to declare 5 structs in an array that hold information read from a file.

I made the struct/array, and it reads from the file, but only the top line, again and again....

Thanks for any advice/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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <istream>
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


int main ()
{
	struct Bankinfo{
		string name;
		int accountnum;
		float checking;
		float savings;
		string phone;
	} bankinfo[5];


	int i;
	i=0;
{cout<<"This is a test program"<<endl;}
char x;
x=0;
while (i<5)
{ifstream infile;
char testinfo [10001];
infile.open("testinfo.txt");
cin.get (testinfo,10001);
cout<<testinfo<<endl;

infile>>bankinfo [x].name>>bankinfo [x].accountnum>>bankinfo [x].checking>>bankinfo [x].savings>>bankinfo [x].phone;

cout<<setw (10)<< (bankinfo[x].name);
cout<<setw (10)<<(bankinfo [x].accountnum);
cout<<setw (10)<<(bankinfo [x].checking);
cout<<setw (10)<<setprecision (2)<<fixed<<(bankinfo [x].savings);
cout<<setw (15)<<(bankinfo [x].phone);
i++;
}
cout<<"                                             "<<endl;
cout<<"Thanks for using the program"<<endl;

			return (0);
}
 
maybe it read only bankinfo[x].name because you havent space between bankinfo and [x] .. try without space between them.. ;)
Surely you mean x to be an int, not a char? Where do you change the value of x to read a different member of the array? If you only use the first member of the array, why did you make the array with a size of 5? This is all very confused code.
Last edited on
1
2
3
4
5
6
7
8
while (i<5)
{ifstream infile;
char testinfo [10001];
infile.open("testinfo.txt");
cin.get (testinfo,10001);
cout<<testinfo<<endl;
//code omitted for brevity
}

I've only glanced at your code but I think the issue lies within your while loop.

If you look at line 2 of the while loop (in this reply) you have declared the variable ifstream infile inside the while loop. This means everytime you iterate through, you are creating and accessing a new instance of infile.

istream::get(...) extracts data from the stream (ifstream infile), so when the while loop iterates it extracts the next lot of data from the stream. This however is not possible if you are constantly creating a new stream (this is a corollary of having the variable declaration inside the while loop).

You need to declare ifstream infile and open the file outside of the while loop.

Here is a link for istream::get(...): http://www.cplusplus.com/reference/iostream/istream/get/

Additionally I've noticed some peculiarities in your code:

On line 23: {cout<<"This is a test program"<<endl;}, why is this cout statement inside parentheses? You can remove them as they only serve to make your code illegible.

On line 26: while (i<5), I assume 5 relates to the number of lines in your input file. This may work now, however a better solution might be to use ifstream::good() (see: http://www.cplusplus.com/reference/iostream/ios/good/). These sorts of magic numbers are bad because should the format of your input file change, you will need to update your code, by using ifstream::good() you eliminate this problem..

You may also want to test that the file opened successfully by using fstream::is_open() (see: http://www.cplusplus.com/reference/iostream/fstream/is_open/)

Finally, consider looking into coding standards to make your code more readable.

I hope this helps!

P.S: You do not need the
1
2
#include <istream>
#include <ostream> 

as these are included in
#include <iostream>
(see: http://www.cplusplus.com/reference/iostream/)
Last edited on
Topic archived. No new replies allowed.