passing arrays / updating object arrays
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:
#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] ;
} ;
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?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
My prof is touching on information hiding, and good programming techniques.
Good. Then don't do this:
#pragma once
#include <iostream>
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.
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
[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?)
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
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?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. thanksAnd 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
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
so do like std::cout << ?
Or using std::cout; I don't understand how to use the Include gaurds; could you explain them?Follow that link I posted. ;)alright, just wanted to make sure there wasn't an easier way. thanksEarly on it's more of an exercise to go through.
#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
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I did follow the link, still didn't make sense
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
#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 #define d, so it gets #define d and the contents #include d. Thereafter, subsequent attempts to #include this header will already have the macro ( PIZZA_H in this example) #define d, and the contents will be skipped.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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
#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 ;
}
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
great, but now i get an overloaded function error on the same function.
prototype: void set ( char , char , int ) ;
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
Are you saying that the function's prototype does not match its definition? You need to have the corresponding change in both places.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
they match:
prototype:
void set ( char , char , int & , int ) ;
function:
void Pizza::set ( char x , char y , int (&a)[7] , int i )
{
type = x ;
size = y ;
for ( int x = 0 ; x < i ; x++ )
toppings[i] = a[i] ;
}
call:
pizza.set ( x , y , a[6] , i ) ;
edit]: I haven't used arrays since last year, so forgive my ignorance on them for now :(
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
No. They don't match. All three would appear to be different. There is no harm in naming parameters in a prototype. Otherwise you could do something icky-looking like int (&)[7] . It's not a reference to an int , it's a reference to an array of int .
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
ah wonderful!
I'm getting closer, but i need your help yet again.
Do you see the logic error in this part of my code?
edit]: stupid mistake; code deleted
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
else if ( answer == 'y' || answer == 'Y' )
{
cout << "How many toppings would you like (maximum of 7)?" << endl ;
cin >> i ;
cout << "Please choose from the following list: " << endl ;
cout << "\t1. Extra Pepperoni \t2. Extra Cheese" << endl ;
cout << "\t3. Sausage \t4. Jalapenos" << endl ;
cout << "\t5. Banana Peppers \t6. Green Peppers" << endl ;
cout << "\t7. Mushrooms \t8. M&Ms" << endl ;
cout << "Enter " << i << " topping numbers, followed by the 'Enter' key: " << endl ;
for ( i = 0 ; i < 7 ; i++ )
{
cin >> a[i] ;
}
}
am i doing something wrong here?
I only want to input the specified number into the array but it's making me input data for the entire array before moving on.
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
cin >> i ;
// ...
cout << "Enter " << i << " topping numbers, followed by the 'Enter' key: " << endl ;
for ( i = 0 ; i < 7 ; i++ )
Between hither and yon, you discard the value entered and hard-code the loop for all 7. You need a loop variable to count to the value entered (or else decrement the counter -- but that gets to be more work).
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
so i just tested my code again; thanks for the help btw !
here's what i got, lol:
<pre><code>Please choose from the following list:
1. Extra Pepperoni 2. Extra Cheese
3. Sausage 4. Jalapenos
5. Banana Peppers 6. Green Peppers
7. Mushrooms 8. M&Ms
Enter 2 topping numbers, followed by the 'Enter' key:
3
2
Thank you
Order Description:
Style: ☻
Size: s
Toppings: 1310720, 1310720, Press any key to continue . . .</code></pre>
a smiley face? ha!
So I would like to output the toppings they chose instead of well... in this case the address in memory.
But even if it output the correct number, i would like to have some way to say if it's a 1 output this if it's a 2 output this etc.
any suggestions? can you do a switch on an array?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
Using a char as a small int ?
// pizza.cpp
#include "pizza.h"
std::ostream& operator<< (std::ostream& o, const pizza &p)
{
switch ( p.m_size )
{
case pizza::small: o << "Small "; break;
case pizza::medium: o << "Medium "; break;
case pizza::large: o << "Large "; break;
}
switch ( p.m_type )
{
case pizza::deep_dish: o << "Deep Dish\n"; break;
case pizza::hand_tossed: o << "Hand-Tossed\n"; break;
case pizza::pan: o << "Pan\n"; break;
}
for ( int i = 0; i < sizeof p.m_topping / sizeof *p.m_topping; ++i )
{
switch ( p.m_topping[i] )
{
case pizza::none: break;
case pizza::pepperoni: o << " Pepperoni\n"; break;
case pizza::sausage: o << " Sausage\n"; break;
case pizza::mushrooms: o << " Mushrooms\n"; break;
case pizza::green_peppers: o << " Green Peppers\n"; break;
case pizza::canadian_bacon: o << " Canadian Bacon\n"; break;
case pizza::onions: o << " Onions\n"; break;
}
}
return o;
}
Just for ideas.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Ok i tried to do what you did and i failed.
=(
void Pizza::outputDesc ( int i )
{
cout << endl << "Style: " << type << endl ;
cout << "Size: " << size << endl ;
cout << "Toppings: " << flush ;
for ( int x = 0 ; x < i ; x++ )
{
switch ( toppings[x] )
{
case 1: cout << "Extra Pepperoni\n" ; break ;
case 2: cout << "Extra Cheese\n" ; break ;
case 3: cout << "Extra Sausage\n" ; break ;
case 4: cout << "Extra Jalapenos\n" ; break ;
case 5: cout << "Extra Banana Peppers\n" ; break ;
case 6: cout << "Extra Green Peppers\n" ; break ;
case 7: cout << "Extra Mushrooms\n" ; break ;
case 8: cout << "M&Ms\n" ; break ;
}
}
}
thanks for all this help; i really appreciate it
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
did you always have 3 rep blocks or did i just do that?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32