Question

In: Computer Science

I need to access the values in the pizzaLocations array when main.cpp is run. The values...

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?

  • operator[](size_t) - This should return the location with the matching index. For example if given an index of 3, you should return the location at index 3 in the list.

#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

Solutions

Expert Solution

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)


Related Solutions

In C++, I have 3 files (Main.cpp, Point.cpp, Point.h). When Compiled and run, it produces 2...
In C++, I have 3 files (Main.cpp, Point.cpp, Point.h). When Compiled and run, it produces 2 errors, in Main.cpp "cannot convert from double to point" and in Point.cpp "name followed by :: must be a class or namespace name". How do you go about fixing these errors without editing Main.cpp? Thanks in advance. //main.cpp #include <iostream> #include <cmath> #include "Point.h" using namespace std; const double PI = 3.14159265359; const double TOL = .0000001; //allows us to do a comparison for...
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
How do you run an ANOVA test when some of the values are non-numeric? I have...
How do you run an ANOVA test when some of the values are non-numeric? I have a large excel database, over 100,000 cells worth of data. For the assignment, I need to see if there is a relation between the type of distribution channel (non-numeric) and the average amount of money spent (numeric). I HAVE to use ANOVA to solve this but I am not sure how. There are 11 different distribution channels and 100,000 different values for the amount...
I need to be able to store 5000 names into an array. I currently have a...
I need to be able to store 5000 names into an array. I currently have a code that generates one random name. I just need to generate an array of 5000 the same way. #include <cstdlib> #include <ctime> #include <iostream> #include <iomanip> using namespace std; string RandomFirstGen(int namelength); int main(){ srand(time(NULL)); int namelength = rand()%(16-8+1)+8; cout<<"name is: "<<RandomFirstGen(namelength)<<endl; return 0; } string RandomFirstGen(int namelength){ for (int i=0; i<=5000 ; i++){ const int MAX = 26; char alphabet[MAX] = { 'a',...
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
in Java, I need to create an array of animals. The user is going to be...
in Java, I need to create an array of animals. The user is going to be prompted to enter the size of the array where a valid size is 10-30(inclusive). Then I have to populate only half of this array. I am not sure how to do that so can you please write comments explaining. Thank you
i need a survey in organization about personality and values .
i need a survey in organization about personality and values .
in this code I have used array two times . I need a way to make...
in this code I have used array two times . I need a way to make the program functional using a string to store the results and print it again later . this game should be array free. import java.util.Scanner; import java.util.Random;//starter code provided public class inputLap { public static char roller; public static String playerName; public static int printed=0; public static int rounds=8,lives=0,randy; public static int tries[]=new int[4];//use arrays to store number of tries in each life public static...
-please answer this written out. Do not use excel I cannot access it. i need to...
-please answer this written out. Do not use excel I cannot access it. i need to know how to do it on paper- Deluxe River Cruises operates a fleet of river vessels. The fleet has two types of vessels: A type A vessel has 60 deluxe cabins and 160 standard cabins, whereas a type B vessel has 80 deluxe cabins and 120 standard cabins. Under a charter agreement with the Odyssey Travel Agency, Deluxe River Cruise sis to provide Odyssey...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT