Program problem reading specific lines from a vector in a file in c++

Hello all! I am new to the language of C++ and am taking a summer class on Data Structures. MY professor has not been helpful or responsive to any questions I've had so I thought I would try my luck here.

I believe I have completed up to step 5 correctly in the instructions but I am confused as to search and find all the specific strings in the input file for #6. Any help would be much appreciated!

The prompt reads as follows.

1. Define struct Dining containing the following 3 fields and 2 functions
a. name of dining location (string)
b. coordinate x (double)
c. coordinate y (double)
d. default constructor Dining()
e. constructor with parameters: Dining(const string& diningName, double x, double y)
2. Define readData() function that reads 3 fields from an input file to an instance of Dining
3. Define printData() function that prints 3 fields of an Dining instance to an output file
4. Define a vector of Dining elements
5. Read and process the first part of the input file
a. read data to an instance of Dining o push it to the vector
b. repeat until you read line “end 0 0”
6. Read and process the second part of the input file
a. read location name to search into a string
b. find all occurrences of this string in the vector
c. for all found locations, print the location name and its coordinates to the output file and to the console (cout)
d. repeat until you reach end of the input file


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

using namespace std;

namespace {

	struct Dining
	{
		string diningName;
		double x;
		double y;

		Dining();

		Dining(const string& diningName, double x, double y);
	};

	void readData  (istream& is, Dining& dining) {
		is>>dining.diningName;
		is>>dining.x;
		is>>dining.y;
	}
	void printData (ostream& os, const Dining& dining) {
		os<<dining.diningName;
		os<<dining.x;
		os<<dining.y;
	}
}

int main()
{
	ifstream fin;
	ofstream fout;
	string fileNameI,fileNameO;
	const string endOfData="end";

	cout<<"enter input and output fileNames: ";
	cin>>fileNameI>>fileNameO;
	fin.open(fileNameI.c_str());
	fout.open(fileNameO.c_str());

	Dining d;
	vector<Dining> v;
	while ( fin.good() ) {
		readData(fin, d);
		v.push_back(d);
	}

	string strSpecificDining;
	while ( fin >> strSpecificDining )
	{
		// TODO process the second part
		// TODO find the strSpecificDining locations and print them
	}
	fin.close();
	fout.close();
	}
Last edited on
I can't see that you implemented 5b, so before worrying about searching to should do this;
How does the input file look like ?
You also need to implement your constructors.

Unless your using the outdated C++98 standard you don't need to use the c_str() member function when opening the streams. C++11 and higher allows using a std::string when opening the C++ streams using either the constructor or the stream.open() function.

You should also start using meaningful variable names and using the actual read operation to control the loop instead of good(), bad() or eof().

Lastly, for now, you should start creating variables closer to first use instead of in one big glob at the beginning of scopes.


Topic archived. No new replies allowed.