I am trying to write a code using vector of classes


Keywords:c++ 


Question: 

What I have to do is use vectors of classes, instead of parallel vecotrs, to store all of the data in the "receipt" section of the code.

What I did first was use parallel vectors in order to do this (and that is what is written in the code), but now I need to use vectors of classes and I am not sure how to do it. What is the proper way to do this?

#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
#include <vector>
#include <fstream>
#include <cstdlib> 
using namespace std;


vector <string> flavor;
vector <string> sizes;
vector <double> price;




void printWelcomeMessage() {
    cout << "Welcome to My Frozen Yogurt!" << endl;
    cout << endl;
    cout<< endl;

}


char toLowerCase (char c) {
    char c_lower = c;
    if ( (c >= 'A') && (c <= 'Z')) c_lower = c + 32;
    return c_lower;

}



void toLowerCase (string& str) {
    for (int k=0; k< str.size(); k++) {
       str[k] = toLowerCase (str[k]);

    }
    return;

}





double getYogurtSize(string& yogurtSize) {

    double subtotal=0.;

    const double TAX= 0.0875, SMALLSIZE= 2.19, MEDIUMSIZE = 3.49, LARGESIZE = 4.49;



    cout << "What size would you like? Please enter small, medium, or large: ";
    getline(cin,yogurtSize);
    toLowerCase(yogurtSize);

    //push_back to enter content into vector
    sizes.push_back(yogurtSize);

    //pop_back to delete any times the user mispells the size
    if (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large")
        sizes.pop_back();


    while (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large") {
            cout << "What size would you like? Please enter small, medium, or large: ";
            getline (cin, yogurtSize);
            sizes.push_back(yogurtSize);
        }


    if (yogurtSize== "small") {
        subtotal += SMALLSIZE;
        price.push_back(SMALLSIZE); }
    else if (yogurtSize == "medium") {
        subtotal += MEDIUMSIZE;
        price.push_back(MEDIUMSIZE); }
    else if (yogurtSize=="large") {
        subtotal+= LARGESIZE;
        price.push_back(LARGESIZE); }

    return subtotal;
}

void getYogurtFlavors(string& flavor1, string& flavor2, string& flavor3) {
    cout << "Enter flavor 1: ";
    getline (cin, flavor1);
    toLowerCase (flavor1);

    //use push_back to into content into vector
    flavor.push_back(flavor1);
    flavor.push_back("-");



    cout << "Enter flavor 2: ";
    getline (cin, flavor2);
    toLowerCase(flavor2);
    flavor.push_back(flavor2);
    flavor.push_back("-");



    cout << "Enter flavor 3: ";
    getline (cin, flavor3);
    toLowerCase(flavor3);
    flavor.push_back(flavor3);
    flavor.push_back("-");

}



// function printOrder prints out the flavors of each input

void printOrder (string yogurtSize, string flavor1, string flavor2, string flavor3, int){

    int orderNumber= 0;
    orderNumber++;
    cout<< endl;
    cout << "************************" << endl;
    cout  << "Order: " << orderNumber << flavor1.substr(0,4) << "-" << flavor2.substr(0,4) << "-"<< flavor3.substr(0,4) << " " << yogurtSize  << endl;
    cout << "************************" << endl;
    cout << endl;


    return; 

}



bool addAnotherOrderQ(){
    string order;
    string yes = "yes";
    string no = "no";
    bool moreorders= true;
    cout << "Would you like another order? ";
    getline (cin, order );

    while (order != yes && order != no) {
        cout << "Would you like another order? Please enter yes or no: ";
        getline (cin, order);
        cout << endl;
    }

    if (order == no){
        moreorders= false;
        cout << endl;

    }
    return moreorders;

}



void printTotalCosts (double& subtotal, int& orderNumber) {

    const double TAX= 0.0875;
    int setpre= 2;
    int totalwidth=15;
    int taxwidth= 9;
    int subwidth =12;
    int itemwidth= 6;
    cout << "===================== Receipt ===========================" << endl;
    cout << endl;

    // for for loops to print the size, flavors, and price in the receipt screen, and in the file
    for (int i=0; i< flavor.size(); i++) {
        cout  <<  flavor[i].substr(0,4)<< " ";
    }
    for (int i=0; i< sizes.size(); i++) {
        cout <<  sizes[i]<< " " ; 

    }

    for (int i=0; i < price.size(); i++) {
        cout << price[i]  << endl;

    }



    cout << "Number of items: " << setw(itemwidth) << orderNumber << endl;
    cout << "Subtotal: "  << "$"  << fixed << setprecision (setpre) << setw (subwidth) << subtotal << endl;
    cout << "Tax (8.75%): " << "$"<< setw(taxwidth) << TAX * subtotal <<  endl;

    cout << "Total: " <<  "$" << setw(totalwidth) << (TAX *subtotal) + subtotal << endl;
    cout << endl;
    cout << "=========================================================" <<endl;     
    cout << endl;

}



class YogurtOrder {
public:
    YogurtOrder (); //default constructor
    YogurtOrder (string, string, string, string, double); //constructor with parameters: size, flavor1, flavor2, flavor3, and price
    string getSize ();
    string getMixedFlavor ();
    double getPrice ();

private:
    string size;
    string flavor1, flavor2, flavor3;
    double price;
};



YogurtOrder:: YogurtOrder() {}


YogurtOrder:: YogurtOrder (string s, string first, string second, string third, double p){
    s= size;
    first= flavor1;
    second= flavor2;
    third= flavor3;
    p= price;
}

string YogurtOrder:: getSize () {

    return size;
}


string YogurtOrder:: getMixedFlavor() { 



    return flavor1, flavor2, flavor3; 
}

double YogurtOrder:: getPrice() {


    return price; 
}



int main () {

    const double TAX= 0.0875;
    int setpre= 2;
    int totalwidth=15;
    int taxwidth= 9;
    int subwidth =12;
    int itemwidth= 6;

    //Print the welcome message
    printWelcomeMessage();

    // initialize the loop variables
    bool more_order = true;
    int orderNumber= 0;


    // varible for cost
    double subtotal= 0.;


    //variable for suze and flavors default intitialized to ""
    string yogurtSize, flavor1, flavor2, flavor3, order;

    //continye to get order until the user is done
    while (more_order) {

        //increment order number
        orderNumber++;

        //update the size and subtotal
        subtotal= subtotal +getYogurtSize(yogurtSize);

        //update the flavors
        getYogurtFlavors(flavor1, flavor2, flavor3);

        // print the current order
        printOrder (yogurtSize, flavor1, flavor2, flavor3, orderNumber);

        //determine whther or not to order more
        more_order= addAnotherOrderQ();

    }







        // Print out the subtotal, tax, and total

    vector <YogurtOrder> flavors; 
    printTotalCosts(subtotal, orderNumber);



    return 0;

}

1 Answer: 

What you need to is make a class of necessary data members as follow:

'

class  FrozenYogurt
{
   private:
       string flavor;
       string size;
       double price;
   public:
       //constructor
       public FrozenYogurt(string f,string s,double p)
       {
             this.flavor=f;
             this.size=s;
             this.price=p;
       }
       //Getters
       public string getFlavor(){return this.flavor;}
       public string getSize(){return this.size;}
       public double getPrice{return this.price;}
       ....//All other necessary functions
}

Then make vector of this class like this :

vector<FrozenYogurt> listOfItems;

You can make objects of FrozenYogurt and insert into this vector list like this

string flavor="somthing";
string size="somthing";
double price=123;
FrozenYogurt item(flavor,size,price);
listOfItems.push_back(item);

Now you can access items like this:

listOfItems[index].getSize();
listOfItems[index].getFlavor();
listOfItems[index].getPrice();