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

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);...
i need a survey in organization about personality and values .
i need a survey in organization about personality and values .
-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...
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
language is c++ I need to take a num and put it into an array one...
language is c++ I need to take a num and put it into an array one number at a time suck that the hundredths place is at 10^3, tens is 10^2 and so on For milestone 1 you need to build a constructor that converts a int to a bigInt You need to peel off each digit from the int and place it into the appropriate location in the array of int. To do this you need to use integer...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT