passing arrays / updating object arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

passing arrays / updating object arrays

 
0
  #1
Sep 4th, 2007
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:

  1. #pragma once
  2.  
  3. #include <iostream>
  4. using namespace std ;
  5.  
  6. const double SMALL = 10.00
  7. const double MEDIUM = 14.00
  8. const double LARGE = 17.00
  9. const double TOPPING = 2.00
  10.  
  11. class Pizza
  12. {
  13. public:
  14. void set ( ) ;
  15. void outputDesc ( ) ;
  16. double computePrice ( ) ;
  17. private:
  18. char type ;
  19. char size ;
  20. int toppings[7] ;
  21. } ;
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?
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #2
Sep 4th, 2007
Originally Posted by Duki View Post
My prof is touching on information hiding, and good programming techniques.
Good. Then don't do this:
Originally Posted by Duki View Post
  1. #pragma once
  2.  
  3. #include <iostream>
  4. using namespace std ;
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.

Originally Posted by Duki View Post
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?
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: passing arrays / updating object arrays

 
0
  #3
Sep 4th, 2007
[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.
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?
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?)
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #4
Sep 4th, 2007
Originally Posted by Dave Sinkula View Post
Good. Then don't do this:
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.
so do like std::cout << ?
And the #pragma once thing is platform-specific. Include guards are what I would prefer.
I don't understand how to use the Include gaurds; could you explain them?


Originally Posted by Rashakil Fol View Post
Originally Posted by Duki View Post
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.
alright, just wanted to make sure there wasn't an easier way. thanks

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?)
I'm not sure I understand; why shouldn't I do outputDesc? I need some way of diplaying a list of information that has been input.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #5
Sep 4th, 2007
Originally Posted by Duki View Post
so do like std::cout << ?
Or using std::cout;

Originally Posted by Duki View Post
I don't understand how to use the Include gaurds; could you explain them?
Follow that link I posted.

Originally Posted by Duki View Post
alright, just wanted to make sure there wasn't an easier way. thanks
Early on it's more of an exercise to go through.
  1. #include <iostream>
  2. #include "pizza.h"
  3.  
  4. int main()
  5. {
  6. pizza::topping toppings[7] =
  7. {
  8. pizza::sausage, pizza::mushrooms,
  9. pizza::none, pizza::none, pizza::none, pizza::none, pizza::none,
  10. };
  11. pizza mypie(pizza::deep_dish, pizza::medium, toppings);
  12. std::cout << mypie;
  13. return 0;
  14. }
  15.  
  16. /* my output
  17. Medium Deep Dish
  18.  Sausage
  19.  Mushrooms
  20. */
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #6
Sep 4th, 2007
I did follow the link, still didn't make sense
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #7
Sep 4th, 2007
#ifndef PIZZA_H
#define PIZZA_H

// ...

#endif
As in...
#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
Each header needs it's own unique #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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #8
Sep 4th, 2007
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

  1. #include "prob7.h"
  2.  
  3. void Pizza::set ( char x , char y , int & a[7] )
  4. {
  5. type = x ;
  6. size = y ;
  7. toppings = a ;
  8. }
  9.  
  10. void Pizza::ouputDesc ( int i )
  11. {
  12. cout << endl << "Style: " << style << endl ;
  13. cout << "Size: " << size << endl ;
  14. cout << "Toppings: " << flush ;
  15.  
  16. for ( int x = 0 ; x < i ; x++ )
  17. cout << toppings [ i ] << ", " << flush ;
  18. }
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing arrays / updating object arrays

 
0
  #9
Sep 4th, 2007
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: passing arrays / updating object arrays

 
0
  #10
Sep 4th, 2007
great, but now i get an overloaded function error on the same function.

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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC