473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C++ assignment dealing with classes and functions

sonic
40
I'm not sure if I'm heading in the right direction with this school project. I am suppose to create a 3 file, class based program that will allow you to run a very simple pizza parlor program. The menu should be displayed similar to what is shown below.

MENU
--------------------------------------------------------------------------------------------
A) What size of pizza do you want?
B) What type of toppings do you want?
C) Display/Place current order.
D) Cancel current order.
E) Quit program.

I've only attempted to complete the first 2 menu items. Can anyone tell me if I'm heading in the right direction. I have 3 days before I need to finish this so any suggestions would be greatly appreciated. Here's the code I have so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // "Pizza.h"
  3. // Specification file for Pizza class
  4.  
  5. #ifndef PIZZA_H
  6. #define PIZZA_H
  7.  
  8. class Pizza
  9. {
  10.     private:
  11.         int pizzaSize;
  12.         int pizzaTop;
  13.  
  14.     public:
  15.         void setPizzaSize(int);
  16.         void setPizzaTop(int);
  17.         int getPizzaSize() const;
  18.         int getPizzaTop() const;
  19. };
  20.  
  21. #endif
  22.  
  23.  
  24.  
  25.  
  26. // "Pizza.cpp"
  27. // Implementation file for Pizza class
  28.  
  29. #include "Pizza.h"
  30. #include <iostream>
  31.  
  32. using namespace std;
  33.  
  34. // setPizzaSize sets default value of ordered pizza
  35. void Pizza::setPizzaSize(int p)
  36. {pizzaSize = p;}
  37.  
  38. // setPizzaTop sets topping default value of ordered pizza
  39. void Pizza::setPizzaTop(int t)
  40. {pizzaTop = t;}
  41.  
  42. // getPizzaSize returns value in the member variable pizzaSize
  43. int Pizza::getPizzaSize() const
  44. {return pizzaSize;}
  45.  
  46. // getPizzaTop returns value in the member variable pizzaTop
  47. int Pizza::pizzaTop() const
  48. {return pizzaTop;}
  49.  
  50.  
  51.  
  52. // "Pizza_Program.cpp"
  53. // Program simulates a simple pizza parlor menu ordering system
  54.  
  55. #include "Pizza.h"
  56. #include <iostream>
  57. #include <iomanip>
  58.  
  59. using namespace std;
  60.  
  61. // Function prototypes
  62. void displayMenu();
  63. void size(Pizza &);
  64. void topping(Pizza &);
  65.  
  66. int main()
  67. {    
  68.     Pizza dinner;  // Instance of pizza class object
  69.     char choice; // menu selection
  70.  
  71.     cout << fixed << showpoint << setprecision(2);
  72.  
  73.     do
  74.     {
  75.         // Display menu and get valid selection
  76.         diplayMenu();
  77.         cin >> choice;
  78.  
  79.         while (toupper(choice) < 'A' || toupper(choice) > 'B')
  80.         {
  81.             cout << "Please make a choice from the menu below (A-B): ";
  82.             cin >> choice;
  83.         }
  84.  
  85.         // Process user choice
  86.         switch(choice)
  87.         {
  88.         case 'a':
  89.         case 'A': cout << "Enter size of pizza (10,16,20,24 inches): ";
  90.                   size();
  91.                   break;
  92.  
  93.         case 'b':
  94.         case 'B': cout << "Please enter toppings on pizza: ";
  95.                   topping();
  96.                   break;
  97.         }
  98.     }
  99.     while (toupper(choice) != 'C');
  100.  
  101.     return 0;
  102. }
  103.  
  104. //********************************************************
  105. // This function displays the user's menu on the screen. *
  106. //********************************************************
  107.  
  108. void displayMenu()
  109. {
  110.     cout << "\n                    MENU\n";
  111.     cout << "--------------------------------------------------\n";
  112.     cout << "A)  What size of pizza do you want?\n";
  113.     cout << "B)  What type of toppings do you wants?\n";
  114.     cout << "C)  Quit Program\n\n";
  115.     cout << "Enter your choice: ";
  116. }
  117.  
  118. //********************************************************************************
  119. // This function accepts a reference to a Pizza object.  The user is asked for   *
  120. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  121. //********************************************************************************
  122.  
  123. void size(Pizza &dinner)
  124. {
  125.     int size;
  126.  
  127.     cout << "
  128.  
  129.  
Thank you for any help that can be offered. Would like to know if I'm heading in the right direction. I'm not posting my code in hopes of someone completing it, but rather to see if I'm heading at this the correct way. Thank you for you time in advance.

-Roger
Apr 21 '07 #1
10 5699
Ganon11
3,652 Expert 2GB
Everything looks good so far. One thing I noticed is, when you call the size() function from your main (inside the switch statement), you pass it no arguments. But, in the start of your definition, you have a single argument - a reference pizza object. This is also mentioned in your documentation. Perhaps you should make a pizza, and then call size using that pizza. You can also call toppings, etc. until the customer is done with the pizza, then add it to their order.
Apr 21 '07 #2
sonic
40
I understand what you said about passing nothing to my size function.....I can just pass a default value of 10"......however, not sure what you mean about making a pizza first and then calling size and toppings. Do you mean I should first make a pizza feeding in all the default arguments (first choices of size and topping menu)? Thank you for your earlier reply. Will continue working on project tomorrow.
Apr 22 '07 #3
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. //******************************************************************************
  2. // This function accepts a reference to a Pizza object.  The user is asked for   *
  3. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  4. //******************************************************************************
  5.  
  6. void size(Pizza &dinner)
You wrote down that this function should be receiving a reference to a Pizza object. But, in your main, you write size() with no arguments, so this won't compile.

What I'm thinking of is this: The user has told you they want to order a pizza. So, in order to have an object to work with, you create a Pizza object with no constructor arguments:

Expand|Select|Wrap|Line Numbers
  1. Pizza myPizza;
You should have a default constructor prepared to assign default values to toppings and size.

Now that you have a pizza, you can get the vital information from the user: what size do they want? What toppings? Etc. etc. You will pass myPizza to each function so that it can edit the pizza, changing the necessary information using the Pizza functions.
Apr 22 '07 #4
sonic
40
I think I've made the changes you suggested. I created a default constructor and have passed an instance of Pizza to both the size and topping function in main(). I'm still having problems with it compiling. Also, I'm not sure what to call in selection 'D' from the menu. Should I call the default constructor to clear previous customer selections? Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // "Pizza.h"
  3. // Specification file for Pizza class
  4.  
  5. #ifndef PIZZA_H
  6. #define PIZZA_H
  7.  
  8. class Pizza
  9. {
  10.     private:
  11.         int pizzaSize;
  12.         int pizzaTop;
  13.  
  14.     public:
  15.         Pizza(int, int);         // Constructor
  16.         void setPizzaSize(int);
  17.         void setPizzaTop(int);
  18.         int getPizzaSize() const;
  19.         int getPizzaTop() const;
  20. };
  21.  
  22. #endif
  23.  
  24.  
  25.  
  26. // "Pizza.cpp"
  27. // Implementation file for Pizza class
  28.  
  29. #include "Pizza.h"
  30. #include <iostream>
  31.  
  32. using namespace std;
  33.  
  34. // Construtor accepts arguments for pizzaSize and pizzaTop
  35. Pizza::Pizza(int s, int t)
  36. {
  37.     pizzaSize = s;
  38.     pizzaTop = t;
  39. }
  40.  
  41. // setPizzaSize sets default value of ordered pizza
  42. void Pizza::setPizzaSize(int p)
  43. {pizzaSize = p;}
  44.  
  45. // setPizzaTop sets topping default value of ordered pizza
  46. void Pizza::setPizzaTop(int t)
  47. {pizzaTop = t;}
  48.  
  49. // getPizzaSize returns value in the member variable pizzaSize
  50. int Pizza::getPizzaSize() const
  51. {return pizzaSize;}
  52.  
  53. // getPizzaTop returns value in the member variable pizzaTop
  54. int Pizza::pizzaTop() const
  55. {return pizzaTop;}
  56.  
  57.  
  58. // "Pizza_Program.cpp"
  59. // Program simulates a simple pizza parlor menu ordering system
  60.  
  61. #include "Pizza.h"
  62. #include <iostream>
  63. #include <iomanip>
  64.  
  65. using namespace std;
  66.  
  67. // Function prototypes
  68. void displayMenu();
  69. void size(Pizza &);
  70. void topping(Pizza &);
  71.  
  72. int main()
  73. {    
  74.     Pizza dinner;  // Instance of pizza class object
  75.     char choice; // menu selection
  76.  
  77.     cout << fixed << showpoint << setprecision(2);
  78.  
  79.     do
  80.     {
  81.         // Display menu and get valid selection
  82.         diplayMenu();
  83.         cin >> choice;
  84.  
  85.         while (toupper(choice) < 'A' || toupper(choice) > 'E')
  86.         {
  87.             cout << "Please make a choice from the menu below (A-E): ";
  88.             cin >> choice;
  89.         }
  90.  
  91.         // Process user choice
  92.         switch(choice)
  93.         {
  94.         case 'a':
  95.         case 'A': cout << "Enter size of pizza (10,16,20,24 inches): ";
  96.                   size(dinner);
  97.                   break;
  98.  
  99.         case 'b':
  100.         case 'B': cout << "Please enter toppings on pizza: ";
  101.                   topping(dinner);
  102.                   break;
  103.  
  104.         case 'c':
  105.         case 'C': cout << "You chose a " << dinner.getPizzaSize << "inch pizza with" << dinner.getPizzaTop << "\n";
  106.                   break;
  107.  
  108. // Not sure what to do here.  Should I call default construtor to wipe it clean??
  109.  
  110.         case 'd':
  111.         case 'D': 
  112.         }
  113.     }
  114.     while (toupper(choice) != 'E');
  115.  
  116.     return 0;
  117. }
  118.  
  119. //********************************************************
  120. // This function displays the user's menu on the screen. *
  121. //********************************************************
  122.  
  123. void displayMenu()
  124. {
  125.     cout << "\n                MENU\n";
  126.     cout << "--------------------------------------------------\n";
  127.     cout << "A)  What size of pizza do you want?\n";
  128.     cout << "B)  What type of toppings do you wants?\n";
  129.     cout << "C)  Display/Place current order.\n";
  130.     cout << "D)  Cancel current order.\n";
  131.     cout << "E)  Quit Program\n\n";
  132.     cout << "Enter your choice: ";
  133. }
  134.  
  135. //********************************************************************************
  136. // This function accepts a reference to a Pizza object.  The user is asked for   *
  137. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  138. //********************************************************************************
  139.  
  140. void size(Pizza &dinner)
  141. {
  142.     int size;
  143.  
  144.     cout << "Enter either (10,16,20,24) for the size pizza you would like: ";
  145.     cin >> size;
  146.     dinner.setPizzaSize(size);
  147.  
  148. }
  149.  
  150. //*******************************************************************************
  151. // This function accepts a reference to a Pizza object.  The user is asked for  *
  152. // choice of topping and the setPizzaTop member of Pizza object is called.      *
  153. //*******************************************************************************
  154.  
  155. void topping(Pizza &dinner)
  156. {
  157.     int topping;
  158.  
  159.     cout << "\t\t Toppings Choices\n";
  160.     cout << "\t---------------------------------\n";
  161.     cout << "\t 1) pepperoni\n";
  162.     cout << "\t 2) sausage\n";
  163.     cout << "\t 3) vegetarian\n";
  164.     cout << "\t 4) deluxe\n";
  165.     cout << "\t 5) plain cheese\n";
  166.     cin >> topping;
  167.     dinner.setPizzaTop(topping);
  168.  
  169. }
  170.  
  171.  
Sorry for all of the questions. I do greatly appreciate your help.
Apr 23 '07 #5
Ganon11
3,652 Expert 2GB
The constructor you have actually isn't a default constructor...it accepts 2 arguments (2 ints). The default constructor will accept no arguments, and will have the header:

Expand|Select|Wrap|Line Numbers
  1. Pizza();
and definition

Expand|Select|Wrap|Line Numbers
  1. Pizza::Pizza() {
  2.    // ...
  3. }
However, this isn't a huge problem. Once you add this, you should be fine in that department. (Inside the constructor's body, set the size and toppings so some default value, like 10 inches with 0 toppings).

