-1

Hey Everyone so I have the below code. Its a simple project and im just trying to practice classes in C++ and what not. The program semi works, but the only issue I have is that the printOrder() should be able to loop through the vector of Flavors and print out the order using the substring of the first 4 letters in the flavor.

Now the problem is that the vector is empty so when I print the order.. there's no orders.. and I know why, its because the vector that im passing in does not reference the private vector in the class. I don't know how to reference the vector in the class. Can anyone help? I'm pretty new to this so please be as detailed as possible, possibly with code examples? I might have butchered the structure of this class, but practice makers perfect.

Thanks everyone in advance.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>


using namespace std;


    int number_small = 0;
    int number_medium = 0;
    int number_large = 0;
    int counter = 1;
    int vecCount = 1;


    class Order
{



public:
    //Default
    Order();
    //Paramerterized
    Order(string s, string flav,vector <string>& f)
    { 

    size = s;
    flavors = flav;
    x = f;
}

    //Functions

    void getYogurtSize(string s)
    {   LOOP:
            cout << "Please enter small, medium, or large: ";
            cin >> size;
            //If Statement
            if (size == "small")
            {   number_small++;}
            else if (size == "medium")
            {   number_medium++;}
            else if (size == "large")
            {   number_large++;}
            else    {   cout << "enter the correct input!\n\n"; 
            goto LOOP;} }


     void getYogurtFlavors(string flavor,vector<string> f)
    {
        vecCount = 1;
        do
        {

            cout << "\nEnter Flavor " << vecCount << ":";
            cin >> flavor;
            if (flavor == "DONE")
            {
                break;
            }

            f.push_back(flavor);  //Moved after the check to not include DONE
            vecCount++;
        } while ((flavor != "DONE") && (vecCount <= 10));
    }



void printOrder(vector<string> flavors)
{
    cout << "Order " << counter << ": ";
    for (auto i : flavors){
        cout << i.substr(0, 4) << "-";
    }
    cout << "**";
}



private:
   //Private Variables
   string size;
   string flavors;  
   vector <string> x;

};


int _tmain(int argc, _TCHAR* argv[])
{

//Variables
Order ord;
string sz;
string flavor;
string input;
vector <string> f;
const double TAX_RATE = 0.0875;
double subtotal;
double tax;
double total;
const double PRICE_SMALL = 2.19;
const double PRICE_MEDIUM = 3.49;
const double PRICE_LARGE = 4.49;

do
{


    ord.getYogurtSize(sz);
    ord.getYogurtFlavors(flavor, f);
    ord.printOrder(f);


    cout << "\n\nWould you like to add another order? ";
    counter++;
    cin >> input;
    f.clear();

} 


while (input == "yes");
{
    subtotal = (number_small*PRICE_SMALL) + (number_medium*PRICE_MEDIUM) + (number_large*PRICE_LARGE);
    tax = (subtotal*TAX_RATE);
    total = tax + subtotal;
    cout << "Subtotal: \t$" << fixed << setprecision(2) << subtotal << endl;
    cout << "Tax (8.75%):    $" << fixed << setprecision(2) << tax << endl;
    cout << "Total:   \t$" << fixed << setprecision(2) << total << endl << endl;
}


return 0;
}

//Default Constructor
Order::Order(){

size = "";
flavors = "";
}
salce
  • 409
  • 1
  • 12
  • 28
  • Did you consider compiling with debugging info and warnings (e.g. with `g++ -Wall -g` if using [GCC](http://gcc.gnu.org/) ...) then **use the debugger** .... – Basile Starynkevitch Aug 06 '14 at 04:22
  • The essence of the problem is, there are TWO vectors of f, one inside the class, and one in main. When you call `getYorgurtFlavors(flavor, f)`, you are passing in a **copy** of `f`. Read about pass-by-value vs pass-by-reference. – NicholasM Aug 06 '14 at 04:26
  • Thank you both for the information! I will definitely read up on that. I learned it before, but now I understand it a bit better. thank you – salce Aug 06 '14 at 04:48

1 Answers1

1

You need to pass f by reference. Your code is passing a copy of f to getYogourtFlavors and printOrder. This means that when you modify f in getYogourtFlavors, the change is not reflected in the calling function.

Your methods should look more like this (note the addition of the & sign):

void getYogurtFlavors(string flavor,vector<string>& f)
...
void printOrder(vector<string>& flavors)
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
  • Awesome! worked like a charm. Can you recommend any good reads for C++ about classes? I would love to learn more about them. – salce Aug 06 '14 at 04:48
  • 1
    Pick your choice(s) from one of these -> http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list . On a different note, however, I don't know how you're learning C++ but I have a bad feeling it's not a good source(LOOP branch, example). In C, the use of GOTOs is not forbidden neither is it in C++ but it's highly NOT recommended. My advice? Please make use of a good book. – iamOgunyinka Aug 06 '14 at 05:07