line 167

Can anyone explain why i am getting an error "no operator ">>" matches these operands" for line 167??

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

//global constants
const int MAX_RESIDENTS = 500;
ifstream textIn;

// enumerated types
enum PhoneType {HOME, WORK, CELL};

//struct
struct RecordType
{
	string socialSecurityNumber;
	string lastName;
	string phoneNumber;
	PhoneType typePhone;
};

// function prototypes
void ProgramIntro ();
bool LoadExistingData ();
bool OpenAndValidateInputFile (ifstream& textIn);
void ReadAndStoreInputFile (ifstream& textIn, RecordType ResidentRecords [], int& counter);
bool EmptyResidentList ();
void DisplayMenu ();
void ShowResidentData ();
void AddNewResident ();
void DeleteResident ();
void SaveAndExitProgram ();

int main()
{
	// local variables
	RecordType ResidentRecords [MAX_RESIDENTS];
	int counter = 0;

	ProgramIntro ();

	if (LoadExistingData ())
	{
		if (OpenAndValidateInputFile (textIn))
		{
			ReadAndStoreInputFile (textIn, ResidentRecords, counter);
		}
		else if (!OpenAndValidateInputFile)
		{
			if (!EmptyResidentList ())
			{
				system ("PAUSE");
				return 1;
			}
		}
	}
	//else if (!LoadExistingData () || EmptyResidentList ())
	//{
		//DisplayMenu ();
		//ShowResidentData ();
		//AddNewResident ();
		//DeleteResident ();
		//SaveAndExitProgram ();
	//}

	system ("PAUSE");
	return 0;
}

void ProgramIntro ()
{
	cout << setw (62) << "This program is used to manage the local police" << endl;
	cout << setw (56) << "department's file of city residents." << endl << endl << endl;
}

bool LoadExistingData ()
{
	char userInput;
	bool result = false;
	bool check = false;
	
	while (!check) 
	{
		cout << "Load existing resident data from a file (Y/N)? ";
		cin >> userInput;

		if (userInput == 'Y' || userInput == 'y')
		{
			result = true;
			check = true;
		}
		else if (userInput == 'N' || userInput == 'n')
		{
			result = false;
			check = true;
		}

		if(check == false) 
		{//display error message
			cout << "Invalid entry! Please try again." << endl << endl;
		}
	}

	return result;
}

bool OpenAndValidateInputFile (ifstream& textIn)
{
	// open input file
    textIn.open ("RESIDENTS.TXT");

	// check if file opens successfully
	// if not, print a warning and exit program from main with a return code 1
	if (!textIn)
	{
		cout << "Error! File cannot be opened!" << endl << endl;
		return false;
	}
	else
	{
		return true;
	}
}

bool EmptyResidentList ()
{
	char userInput;
	bool result = false;
	bool check = false;
	
	while (!check) 
	{
		cout << "Would you like to continue the program with an empty resident list (Y/N)? ";
		cin >> userInput;

		if (userInput == 'Y' || userInput == 'y')
		{
			result = true;
			check = true;
		}
		else if (userInput == 'N' || userInput == 'n')
		{
			result = false;
			check = true;
		}

		if(check == false) 
		{//display error message
			cout << "Invalid entry! Please try again." << endl << endl;
		}
	}

	return result;
}

void ReadAndStoreInputFile (ifstream& textIn, RecordType ResidentRecords [], int& counter)
{
	// while file is still readable, and peek does not find end-of-file
	while (textIn.good () && textIn.peek () != EOF)
	{
		// read in data from RESIDENTS.TXT
		textIn >> ResidentRecords [counter].socialSecurityNumber;
		textIn >> ResidentRecords [counter].lastName;
		textIn >> ResidentRecords [counter].phoneNumber;
		textIn >> ResidentRecords [counter].typePhone;
	}

}
files do not save enum type.
I never used enums but I think write function might solve your problem.Not sure
This is coming from a noob on file handling, but I always get the same error message when I get my 'ifstream's and 'ofstream's mixed up. So you might want to double check that.
If you are reading IN data from textIn then why are you using the >> operator? Why not use something like the following?

1
2
3
4
5
std::string buf;
while(getine(textIn,buf)  && textIn.peak()!=EOF){
 ...
}


One you read the line into buf you use a variety of searches on the string buf to find the data you are looking for. For example, if your data is organized by line as follow.

1
2
3
RESIDENTS.TXT

socialSecurityNumber,latName,phoneNumber,typePhone


You might modify your while statement to the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string buf;
int pos;
while(getline(fileIn,buf){
    pos=buf.find(','); //Searching the comma separator. Pos now stores its position
	ResidentRecords[counter].socialSecurityNumber=buf.substr(0,pos);
	buf.erase(0,pos+1); //We use +1 to erase the commas as well
	pos=buf.find(','); //Find the next comma
	ResidentRecord[counter].lastName=buf.substr(0,pos);
	buf.erase(0,pos+1);
	pos=buf.find(',');
	ResidentRecord[counter].phoneNumber=buf.substr(0,pos);
	buf.erase(0,pos+1);
	/*Having no more commas we can just store the rest of buf into the struct*/
	ResidentRecord[counter].typePhone=buf;	/*You have typePhone as an enum so you might need to convert this to an int or some other data type depending on your needs./*
	counter++;
	/*The loop will now start back over with the NEXT line of data. It looks like you need to incriment counter, which you did not do here in this function.*/
}


In truth I usually do it slightly differently than what I have shown above. I nest another while loop WITHIN the one above as follow

1
2
3
4
5
while(getline(textIn,buf){
	while (!buf.empty()){
		//Retrieve data here	
	}
}


The two while loop method may require the use of a different data type so you may prefer not to use it.
The answer to his original problem is answered above.There is no standard operator in fstream library that takes right hand operand as enum type.

But you can save its elements by using there name as used for calling.

Try using write function for enum type(not sure about this one).It works for class and structs
I had a guy at work help me out, and he gave me this...it seems to work
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
void ReadAndStoreInputFile (ifstream& textIn, RecordType ResidentRecords [], int& counter)
{
	// while file is still readable, and peek does not find end-of-file
	while (textIn.good () && textIn.peek () != EOF)
	{
		// if the file contains too many lines of data, stop reading data and issue warning
		if (counter >= MAX_RESIDENTS)
		{
			cout << "Error: file contains too many lines of data" << endl;
		}
		else
		{
			// read in data from RESIDENTS.TXT
			textIn >> ResidentRecords [counter].socialSecurityNumber;
			textIn >> ResidentRecords [counter].lastName;
			textIn >> ResidentRecords [counter].phoneNumber;
			textIn >> (int&) ResidentRecords [counter].typePhone;

			counter++;
		}
		
		// read newline
		textIn.get ();
	}
}
Topic archived. No new replies allowed.