Unfortunately, no, you can't call the constructor after an object has been made. The constructor is like a set of instructions, listing what steps should be taken to properly build a Pizza. Once the Pizza is built, it doesn't make sense to call the constructor again - it can't be built again!

You should instead add a new function, reset(). reset() can do just what it says - reset size and toppings to the default values.
Apr 23 '07 #6
sonic
40
Thank you again for the continued input. I still have having trouble getting it to compile. I have five errors which is down from the original 11 that I had....yay!. They are all in my pizza_program.cpp file. They say I have to create a pointer to a member. I'm not sure why I need a pointer. Here are the error messages:

Error 1 error C3867: 'Pizza::getPizzaSize': function call missing argument list; use '&Pizza::getPizzaSize' to create a pointer to member

Error 2 error C3867: 'Pizza::getPizzaTop': function call missing argument list; use '&Pizza::getPizzaTop' to create a pointer to member

Error 3 error C2065: 'dinner' : undeclared identifier

Error 4 error C2228: left of '.setPizzaSize' must have class/struct/union

Error 5 error C2228: left of '.setPizzaTop' must have class/struct/union


Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // "Pizza_Program.cpp"
  3. // Program simulates a simple pizza parlor menu ordering system
  4.  
  5. #include "Pizza.h"
  6. #include <iostream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. // Function prototypes
  12. void displayMenu();
  13. void size(Pizza &);
  14. void topping(Pizza &);
  15. void reset();
  16.  
  17. int main()
  18. {    
  19.     Pizza dinner;  // Instance of pizza class object
  20.     char choice; // menu selection
  21.  
  22.     cout << fixed << showpoint << setprecision(2);
  23.  
  24.     do
  25.     {
  26.         // Display menu and get valid selection
  27.         displayMenu();
  28.         cin >> choice;
  29.  
  30.         while (toupper(choice) < 'A' || toupper(choice) > 'E')
  31.         {
  32.             cout << "Please make a choice from the menu below (A-E): ";
  33.             cin >> choice;
  34.         }
  35.  
  36.         // Process user choice
  37.         switch(choice)
  38.         {
  39.         case 'a':
  40.         case 'A': cout << "Enter size of pizza (10,16,20,24 inches): ";
  41.                   size(dinner);
  42.                   break;
  43.  
  44.         case 'b':
  45.         case 'B': cout << "Please enter toppings on pizza: ";
  46.                   topping(dinner);
  47.                   break;
  48.  
  49.         case 'c':
  50.         case 'C': cout << "You chose a " << dinner.getPizzaSize << "inch pizza with" << dinner.getPizzaTop << "\n";
  51.                   break;
  52.  
  53.         case 'd':
  54.         case 'D': reset();
  55.         }
  56.     }
  57.     while (toupper(choice) != 'E');
  58.  
  59.     return 0;
  60. }
  61.  
  62. //********************************************************
  63. // This function displays the user's menu on the screen. *
  64. //********************************************************
  65.  
  66. void displayMenu()
  67. {
  68.     cout << "\n                MENU\n";
  69.     cout << "--------------------------------------------------\n";
  70.     cout << "A)  What size of pizza do you want?\n";
  71.     cout << "B)  What type of toppings do you wants?\n";
  72.     cout << "C)  Display/Place current order.\n";
  73.     cout << "D)  Cancel current order.\n";
  74.     cout << "E)  Quit Program\n\n";
  75.     cout << "Enter your choice: ";
  76. }
  77.  
  78. //********************************************************************************
  79. // This function accepts a reference to a Pizza object.  The user is asked for   *
  80. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  81. //********************************************************************************
  82.  
  83. void size(Pizza &dinner)
  84. {
  85.     int size;
  86.  
  87.     cout << "Enter either (10,16,20,24) for the size pizza you would like: ";
  88.     cin >> size;
  89.     dinner.setPizzaSize(size);
  90.  
  91. }
  92.  
  93. //*******************************************************************************
  94. // This function accepts a reference to a Pizza object.  The user is asked for  *
  95. // choice of topping and the setPizzaTop member of Pizza object is called.      *
  96. //*******************************************************************************
  97.  
  98. void topping(Pizza &dinner)
  99. {
  100.     int topping;
  101.  
  102.     cout << "\t\t Toppings Choices\n";
  103.     cout << "\t---------------------------------\n";
  104.     cout << "\t 1) pepperoni\n";
  105.     cout << "\t 2) sausage\n";
  106.     cout << "\t 3) vegetarian\n";
  107.     cout << "\t 4) deluxe\n";
  108.     cout << "\t 5) plain cheese\n";
  109.     cin >> topping;
  110.     dinner.setPizzaTop(topping);
  111.  
  112. }
  113.  
  114. //*********************************************************************
  115. // This function cancels the currect order.  It resets the pizzaSize  *
  116. // and pizzaTop private member variables.                             *
  117. //*********************************************************************
  118.  
  119. void reset()
  120. {
  121.     int size = 10;
  122.     int topping = 1;
  123.  
  124.     dinner.setPizzaSize(size);
  125.     dinner.setPizzaTop(topping);
  126.  
  127. }
  128.  
  129.  
  130.  
  131. // "Pizza.cpp"
  132. // Implementation file for Pizza class
  133.  
  134. #include "Pizza.h"
  135. #include <iostream>
  136.  
  137. using namespace std;
  138.  
  139. // Construtor 
  140. Pizza::Pizza()
  141. {
  142.     pizzaSize = 10;
  143.     pizzaTop = 1;
  144. }
  145.  
  146. // setPizzaSize sets default value of ordered pizza
  147. void Pizza::setPizzaSize(int p)
  148. {pizzaSize = p;}
  149.  
  150. // setPizzaTop sets topping default value of ordered pizza
  151. void Pizza::setPizzaTop(int t)
  152. {pizzaTop = t;}
  153.  
  154. // getPizzaSize returns value in the member variable pizzaSize
  155. int Pizza::getPizzaSize() const
  156. {return pizzaSize;}
  157.  
  158. // getPizzaTop returns value in the member variable pizzaTop
  159. int Pizza::getPizzaTop() const
  160. {return pizzaTop;}
  161.  
  162.  
  163. // "Pizza.h"
  164. // Specification file for Pizza class
  165.  
  166. #ifndef PIZZA_H
  167. #define PIZZA_H
  168.  
  169. class Pizza
  170. {
  171.     private:
  172.         int pizzaSize;
  173.         int pizzaTop;
  174.  
  175.     public:
  176.         Pizza();         // Constructor
  177.         void setPizzaSize(int);
  178.         void setPizzaTop(int);
  179.         int getPizzaSize() const;
  180.         int getPizzaTop() const;
  181. };
  182.  
  183. #endif
  184.  
  185.  
