Can you not

Do this
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 10;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string city;
	string zip;
	string phone;
};


// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{
	
	for( string::size_type i = 0; i < 1; i++)
	{
		fin >> tele[i].fname;
		fin >> tele[i].lname;
//This=>        getline( fin, tele[i].streetAdd);
	}

}

In class my teacher used all char's can you not do this? If I have to get a line of words how can I do it? Thanks
What you're doing is perfectly legitimate. Though passing that size parameter as an integer reference is a bit awkward.
I can;t get my information when I do it though? What do you mean that size parameter as an integer reference is a bit akward?
I can;t seem to get this third line of info.
Thanks
Trying to read this line

Ann Arbor Mi, 48719
I had to put fin.get() before my issue line above. Why is that?
Ah, when you use fin>>, it stops at a newline but leaves it there. So when you next call getline() it immediately reads that newline and returns an empty string.
Cool Thanks, I got another question too. Like I said I'm trying to read from a file All my junk in main right now is just trying to see if I get everything I need from the file.
Right now all I get is the same thing repeated over and over. It works for the first set of info then it does it over and over. I cant figure out why. I have the idea that it is because the index is not updating in my readData function


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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 10;

//structure
struct infoType
{	
	string fname;
	string lname;
	string streetAdd;
	string cityStateZ;
	string phone;
};


// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{	
	
	int i = 0;
	while(!fin.eof())
	{
		fin >> tele[i].fname;
		fin >> tele[i].lname;
		fin.get();
		getline(fin, tele[i].streetAdd, '\n');
		getline(fin, tele[i].cityStateZ, '\n');
		getline(fin, tele[i].phone, '\n');
	
		i++;
	}

}



void displayMenu()
{
	cout << "Phone Directory Program " << endl;
	cout << endl;
	cout << "Options" << endl;
	cout << "[A]dd and entry to the phone directory. " << endl;
	cout << "[D]elete an entry from the phone directory. " << endl;
	cout << "[U]pdate an entry from the phone directory. " << endl;
	cout << "[L]ist the entire phone directory. " << endl;
	cout << "[E]xit the menu" << endl;
}



int main()
{
	ifstream fin("input.txt");
	
   int newSize = 0;
   infoType tele[SIZE];
int j = 0;	
   do

   {
	 readData(fin, tele, newSize );

	 for(int i = 0; i < 1; i++)
	 {
		 cout << tele[i].fname << endl;

	 }
  
 
	 for(int i = 0; i < 1; i++)
	 {
		 cout << tele[i].lname << endl;

	 }
	 
	  for(int i = 0; i < 1 ; i++)
	 {
		 cout << tele[i].streetAdd << endl;

	 }

for(int i = 0; i < 1; i++)
	 {
		 cout << tele[i].cityStateZ << endl;

	 }
for(int i = 0; i < 1; i++)
	 {
		 cout << tele[i].phone << endl;

	 }
	 j++;
   }while( j < 3);
   
	 
//displayMenu();
 
  system("pause");
 
 return 0;

}
OK I see now, but there is an issue. I changed it so it works except.

There are 5 lines of names

If I change

1
2
3
4
5
for(int i = 0; i < 1; i++)
	 {
		 cout << tele[i].fname << endl;

	 }

i<3 works fine I get the names I'm supposed to. When I make i<4 I get an issue because instead of the next name I get a phone number. All lines of input are of length 4 for one address. So its not missing a line of input. Whats going on with this ?
Actually got it. It is becaue of the input an inital getting in the way. Thanks ne ways
Topic archived. No new replies allowed.