In: Computer Science
I need to access the values in the pizzaLocations array when main.cpp is run. The values include name, address, city, postalCode, province, latitude, longitude, priceRangeMax, priceRangeMin. I tried declaring getter functions, but that does not work.
How do I access the values? Do I need to declare a function to return the values?
#pragma once
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using std::getline;
using std::ifstream;
using std::string;
using std::stringstream;
struct Location {
string name, address, city, postalCode,
province;
double latitude, longitude;
int priceRangeMin, priceRangeMax;
string getName(){ return name};
string getAddress(){ return address};
string getCity(){ return city};
string getPostalCode(){ return postalCode};
string getProvince(){ return province};
double getLatitude(){ return latitude};
double getLongitude(){ return longitude};
int getPriceRangeMin(){ return priceRangeMin};
int getPriceRangeMax(){ return
priceRangeMax};
};
class PizzaZine {
private:
Location*
pizzaLocations;
size_t size;
public:
//default
constructor
PizzaZine()
{
pizzaLocations = new Location[50];
this->size = 50;
}
//dynamically allocate array with user specified size
PizzaZine(size_t size) {
pizzaLocations = new Location[size];
this->size = size;
}
//destruct constructor
~PizzaZine()
{
delete[] pizzaLocations;
}
Location &operator[](size_t);//return the desired size for array
// This function is
implemented for you
void readInFile(const string
&);
};
Location &PizzaZine::operator[](size_t index)
{
return pizzaLocations[index];
}
//this function has been implemented for you
void PizzaZine::readInFile(const string &filename)
{
ifstream inFile(filename);
Location newLoc; //newLoc locally stores
values of pizzaLocations?
string line;
string cell;
// Read each line
for (int i = 0; i < size; ++i)
{
// Break each line up
into 'cells'
getline(inFile,
line);
stringstream
lineStream(line);
while (getline(lineStream,
newLoc.name, ',')) {
getline(lineStream, newLoc.address,
',');
getline(lineStream, newLoc.city,
',');
getline(lineStream, cell, ',');
if
(!cell.empty()) {
newLoc.postalCode = stoul(cell);
}
getline(lineStream, newLoc.province,
',');
getline(lineStream, cell, ',');
newLoc.latitude = stod(cell);
getline(lineStream, cell, ',');
newLoc.longitude = stod(cell);
newLoc.priceRangeMin = -1;
getline(lineStream, cell,
',');
if
(!cell.empty()) {
newLoc.priceRangeMin =
stoul(cell);
}
newLoc.priceRangeMax = -1;
getline(lineStream, cell, ',');
if
(!cell.empty() && cell != "\r") {
newLoc.priceRangeMax =
stoul(cell);
}
pizzaLocations[i] = newLoc; //how do I retrieve
the values in pizzaLocations once main.cpp is
called?
}//end of while
loop
}//end of for loop
}//end of readInFile function
You can get the Location Values in the Array as follows:
Location &PizzaZine::operator[](size_t index)
{
return pizzaLocations[index];
}
This will return the Particular location at the index.
In the main function you can write the code as follows:
PizzaZine aPizzaZine;
aPizzaZine[3].name //Return the name of the location stored in the list at the 3rd index.
aPizzaZine[3].address //Return the address of the location stored in the list at the 3rd index.
aPizzaZine[3].city ////Return the city of the location stored in the list at the 3rd index.
aPizzaZine[3].postalCode //Return the postal code of the location stored in the list at the 3rd index.
aPizzaZine[3].province //Return the province of the location stored in the list at the 3rd index.
Only dot operator is required to access each attribute of the location as all the fields in the struct is by default public.
and there is not need to define the getters and setters for the same.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using std::getline;
using std::ifstream;
using std::string;
using std::stringstream;
struct Location {
string name, address, city, postalCode,
province;
double latitude, longitude;
int priceRangeMin, priceRangeMax;
};
class PizzaZine {
private:
Location* pizzaLocations;
size_t size;
public:
//default constructor
PizzaZine()
{
pizzaLocations = new Location[50];
this->size = 50;
}
//dynamically allocate array with user specified size
PizzaZine(size_t size) {
pizzaLocations = new
Location[size];
this->size = size;
}
//destruct constructor
~PizzaZine()
{
delete[] pizzaLocations;
}
Location &operator[](size_t);//return the desired size for array
// This function is implemented for you
void readInFile(const string &);
};
Location &PizzaZine::operator[](size_t index)
{
return pizzaLocations[index];
}
//this function has been implemented for you
void PizzaZine::readInFile(const string &filename) {
ifstream inFile(filename);
Location newLoc; //newLoc locally stores values of
pizzaLocations?
string line;
string cell;
// Read each line
for (int i = 0; i < size; ++i) {
// Break each line up into
'cells'
getline(inFile, line);
stringstream lineStream(line);
while (getline(lineStream,
newLoc.name, ',')) {
getline(lineStream, newLoc.address, ',');
getline(lineStream, newLoc.city, ',');
getline(lineStream, cell, ',');
if
(!cell.empty()) {
newLoc.postalCode = stoul(cell);
}
getline(lineStream, newLoc.province, ',');
getline(lineStream, cell, ',');
newLoc.latitude
= stod(cell);
getline(lineStream, cell, ',');
newLoc.longitude
= stod(cell);
newLoc.priceRangeMin = -1;
getline(lineStream, cell, ',');
if
(!cell.empty()) {
newLoc.priceRangeMin = stoul(cell);
}
newLoc.priceRangeMax = -1;
getline(lineStream, cell, ',');
if
(!cell.empty() && cell != "\r") {
newLoc.priceRangeMax = stoul(cell);
}
pizzaLocations[i] = newLoc; //how do I retrieve the values in
pizzaLocations once main.cpp is called?
}//end of while loop
}//end of for loop
}//end of readInFile function
int main(int argc, char const *argv[])
{
// test the first 10 items in the list
PizzaZine top10(10);
top10.readInFile("data.csv");
std::cout << top10[2].address<<" ";
std::cout << top10[2].city<<" ";
std::cout << top10[2].name<<" ";
std::cout << top10[2].latitude<<" ";
return 0;
}
OUTPUT:
File(data.csv)