I'm going to continue trying. I'm not sure why I can't use dinner as an identifier in my reset() function. Do I need to use a destructor to eliminate the old Pizza dinner object and then use a constructor to rebuild it so I can reset it to default values? Thank you so much for your help!
Apr 24 '07 #7
sonic
40
I've tried to use a pointer like it suggests. Here's is what I did to my Pizza_Program.cpp file:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // "Pizza_Program.cpp"
  3. // Program simulates a simple pizza parlor menu ordering system
  4.  
  5. #include "Pizza.h"
  6. #include <iostream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. // Function prototypes
  12. void displayMenu();
  13. void size(Pizza &);
  14. void topping(Pizza &);
  15. void reset();
  16.  
  17. int main()
  18. {    
  19.     Pizza dinner;  // Instance of pizza class object
  20.     char choice; // menu selection
  21.     Pizza* pizzaPtr;
  22.     pizzaPtr = &dinner;
  23.  
  24.     cout << fixed << showpoint << setprecision(2);
  25.  
  26.     do
  27.     {
  28.         // Display menu and get valid selection
  29.         displayMenu();
  30.         cin >> choice;
  31.  
  32.         while (toupper(choice) < 'A' || toupper(choice) > 'E')
  33.         {
  34.             cout << "Please make a choice from the menu below (A-E): ";
  35.             cin >> choice;
  36.         }
  37.  
  38.         // Process user choice
  39.         switch(choice)
  40.         {
  41.         case 'a':
  42.         case 'A': cout << "Enter size of pizza (10,16,20,24 inches): ";
  43.                   size(dinner);
  44.                   break;
  45.  
  46.         case 'b':
  47.         case 'B': cout << "Please enter toppings on pizza: ";
  48.                   topping(dinner);
  49.                   break;
  50.  
  51.         case 'c':
  52.         case 'C': cout << "You chose a " << pizzaPtr->getPizzaSize << " inch pizza with" << pizzaPtr->getPizzaTop << "\n";
  53.                   break;
  54.  
  55.         case 'd':
  56.         case 'D': reset();
  57.         }
  58.     }
  59.     while (toupper(choice) != 'E');
  60.  
  61.     return 0;
  62. }
  63.  
  64. //********************************************************
  65. // This function displays the user's menu on the screen. *
  66. //********************************************************
  67.  
  68. void displayMenu()
  69. {
  70.     cout << "\n                MENU\n";
  71.     cout << "--------------------------------------------------\n";
  72.     cout << "A)  What size of pizza do you want?\n";
  73.     cout << "B)  What type of toppings do you wants?\n";
  74.     cout << "C)  Display/Place current order.\n";
  75.     cout << "D)  Cancel current order.\n";
  76.     cout << "E)  Quit Program\n\n";
  77.     cout << "Enter your choice: ";
  78. }
  79.  
  80. //********************************************************************************
  81. // This function accepts a reference to a Pizza object.  The user is asked for   *
  82. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  83. //********************************************************************************
  84.  
  85. void size(Pizza &dinner)
  86. {
  87.     int size;
  88.  
  89.     cout << "Enter either (10,16,20,24) for the size pizza you would like: ";
  90.     cin >> size;
  91.     dinner.setPizzaSize(size);
  92.  
  93. }
  94.  
  95. //*******************************************************************************
  96. // This function accepts a reference to a Pizza object.  The user is asked for  *
  97. // choice of topping and the setPizzaTop member of Pizza object is called.      *
  98. //*******************************************************************************
  99.  
  100. void topping(Pizza &dinner)
  101. {
  102.     int topping;
  103.  
  104.     cout << "\t\t Toppings Choices\n";
  105.     cout << "\t---------------------------------\n";
  106.     cout << "\t 1) pepperoni\n";
  107.     cout << "\t 2) sausage\n";
  108.     cout << "\t 3) vegetarian\n";
  109.     cout << "\t 4) deluxe\n";
  110.     cout << "\t 5) plain cheese\n";
  111.     cin >> topping;
  112.     dinner.setPizzaTop(topping);
  113.  
  114. }
  115.  
  116. //*********************************************************************
  117. // This function cancels the currect order.  It resets the pizzaSize  *
  118. // and pizzaTop private member variables.                             *
  119. //*********************************************************************
  120.  
  121. void reset()
  122. {
  123.     int size = 10;
  124.     int topping = 1;
  125.  
  126.     dinner.setPizzaSize(size);
  127.     dinner.setPizzaTop(topping);
  128.  
  129. }
  130.  
  131.  
