Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help reading this file into my program. CornerBakery 100.3 205.6 PaneraBrea

ID: 3742802 • Letter: N

Question

Need help reading this file into my program.

CornerBakery 100.3 205.6
PaneraBread 77.7 888.8
StarBucks 9.87 6.54
PaneraBread 1.7 2.5
end 0 0
PaneraBread
McDonalds
StarBucks

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is the code i have so far and what needs to be done to work successfully.

read data to an instance of Dining and push it to the vector

repeat until you read line “end 0 0”

Read and process the second part of the input file, read location name to search into a string , find all occurrences of this string in the vector

for all found locations, print the location name and its coordinates to the output file and to the console

repeat until you reach end of the input file.

---------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

namespace {

struct Dining

{

string name;

double x,y;

Dining()

{

x = 0;

y = 0;

name = " ";

}

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

{

name = diningName;

this->x = x;

this->y = y;

}

};

void readData (istream& is, Dining& dining)

{

is >> dining.name >> dining.x >> dining.y;

}

void printData (ostream& os, const Dining& dining)

{

os << dining.name << endl;

os << dining.x << dining.y << endl;

}

}

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.eof())

{

readData(fin, d);

v.push_back(d);

}

string strSpecificDining;

while ( fin >> strSpecificDining )

{

int count = -1;

for(int i = 0; i < v.size(); ++i)

cout << strSpecificDining;

}

for(int i = 0; i < v.size(); ++i)

printData(fout,v[i]);

fin.close();

fout.close();

}

Explanation / Answer

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

// A Structure to Hold on Dining Instances
struct Dining {
string loc_name;
double x,y;
};

int main()
{
// Reading File Input Here
ifstream infile("read.txt");
string name,line;
double x, y;
vector<Dining> dinings; // For Dining Objects
vector<string> searchLoc; // We will store all search locations here
// Loop to process the input from File
while (getline(infile, line))
{
// Reading Line by Line input from file
istringstream iss(line);
iss >> name >> x >> y;
if( name!="end" &&(x>0 && y > 0)){
Dining d;
d.loc_name = name;
d.x = x;
d.y = y;
dinings.push_back(d);
} else {
if(name=="end" )
continue;
searchLoc.push_back(name);
}
}
// Closing the input file stream
  
infile.close();
  
// To Open an Output file
ofstream outputFile("output.txt");

//Processing the Search Data
for(int i = 0; i<searchLoc.size(); i++){
// cout<<searchLoc[i]<<endl;
for(int j =0; j< dinings.size();j++){
Dining presentDine = dinings[j];
if(presentDine.loc_name == searchLoc[i]){
ostringstream output;
output << presentDine.loc_name;
output << " ";
output << presentDine.x;
output << " ";
output << presentDine.y;
output << " ";
outputFile << output.str();
cout<<output.str();
}
}
}
return 0;
}

Let me know if you are not able to understand anything.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote