| | |
passing arrays / updating object arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
My prof is touching on information hiding, and good programming techniques. He said it's a good idea to keep as much of the user interface (i.e., couts) in main, and then modify object variables through the use of functions.
Here's what I want to do:
I'm starting on a program that lets the user input the type of pizza (hand tossed, pan, deep dish), the size (s, m, l) and the toppings they want.
So far, here's what I've setup in my class:
For the toppings, I figured the easiest way is to do a loop of some sort until they're finished inputting toppings (maximum of 7). The reason I'm using an array is because I have to include a function that outputs the object data, including each topping.
My question is this: How can I allow the user to input the toppings they want in main(), and then put that data into the array in my object without creating a temp array for user input?
Here's what I want to do:
I'm starting on a program that lets the user input the type of pizza (hand tossed, pan, deep dish), the size (s, m, l) and the toppings they want.
So far, here's what I've setup in my class:
C++ Syntax (Toggle Plain Text)
#pragma once #include <iostream> using namespace std ; const double SMALL = 10.00 const double MEDIUM = 14.00 const double LARGE = 17.00 const double TOPPING = 2.00 class Pizza { public: void set ( ) ; void outputDesc ( ) ; double computePrice ( ) ; private: char type ; char size ; int toppings[7] ; } ;
My question is this: How can I allow the user to input the toppings they want in main(), and then put that data into the array in my object without creating a temp array for user input?
Last edited by Duki; Sep 4th, 2007 at 9:26 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
•
•
My prof is touching on information hiding, and good programming techniques.
Having the
using namespace std basically defeats the purpose of a namespace. And putting it in the header is known to cause problems down the line. And the
#pragma once thing is platform-specific. Include guards are what I would prefer.You could pass parameters to your member functions.
Last edited by Dave Sinkula; Sep 4th, 2007 at 9:47 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
[QUOTE=Duki;428577]
For the toppings, I figured the easiest way is to do a loop of some sort until they're finished inputting toppings (maximum of 7). The reason I'm using an array is because I have to include a function that outputs the object data, including each topping.
Make a member function void add_topping(int topping_number), that adds a topping. Or make an extra array. It's not like it's a big deal, to make an extra array.
And what is 'outputDesc' for? You're having a pizza class that can output its information? That violates the principle you've described. (And what is 'set', too?)
For the toppings, I figured the easiest way is to do a loop of some sort until they're finished inputting toppings (maximum of 7). The reason I'm using an array is because I have to include a function that outputs the object data, including each topping.
•
•
•
•
My question is this: How can I allow the user to input the toppings they want in main(), and then put that data into the array in my object without creating a temp array for user input?
And what is 'outputDesc' for? You're having a pizza class that can output its information? That violates the principle you've described. (And what is 'set', too?)
All my posts may be redistributed under the GNU Free Documentation License.
•
•
•
•
Good. Then don't do this:
Having theusing namespace stdbasically defeats the purpose of a namespace. And putting it in the header is known to cause problems down the line.
I don't understand how to use the Include gaurds; could you explain them?
•
•
•
•
••••For the toppings, I figured the easiest way is to do a loop of some sort until they're finished inputting toppings (maximum of 7). The reason I'm using an array is because I have to include a function that outputs the object data, including each topping.
Make a member function void add_topping(int topping_number), that adds a topping. Or make an extra array. It's not like it's a big deal, to make an extra array.
•
•
•
•
And what is 'outputDesc' for? You're having a pizza class that can output its information? That violates the principle you've described. (And what is 'set', too?)
Set is the function that sets the values for type/size/toppings
Last edited by Duki; Sep 4th, 2007 at 10:08 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
Or
Follow that link I posted. 
Early on it's more of an exercise to go through.
using std::cout;•
•
•
•
I don't understand how to use the Include gaurds; could you explain them?

•
•
•
•
alright, just wanted to make sure there wasn't an easier way. thanks
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "pizza.h" int main() { pizza::topping toppings[7] = { pizza::sausage, pizza::mushrooms, pizza::none, pizza::none, pizza::none, pizza::none, pizza::none, }; pizza mypie(pizza::deep_dish, pizza::medium, toppings); std::cout << mypie; return 0; } /* my output Medium Deep Dish Sausage Mushrooms */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
#ifndef PIZZA_H #define PIZZA_H // ... #endif
#ifndef PIZZA_H #define PIZZA_H #include <ostream> class pizza { public: enum type { hand_tossed, pan, deep_dish }; enum size { small, medium, large }; enum topping { none, pepperoni, sausage, mushrooms, green_peppers, canadian_bacon, onions }; private: type m_type; size m_size; topping m_topping[7]; public: pizza(type type_ = hand_tossed, size size_ = large, topping toppings_[7]) : m_type(type_), m_size(size_) { for ( int i = 0; i < sizeof m_topping / sizeof *m_topping; ++i ) { m_topping[i] = toppings_[i]; } } friend std::ostream& operator<< (std::ostream& o, const pizza &p); }; #endif
#define that you make up.[edit]The first time you
#include "pizza.h", your macro (PIZZA_H in this example) is not #defined, so it gets #defined and the contents #included. Thereafter, subsequent attempts to #include this header will already have the macro (PIZZA_H in this example) #defined, and the contents will be skipped. Last edited by Dave Sinkula; Sep 4th, 2007 at 11:00 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Oh ok, I still don't quite get it but it's making more sense now.
is there something wrong with this part of my code? I get an error saying arrays of refrences are illegal
is there something wrong with this part of my code? I get an error saying arrays of refrences are illegal
C++ Syntax (Toggle Plain Text)
#include "prob7.h" void Pizza::set ( char x , char y , int & a[7] ) { type = x ; size = y ; toppings = a ; } void Pizza::ouputDesc ( int i ) { cout << endl << "Style: " << style << endl ; cout << "Size: " << size << endl ; cout << "Toppings: " << flush ; for ( int x = 0 ; x < i ; x++ ) cout << toppings [ i ] << ", " << flush ; }
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
great, but now i get an overloaded function error on the same function.
prototype: void set ( char , char , int ) ;
prototype: void set ( char , char , int ) ;
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
![]() |
Similar Threads
- Passing arrays between subs (VB.NET)
- Help: Passing arrays between functions (C)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Passing Arrays of Objects to Member Functions (C++)
- passing arrays to classes? (Java)
- Arrays (C++)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Declaring a list in a header file
- Next Thread: Dynamic array
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count database delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