I'm still getting same error about creating a pointer to a member.
Apr 24 '07 #8
Ganon11
3,652 Expert 2GB
OK, stick with the non-pointer version. Using pointers now will likely get you confused later. You are getting the pointer-errors because, in your display, you say dinner.getPizzaTop without parentheses. Adding parentheses to this function call (and getPizzaSize) should eliminate the error.

Next, dinner is undeclared because it is a local object, declared inside the main() fnction. That means it is only visible to the main() and not in reset. The reset() function should be a part of the Pizza class, so you will call dinner.reset() to reset the Pizza. Try these changes and see if it works.

It's looking closer and closer every time!
Apr 24 '07 #9
sonic
40
Almost there I think. What am I doing wrong in my class declaration file? I think reset() would have to be able receive two integers to reset private member variables pizzaSize and pizzaTop. However, I seem to be running into problems when I try to return Pizza (my constructor) to void Pizza:reset(). Do I need to change void Pizza::reset() to enable it to accept two integers? Ex: Pizza::reset(int, int) Here's what I have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // "Pizza.h"
  3. // Specification file for Pizza class
  4.  
  5. #ifndef PIZZA_H
  6. #define PIZZA_H
  7.  
  8. class Pizza
  9. {
  10.     private:
  11.         int pizzaSize;
  12.         int pizzaTop;
  13.  
  14.     public:
  15.         Pizza();         // Constructor
  16.         void setPizzaSize(int);
  17.         void setPizzaTop(int);
  18.         int getPizzaSize() const;
  19.         int getPizzaTop() const;
  20.         void reset(int, int);
  21.  
  22. };
  23.  
  24. #endif
  25.  
  26.  
  27.  
  28. // "Pizza.cpp"
  29. // Implementation file for Pizza class
  30.  
  31. #include "Pizza.h"
  32. #include <iostream>
  33.  
  34. using namespace std;
  35.  
  36. // Construtor 
  37. Pizza::Pizza()
  38. {
  39.     pizzaSize = 10;
  40.     pizzaTop = 1;
  41. }
  42.  
  43. // setPizzaSize sets default value of ordered pizza
  44. void Pizza::setPizzaSize(int p)
  45. {pizzaSize = p;}
  46.  
  47. // setPizzaTop sets topping default value of ordered pizza
  48. void Pizza::setPizzaTop(int t)
  49. {pizzaTop = t;}
  50.  
  51. // getPizzaSize returns value in the member variable pizzaSize
  52. int Pizza::getPizzaSize() const
  53. {return pizzaSize;}
  54.  
  55. // getPizzaTop returns value in the member variable pizzaTop
  56. int Pizza::getPizzaTop() const
  57. {return pizzaTop;}
  58.  
  59. // reset sets default values of pizzaSize and pizzaTop member variables
  60. void Pizza::reset()
  61. {return Pizza;}
  62.  
  63.  
  64.  
  65. // "Pizza_Program.cpp"
  66. // Program simulates a simple pizza parlor menu ordering system
  67.  
  68. #include "Pizza.h"
  69. #include <iostream>
  70. #include <iomanip>
  71.  
  72. using namespace std;
  73.  
  74. // Function prototypes
  75. void displayMenu();
  76. void size(Pizza &);
  77. void topping(Pizza &);
  78. void reset();
  79.  
  80. int main()
  81. {    
  82.     Pizza dinner;  // Instance of pizza class object
  83.     char choice; // menu selection
  84.     char subChoice; // sub-menu selection in select case 'C'
  85.  
  86.     cout << fixed << showpoint << setprecision(2);
  87.  
  88.     do
  89.     {
  90.         // Display menu and get valid selection
  91.         displayMenu();
  92.         cin >> choice;
  93.  
  94.         while (toupper(choice) < 'A' || toupper(choice) > 'E')
  95.         {
  96.             cout << "Please make a choice from the menu below (A-E): ";
  97.             cin >> choice;
  98.         }
  99.  
  100.         // Process user choice
  101.         switch(choice)
  102.         {
  103.         case 'a':
  104.         case 'A': size(dinner);
  105.                   break;
  106.  
  107.         case 'b':
  108.         case 'B': topping(dinner);
  109.                   break;
  110.  
  111.         case 'c':
  112.         case 'C': cout << "You chose a " << dinner.getPizzaSize() << " inch pizza with" << dinner.getPizzaTop() << "\n\n";
  113.                   cout << "Choose either (R) to restart or (E) to exit.";
  114.                   cin >> subChoice;
  115.                   if (toupper(subChoice) != 'R' || toupper(subChoice) != 'E')
  116.                   {cout << "Please enter R or D: ";
  117.                    cin >> subChoice;
  118.                   }
  119.                   else
  120.                   break;
  121.  
  122.         case 'd':
  123.         case 'D': reset();
  124.                   size(dinner);
  125.                   break;
  126.         }
  127.     }
  128.     while (toupper(choice) != 'E');
  129.  
  130.     return 0;
  131. }
  132.  
  133. //********************************************************
  134. // This function displays the user's menu on the screen. *
  135. //********************************************************
  136.  
  137. void displayMenu()
  138. {
  139.     cout << "\n                MENU\n";
  140.     cout << "--------------------------------------------------\n";
  141.     cout << "A)  What size of pizza do you want?\n";
  142.     cout << "B)  What type of toppings do you wants?\n";
  143.     cout << "C)  Display/Place current order.\n";
  144.     cout << "D)  Cancel current order.\n";
  145.     cout << "E)  Quit Program\n\n";
  146.     cout << "Enter your choice: ";
  147. }
  148.  
  149. //********************************************************************************
  150. // This function accepts a reference to a Pizza object.  The user is asked for   *
  151. // size of pizza and the setPizzaSize member of Pizza object is called.          *
  152. //********************************************************************************
  153.  
  154. void size(Pizza &dinner)
  155. {
  156.     int size;
  157.  
  158.     cout << "Enter either (10,16,20,24) for the size pizza you would like: ";
  159.     cin >> size;
  160.     if (size != 10 && size != 16 && size != 20 && size != 24)
  161.         {cout << "Please enter 10,16,20,24 for size of pizza!";
  162.         cin >> size;
  163.         }
  164.     else
  165.     dinner.setPizzaSize(size);
  166.  
  167. }
  168.  
  169. //*******************************************************************************
  170. // This function accepts a reference to a Pizza object.  The user is asked for  *
  171. // choice of topping and the setPizzaTop member of Pizza object is called.      *
  172. //*******************************************************************************
  173.  
  174. void topping(Pizza &dinner)
  175. {
  176.     int topping;
  177.  
  178.     cout << "\t\t Toppings Choices\n";
  179.     cout << "\t---------------------------------\n";
  180.     cout << "\t 1) pepperoni\n";
  181.     cout << "\t 2) sausage\n";
  182.     cout << "\t 3) vegetarian\n";
  183.     cout << "\t 4) deluxe\n";
  184.     cout << "\t 5) plain cheese\n";
  185.     cin >> topping;
  186.     if (topping < 1 || topping > 5)
  187.     {cout << "Please enter a valid topping (1-5): ";
  188.     cin >> topping;
  189.     }
  190.  
  191.     dinner.setPizzaTop(topping);
  192.  
  193. }
  194.  
  195. //*********************************************************************
  196. // This function cancels the currect order.  It resets the pizzaSize  *
  197. // and pizzaTop private member variables.                             *
  198. //*********************************************************************
  199.  
  200. void reset()
  201. {
  202.     dinner.reset();
  203. }
  204.  
  205.  
