Text File into four parallel array

Hello all
My sad little program which compiles but provides no result.
this is an assignment which has way more than this
right now im just trying to get output so i can start working out on sorting and adding and all.
I would really appreciate all your help
thank you
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
#include <iostream> 
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
using namespace std;
int Account_number[100];
double balance[100];
double limit[100];
string name[100];

string in="cardinfo.txt";

 ifstream infile;
 infile.open(in.c_str());	

	 int i = 0;

 while(not infile.eof()) 
 {
getline (infile,name[i]);
infile>> Account_number[i]>> balance[i]>>limit[i];
   i++; //count how many records you have
 }

	 infile.close();

  for(int n=0; n<13; n++)
  {
    cout << Account_number[n] << "  " << Fname[n] << "  "
     << Lname[n] << "  " << balance[n]<<" "<<" "<<limit[n];
 }
return 0;
}


The text file im trying to read
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
Sandy Cannon
0000111134098567
540.77
1000.00
Mary Smith
2222333345789515
22535.23
21000.00
Mark Edwards
7777888890098654
1234.87
1500.00
Linda Lewis
4444555567778451
20701.10
50000.00
Alex Yorke
2222333345056111
4545.55
3000.00
Roberta Flack
0000111134998334
15.45
7500.00
Katie Cello
7777888890110091
6722.11
5575.00
Eugenia Allen
2222333345224217
10999.95
23000.00
Sarah Unser
7777888890008712
8892.12
12500.00
Marcie Oppenheimer
4444555567451007
4044.43
4250.00
Tom Ullman
4444555567336875
12533.37
10000.00
Harrison Ford
0000111134927262
95672.22
100000.00
Last edited on
1
2
cout << Account_number[n] << "  " << Fname[n] << "  "
     << Lname[n] << "  " << balance[n]<<" "<<" "<<limit[n];

where are Lname and Fname come from?

but the problem is, that after ...>limit[i]; you are reading the '\n' with the next getline(infile, name[i]);.
you can use infile.ignore() to skip the newline. that worked for me.
Last edited on
This is one solution, i recommend you to replace atoi() and atof() calls with stringstream classes:

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char argv[])
	{
	vector<int> Account_number;
	vector<double> balance;
	vector<double> limit;
	vector<string> name;
	string in = "cardinfo.txt";

	ifstream infile;
	infile.open(in.c_str());
	if(!infile.is_open()){
		cout << "file not found" << endl;
		return EXIT_FAILURE;
		}

	string buf;
	int j = 0;
	while(getline (infile, buf))
		{
		switch(j){
		case 0:
			name.push_back(buf);
			break;
		case 1:
			Account_number.push_back(atoi(buf.c_str()));
			break;
		case 2:
			balance.push_back(atof(buf.c_str()));
			break;
		case 3:
			limit.push_back(atof(buf.c_str()));
			break;
		default:
			break;
			}
		j++;
		if(j > 3)
			j = 0;
		}

	infile.close();
	
	for(size_t n = 0; n < name.size(); n++)
		{
		cout << Account_number[n] << " " << name[n] << "  " << balance[n] <<" "<<" "<< limit[n] << endl;
		}
	return 0;
	}
Oh yea sorry mathes,
it was suppose to be one name then account # and so on.
well i replaced both part of codes with
1
2
3
4
5
6
7
while(not infile.eof()) 
 {
getline (infile,name[i]);
infile>> Account_number[i]>> balance[i]>>limit[i];
infile.ignore(); 
 i++; //count how many records you have
 }

and
1
2
3
4
5
for(int n=0; n<13; n++)
  {
   cout << Account_number[n] << "  " << name[n] 
   <<"  " << balance[n]<<" "<<" "<<limit[n];
 }


i also added cout<<i;
just to see where it get stucks and im pretty sure its during reading the fille.
but i still get blank response no error while compiling but nothing while running either

Hello modoran,
ur code works perfectly but i have no clue about string bufs
and in further i have to sort the everything one by one or add new account,
so rite now just working with the basics..
Thanks for the help though
Topic archived. No new replies allowed.