pizza

Only sharing some recent progress.

Managed to find a book about C++ game programming. Instead of writing about aliens I figured I would write about pizza though.

Here is the code if anyone is interested
I do not totally understand the ENUM feature though. Is this like defining an integer with multiple values? It is great for defining different difficulties or "levels" of a certain idea I have found. Does anyone have any input on ENUM?

I will most likely read over the explanation in a few minutes although I wanted to share because I like the way the book is written so far.

#include <iostream>
using namespace std;

// Pizza stats
// Demonstrates constants

int main()
{
    const int PIZZA_POINTS = 150;
    int pizzasEaten = 10;
    int score = pizzasEaten * PIZZA_POINTS;
    cout << "score: "  << score << endl;
    
    enum fatness {REGULAR, CRITIC, SOUSCHEF, NEWYORKER, ITALIAN};
    fatness myfatness = CRITIC;
    
    enum cafe {PIZZERIA = 25, CAFE, BAR = 50, BAKERY = 100};
    cafe mycafe = CAFE;
    cout << "\nTo upgrade pizzerias, you must use "
    << (BAR - mycafe)  << " units of fatness.\n";
    cin.get();
    return 0;
    }


Note ** Something everyone probably already knows but I thought was interesting and have just read on using ENUM : Any enumerators that are not assigned values get the value of the previous enumerator plus one.

Here is what it makes (basic program with predefined number of eaten pizzas and fatness)
http://img.photobucket.com/albums/v203/Lerygen/Untitled-2-3.jpg
If you have a series of mutually exclusive items that you need to keep track of, you can give them each a value to keep track of what the current object is. The following code snippets are essentially equivalent.

For example, if you have a computer that could be in multiple states, you may use this:
1
2
3
4
5
6
int state = 0; //STBY
state = 1; //Startup
state = 2; //Busy
state = 3; //Off

if (state == 0) //do something 


If you don't want the next prgrammer to memorize this convention then you could use another method (applicable to C):
1
2
3
4
5
6
#define STBY 0
#define Startup 1
#define Busy 2
#define Off 3

if (state == STBY) //do something 


defines can get messy if you have too many as they complete overwrite anything else you have writte. Therefore you could also define them as constants:
1
2
3
4
5
6
const int STBY = 0;
const int Startup = 1;
const int Busy = 2;
const int Off = 3;

if (state == STBY) // do something 


The problem with defining them as constants is that you suddenly have a bunch of global variables which aren't really stuck togeather in an easy-to-read fashtion. While equivalent, in a more c++ style, you can define them as enums. The thing about enums is that the value 0,1,2,3 doesn't really matter. The only thing that matters is that they are different. There isn't much of an advantage other than you don't need to manually set the values.

1
2
3
4
5
6
7
8
9
enum states
{
    STBY,
    Startup,
    Busy,
    Off,
};

if (state == STBY) // do something 
Last edited on
Thank you for the information! It is very helpful. I like the way you have written the last box with multiple lines, it is much easier to understand.

I have also just written program about pizza and polar bears.

I will have to look into this further. It is very fun.
I have not learned about strings yet also as well as #define (which looks very useful) but the example program was using a string for the name so I figured it was alright. Also, I am not sure why but the program would still close even after typing cin.get(); once before return 0; at the end of the program so I had to use two, I am not sure why.



#include <iostream>
using namespace std;