I really appreciate the help!
Apr 24 '07 #10
Ganon11
3,652 Expert 2GB
OK, I tried compiling this, and I got the following errors:

Expand|Select|Wrap|Line Numbers
  1. 60 C:\Documents and Settings\Stark\My Documents\Untitled1.cpp prototype for `void Pizza::reset()' does not match any in class `Pizza' 
  2. 19 C:\Documents and Settings\Stark\My Documents\Untitled1.cpp void Pizza::reset(int, int) 
You declared the function as receiving two ints, then defined it as having no arguments. I don't think it should have any int arguments - if the Pizza is being reset, then what does it matter what size/toppings it has? The user should still have to input that information. Thus, the reset() function should set size and toppings to some arbitrary value - perhaps 0, since you know you can't have a pizza of diameter 0 inches. If you ever see an output of 0 inches, you know the pizza hasn't had its size set.

Expand|Select|Wrap|Line Numbers
  1. C:\Documents and Settings\Stark\My Documents\Untitled1.cpp In member function `void Pizza::reset()': 
  2. 60 C:\Documents and Settings\Stark\My Documents\Untitled1.cpp expected primary-expression before ';' token 
  3. 60 C:\Documents and Settings\Stark\My Documents\Untitled1.cpp return-statement with a value, in function returning 'void' 
These two deal with the fact that you are trying to return a class name in reset() - this is not how it should be implemented.

Expand|Select|Wrap|Line Numbers
  1.  C:\Documents and Settings\Stark\My Documents\Untitled1.cpp In function `void reset()': 
  2. 201 C:\Documents and Settings\Stark\My Documents\Untitled1.cpp `dinner' undeclared (first use this function) 
Again, dinner is local to main() and not visible in any other function - instead of having a function reset() that calls dinner.reset(), why not just call dinner.reset() in the first place?
Apr 24 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
9
by: Klaus Neuner | last post by:
Hello, I would like to understand the reason for the following difference between dealing with lists and dealing with strings: What is this difference good for? How it is accounted for in Python...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
17
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
2
by: gyarnell | last post by:
Just ran into a problem upgrading from GCC 2.96 to GCC 3.3.2. There is a bug somewhere, could be in 2.96, could be in 3.3.2, could be in the code I'm compiling. Here's a minimal example class A {...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
1
by: Bartholomew Simpson | last post by:
I know this is a VB.Net ng. But hopefully, someone remembers some classic (VB6) coding - besides I've not had any response when I posted this to the VB6 specific ng. I wrote a C++ librray that I...
3
by: Rambaldi | last post by:
Since i am not use to program in c++, i got this question: I've been thinking in making a mp3 player in C#, the language i am more confortable dealing with, but instead of using librarys provided...
1
by: khalfan | last post by:
Assignment Objectives: The objective of this assignment is to implement java programs with major object oriented concepts and applet with event handling. Assignment Purposes This assignment aims...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.