Sign in to follow this  
NUCLEAR RABBIT

WTF? FUCNTIONS!

Recommended Posts

NUCLEAR RABBIT    318
+-My Questions have to do with fucntions/Classes-+ 1) In this code, is it possible to make variables global without declairing them in the function like in the function gameLoop? 2) Could i make classes, and use the variables within that class in my functions? (How would I do that :[ ) +-Thanks for Any Help-+
/**$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*$
$*%    PROGRAMMER NAME: BRANDON WALL   *$%
%*$    DATE:  4-15-06                  %*$
*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$**/ 

// Practicing Functions4
// Paper, Scissor, Rock : Evolution

#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

      //Declairing Variables 
      int weapon, comp, compWeapon;
      char playAgain;
      
//First Function
void choice()
{
     
     cout << "Welcome! Are you ready to Play PSR-Evolution? \n"
          << "Who cares? Your playing anyway!"
          << "\n\n"
          << "Here are the rules: \n\n"
          << "***************************************\n"
          << "**                                   **\n"
          << "** 1) Flamthrower beats Gea Pet      **\n"
          << "** 2) Gea Pet Beats Water Hoes       **\n"
          << "** 3) Water Hoes Beats Flamethrower  **\n"
          << "**                                   **\n"
          << "***************************************\n\n"
          << "Have Fun!\n\n";
          
     //Waits for user to continue
     system("pause");
     
}
     
void weaponSelection()
{
     //Clears screen to pick Weapon
     system("cls");
     
     //Weapon Selection
     cout << "\n\n"
          << "****************************" << endl
          << "**                        **" << endl
          << "** +Select Your Weapon+   **" << endl
          << "**                        **" << endl
          << "** 1) FLAMETHROWER        **" << endl
          << "** 2) GEA PET             **" << endl
          << "** 3) WATER HOES          **" << endl
          << "**                        **" << endl
          << "****************************\n\n"
          << "\nWeapon Of Choice: ";
     cin >> weapon;
     system("cls");

}

void gameLoop(int weapon, int comp, int compWeapon)
{
     
     //Generates The comps Random choice
     srand(time(0));
     comp = rand();
     compWeapon = comp % 3 + 1;
     
     //Game Loop Begins
     if(weapon == 1 && compWeapon == 3)
     {
                  cout << "You picked a FlameThrower and The Computer picked a "
                       << "Water Hoes"
                       << "\n\nSorry, Game Over!\n\n";
                  system("pause");
     }
     else if(weapon == 2 && compWeapon == 3)
     {
               cout << "You picked Gea Pet and the Computer picked "
                    << "Water Hoes"
                    << "\n\nYAY! You Won!\n\n";
               system("pause");
     }
     else if(weapon == 3 && compWeapon == 3)
     {
          cout << "You picked Water Hoes and the Computer picked "
               << "Water Hoes"
               << "\n\noOOoOo! It's a Draw!\n\n";
          system("pause");
     }
     
     //Second Loop Summary
     if(weapon == 1 && compWeapon == 2)
     {
                  cout << "You picked Flamethrower and the Computer picked "
                       << "Gea Pet"
                       << "\n\nGood Job! You Won!\n\n";
                  system("pause");
     }
     if(weapon == 2 && compWeapon == 2)
     {
               cout << "You picked Gea Pet and the Computer picked "
                    << "Gea Pet"
                    << "\n\nIts a Draw!\n\n";
               system("pause");
     }
     else if(weapon == 3 && compWeapon == 2)
     {
          cout << "You picked Water Hoes and the Computer picked "
               << "Gea Pet"
               << "\n\nToo bad...You Lost!\n\n";
          system("pause");
     }
     
     //Last Game Loop
     if(weapon == 1 && compWeapon == 1)
     {
                  cout << "You picked Flamethrower and the Computer picked "
                       << "Flamethrower"
                       << "\n\nIts a Draw!\n\n";
                  system("pause");
     }
     if(weapon == 2 && compWeapon == 1)
     {
               cout << "You picked Gea Pet and the Computer picked "
                    << "Flamethrower"
                    << "\n\nYou Lost! Game Over!\n\n";
               system("pause");
     }
     else if(weapon == 3 && compWeapon == 1)
     {
          cout << "You picked Water Hoes and the Computer picked "
               << "Flamethrower"
               << "\n\nGood Job! You Are Successful!\n\n";
          system("pause");
     }
     
}

int main()
{
    choice();
    weaponSelection();
    gameLoop(weapon, comp, compWeapon);
    
    //Clears Screen for loop option
    system("cls");
    
    cout << "Play Again? (y or n): ";
    cin >> playAgain;
    
    while(playAgain == 'y')
    {
                    //Clear Screen for New Game
                    system("cls");
                    
                    weaponSelection();
                    gameLoop(weapon, comp, compWeapon);
                    
                    //clears screen for Loop Option
                    system("cls");
                    
                    cout << "Play Again? (y or n): ";
                    cin >> playAgain;
    }
    if(playAgain == 'n')
    {
                 //Clears Screen for Last Greeting
                 system("cls");
                 
                 cout << "*********************************\n"
                      << "**                             **\n"
                      << "**   Thank You For Playing!    **\n"
                      << "**          Buh-Bye!           **\n"
                      << "**                             **\n"
                      << "*********************************\n\n";
                 system("pause");
    }
    
    return 0;
}

Share this post


Link to post
Share on other sites
matthughson    588
Quote:
Original post by NUCLEAR RABBIT
1) In this code, is it possible to make variables global without declairing them in the function like in the function gameLoop?