int main()
{
    const int PARMESEAN_CHEESE = 900;
    int pizzas, pizzasEaten, remaining;
    string chef;
    
        
    //get the information
    cout << "Welcome to Pizza Hut \n";
    cout << "Please enter the following for your personalized adventure \n";
    
    cout << "Enter a number: ";
    cin >> pizzas;
        
    cout << "Enter a number, smaller than the first: ";
    cin >> pizzasEaten;
    
    remaining = pizzas - pizzasEaten;
    
    cout << "enter generic name: ";
    cin >> chef;
    
    
    // story
    
    cout << "\nA group of " << pizzas << " hot and ready pizzas";
    cout << "are placed on the buffet line by the chef " << chef << ".\n";
    
    cout << "however since the restraunt is located in Antartica, \n"; 
    cout << "it often does not receive many customers. \n" ;
    
    cout << "Of the pizzas, " << pizzasEaten << "were eaten by polar bears,";
    cout << "leaving only " << remaining << " on the worlds only \nsolar powered climate controlled buffet table";
    cout << "located within the worlds finest sub - antarctican diner.";
    cout << "\nThis was troubling for " << chef << ", and inspiring chef.\n";
    cout << "However, after much thought," << chef << "realized that investing\n";
    cout << "in a new location would be detrimental to the customers \nhe currently provides for.\n";
    cout << "In an act of contrition," << chef << " decides to donate the remaining " << PARMESEAN_CHEESE;
    cout << "boxes of parmesean cheese up between the polar bears and";
    cout << " hold on to a modest" << (PARMESEAN_CHEESE / remaining);
    cout << " for the road to spice up the remaining pizzas";
    cin.get();
     cin.get();
    
    return 0;
    }
    
    


Last edited on
Cool. I made a simple program that uses the #define function and I have to say I like how you don't have to type a semicolon at the end.

#include <iostream>
using namespace std;

//defining variables

#define x 1
#define y 2
#define apples (x + y)

int main()
{
cout << "x = " << x << ".\n";
cout << "y = " << y << ".\n";
cout << "x - y = " << x - y << ".\n";
cin.get();


cout << "Maria takes " << x << " apples from the apple tree, leaving\n ";
cout << y <<"left for the birds, only one less than the original " << apples << ".";

cin.get();
return 0;
}
Last edited on
Defines are more of a C thing instead of C++. They can be very useful. What happens here is that in
#define x 1
any instance of x is replaced with 1 before even any compiling gets done. This makes things effecient at runtime, but be careful. The reason is that if you have some defines and functions and/or objects with the same name, you may not get warnings or errors and you'll get some very funny behavior that is tough to debug.

For this reason, enums are nicer as you WILL get errors or warnings when applicable.


Defines can also be used for simple inline functions. But here is another example of where they fall short:

#define max(a,b) (((a) > (b)) ? (a) : (b))

This will return the maximum of a or b. However, if you call it with something like this:
max(i++, j)
you'll see that it will be replace with this before the compiler gets involved:
(((i++) > (j)) ? (i++) : (j))

We see that i gets incremented twice which was not our intent.

In conclusion, you are welcome to use defines, but understand where there may be problems.
Last edited on
I think I get it.

if you define something with #define though can you then redefine it later with the same function?

say
1
2
3
4
5
6
7
8
 
#define x 1

cout << "x is now 1.\n";

#define x 2

cout << "x is now 2.\n";




or what if you write

#define A 1
and then write
A = 2

Would the simple command of writing A=2 overwrite this variable or are they stored in different places? I am interested in grasping the basics before I move on to more complex time consuming things. Thank you for your input up until this point. I believe I will most likely move on to the next chapter momentarily.



Edit ** I will have to look into defining variables further, it looks like there are many ways.
I believe int A = 2 would also work.

I am going to have to make my own website before long.
Last edited on
You'd have to redefine it later. However be careful because this is done before the compiler even sees the code.

To redefine do this:

1
2
3
4
5
6
7
8
#define x 1

cout << "x is now 1.\n";

#undef x
#define x 2

cout << "x is now 2.\n";



Defines are best to avoid when doing logic. Here is a more useful application for them... Say you are writing a professional application. You only want to use certain couts for debug purposes. Therefore we don't want to compile those lines when we release the real thing. We can get rid of them with defines:

1
2
3
4
5
6
7
8
//#define _debug

#ifdef _debug
cout << "We are in debug mode\n";
cout << "Here's some debug information.....blah blah blah\n";
#else
cout << "We are in release mode\n";
#endif 


If we manually define _debug then our compiler won't even compile line 7. If we comment out that define, then the compiler won't compile lines 4-5.
Topic archived. No new replies allowed.