Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,098 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,639 people online right now. Registration is fast and FREE... Join Now!




Using Vectors

 
Reply to this topicStart new topic

Using Vectors, Absolute C++, 3d Ed, Savitch

loufrank
15 Oct, 2007 - 10:36 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 2


My Contributions
I am having a problem understanding how to set up a vector in the way the book is asking.

I understand vectors are similar to arrays but are flexible.

CODE

//Chapter 6 - Programming Project #7
//Program designed to track type of pizza
//along with the size and number of toppings.
//Written by Louis McAlister
//COSC 231 - OOP
//Professor Doc Craddock
#pragma once

#include <iostream>
#include <string>
#include <iomanip>
#include<cstdlib>
#include<vector>

using namespace std;

class Pizza
{
private:
    int pie_type;
    int pie_size;
    int pie_topz;

public:
    void Output_Description();    //Displays pizza type, size, toppings
    void Compute_Price();        //Displays cost of pizza
    
    void input_Ptype();            //User input of pizza type
    void input_Psize();            //User input of pizza size
    void input_NumTops();        //User input of number of toppings

};

class Order
{
private:
    vector <Pizza> orders;

public:
    void add_pizz\a(Pizza);
};

[MAIN]
#include "Pizza.h"

const string Pizza_Type [3] = {"Deep Dish", "Hand Tossed", "Pan"};
const string Pizza_Size [3] = {"Small", "Medium", "Large"};
const string Pizza_Topz [3] = {"Pepperoni", "Cheese", "Pepperoni and Cheese"};
const int Pizza_Cost [3] = {10, 12, 17};

int main()
{
    Pizza pie;
    
    pie.input_Ptype();
    pie.input_Psize();
    pie.input_NumTops();
    pie.Output_Description();
//    test_disp();
}

void Pizza::input_Ptype(){
    cout << "Type of Pizza\n";
    cout << "1 - " << Pizza_Type[0] << "\n";
    cout << "2 - " << Pizza_Type[1] << "\n";
    cout << "3 - " << Pizza_Type[2] << "\n";
    cin >> pie_type;

    if (pie_type > 3 || pie_type == 0)
    {
        cout << "Invalid selection for pizza type.\n"
             << "Selection must be 1, 2, or 3\n\n"
             << "Program will now terminate."
             << endl;
        exit(1);
    }
}

void Pizza::input_Psize(){
    cout << "Pizza Size\n";
    cout << "1 - " << Pizza_Size[0] << "\n";
    cout << "2 - " << Pizza_Size[1] << "\n";
    cout << "3 - " << Pizza_Size[2] << "\n";
    cin >> pie_size;

    if (pie_size > 3 || pie_size == 0)    
    {    cout << "Invalid selection for pizza size.\n"
             << "Selection must be 1, 2, or 3\n\n"
             << "Program will terminate."
             << endl;
        exit (1);
    }
}

void Pizza::input_NumTops(){
    cout << "\nPizza Toppings\n"
         << "Number of toppings for this pizza.\n"
         << "Each topping is $2.00: ";
    
    cout << "\n1 - " << Pizza_Topz[0] << "\n";
    cout << "2 - " << Pizza_Topz[1] << "\n";
    cout << "3 - " << Pizza_Topz[2] << "\n";
    cin >> pie_topz;

    if (pie_topz > 3 || pie_topz == 0)    
    {    cout << "Invalid selection for pizza toppins.\n"
             << "Selection must be 1, 2, or 3\n\n"
             << "Program will terminate."
             << endl;
        exit (1);
    }
}
void Pizza::Output_Description()
{
    //Order lfm;
    //lfm.orders.push_back(pt,ps, topz1);
    //lfm.orders.push_back("j",1);
    cout << "\nThe output for Pizza Type: "
         << pie_type
         << "\nThe output for Pizza Size: "
         << pie_size
         << "\nThe output for Pizza Toppings: "
         << pie_topz << endl;
    //cout << lfm.orders[0];
}

Problem from book

6. This Programming Project requires you to first complete Programming Project 7 from Chapter 5, which is an implementation of a Pizza class.

Add an Order class that contains a private vector of type Pizza.
This class represents a customer’s entire order, where the order may consist of multiple pizzas.

Include appropriate functions so that a user of the Order class can add pizzas to the order (type is deep dish, hand tossed, or pan; size is small, medium, or large; number of pepperoni or cheese toppings). You can use constants to represent the type and size.

Also write a function that outputs everything in the order along with the total price.
Write a suitable test program that adds multiple pizzas to an order(s).
This is what I am not understanding.

Thanks again for your help.
Louis
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Vectors
15 Oct, 2007 - 11:31 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Hi again, loufrank.

Please check the forum rules - this particular forum is more of a "hi, how you doin', I'm here lookin' for love" type of place. For specific programmin questions, please post in the forum associated with your language.

And post your code using the code tags - makes for way easier reading. Take a look at the gray text on the background of the post/reply text box for instructions.

This'll probably get bumped over to C/C++ by the mods shortly, so we'll see you there.

-jjh
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Using Vectors
15 Oct, 2007 - 06:46 PM
Post #3

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Welcome to </DIC> smile.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using Vectors
15 Oct, 2007 - 10:49 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Moved and modified title.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Vectors
15 Oct, 2007 - 11:06 PM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
There's a couple of tutorials on vectors in the C++ tutorials sections. You might want to look at them. They are the best thing that has ever been written, in any language, ever. tongue.gif

And they are similar to arrays - but in a lot of ways ther are better, because they can handle their own memory allocation/reallocation/deletion, and they have a bunch of convenient methods associated with them.

To actually use the vector in the order, you'll need to first create an Order object, before you create an Order object to put the Pizza object into:
CODE
int main()
{
    Order myOrder;
    Pizza pie;
    ...


Your Order class needs to have an add_pizza() method defined (and your current method declaration has an errant "\" in the method name). In this case, you would pass the completed Pizza object as the argument to add_pizza(), then use the std::vector method push_back() to add the Pizza to the vector in the Order:
CODE
void Order::add_pizza(Pizza) {
    orders.push_back(Pizza);
}

That gets your Pizza into the vector. You'll then have to write your function to output the order; I'll let you take a look at the tutorials to see some of the other vector methods that you may need, but at() will be an important one. But please post if you're having problems with it.

Hope that helps,

-jjh

This post has been edited by jjhaag: 15 Oct, 2007 - 11:07 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month