A variable declared inside a function would not be global. A global variable is one that is declared outside of class declarations and functions:

// This variable is global
int iGlobalVar;

void someFunction()
{
// This one is not
int iNotGlobalVar;
}


Quote:
Original post by NUCLEAR RABBIT
2) Could i make classes, and use the variables within that class in my functions? (How would I do that :[ )


I'm not exactly sure what you mean here, but it kind of sounds like "static member variables". These are variables that belong to a class as a whole, rather than induvidual objects.

Does that answer your questions?

Matt

Share this post


Link to post
Share on other sites
Although globals are considered bad practice, it is entirely possible to utilize them. basicly, declare them outside of the scope of the function.

Another option is to have a purely static class with the globals set to public.

class globals
{
public:
static int var_1;
static int var_2;
};

...somewhere in your code...

globals::var_1 = globals::var2;

this works, but it's also kind of messy. I highly suggest picking up Java or another pure OO language to learn from, it will teach you how to not be dependant on globals, which are bad :P.

Share this post


Link to post
Share on other sites
NUCLEAR RABBIT    318
just to make things crystal clear, could i for example, do this:

class Poop
{
int diaper = 45;
int babyWipe = 10;
};

and in my function called trash do this:

void trash()
{

cout << diaper;
cout << "\n\n";
cout << babyWipe;
system("pause");
}

int main()
{
trash();
return 0;
}

Share this post


Link to post
Share on other sites
EricmBrown    124
Yes you can. First you have to declare an instance of the class. So it would look like this


class Poop
{
public: //we have to make the variable public in order to access them
//variables/functions in classes are "private" by default
int diaper;
int babyWipe;
};

//and in my function called trash do this:

void trash()
{
Poop myPoop; //a variable of type Poop

cout &lt;&lt; myPoop.diaper;
cout &lt;&lt; "\n\n";
cout &lt;&lt; myPoop.babyWipe;

system("pause");
}

Share this post


Link to post
Share on other sites
EricmBrown    124
Just to add, I dont think you can assign class variables values directly in c++.

class Poop
{
int diaper = 45;
int babyWipe = 10;
};

isn't legal.

Instead you have to say


class Poop
{
int diaper;
int babyWipe;

Poop() //this is called a "constructor", it is a function called automatically when an instance of Poop is created
{
diaper = 45;
babyWipe = 10;
}

};

Share this post


Link to post
Share on other sites
matthughson    588
Quote:
Original post by NUCLEAR RABBIT
just to make things crystal clear, could i for example, do this:

*** Source Snippet Removed ***


Nope. Variables defined in classes are known as members. Unless they are static members, you cannot access them directly. What you want to do is create an instance of that class, and access the members through it.


class Gun
{
public:

int ammo;
int type;
char name[256];
}

void main()
{
// First create an instance of a gun
Gun rifle;

// Now rifle stores versions of all the variables declared in the Gun class, and
// can be accessed as such
rifle.ammo = 10;
rifle.type = 0;
strcpy(rifle.name, "22 Cal. Rifle");

// You can also create a different instance of a gun, which will store its
// own versions of all those variables
Gun machineGun;

machineGun.ammo = 5500;
machineGun.type = 1;
strcpy(machineGun.name, "Machine Gun");

// To print out what is stored in the Gun objects you need to do something like this:

cout << "Rifle: Ammo = " << rifle.ammo << ", type = " << rifle.type << ", name = " << rifle.name << "\n";
cout << "Machine Gun: Ammo = " << machineGun.ammo << ", type = " << machineGun.type << ", name = " << machineGun.name << "\n";
}

// This would output

Rifle: Ammo = 10, type = 0, name = 22 Cal. Rifle
Machine Gun: Ammo = 5500, type = 1, name = Machine Gun

Share this post


Link to post
Share on other sites
DeadXorAlive    535
You can't do that. A class defines a type, not an object. So you first have to make an object.

In you example (what's up with the poop?):

class Poop
{
// class member default to private, meaning access only inside class scope.
// Public means everybody can mess with things.
public:
// A constructor is sort of a member function with the same name as the class, which
// gets called when you 'make' a new object
Poop(int diaper, int babyWipe)
: BabyWipe(babyWipe), Diaper(diaper) // initialize these members
{} // you can go more stuff here if you want
// Members will be initialized in the order they are declared, so you might think
// BabyWip will be initialized first, that isn't the case as Diaper is declared first.
int Diaper;
int BabyWipe;
}; //

void trash()
{
Poop SomePoop(45, 10); // make some poop
Poop SomeMorePoop(100, 30); // more poop

cout << SomePoop.Diaper; // prints 45
cout << "\n\n";
cout << SomePoop.BabyWipe; //prints 10
cout << "\n\n";
cout << SomeMorePoop.Diaper; //prints 100
cout << "\n\n";
cout << SomeMorePoop.BabyWipe; // prints 30

system("pause");
}



Now this is just a very small part of all things you can do with classes, and not particularly useful or proper. You need to have something like a good book about it, in the noteworthy threads sticky are some very useful pointers to good books and / or tutorials. Here's two I liked:
C++ in action, can be a bit difficult but very good.
C++ a dialog easier read, first 6 or 7 chapters for free.

Good luck.




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this