Thread: another structure problem

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    178

    another structure problem

    Here's my problem. I have a structure declared before my main function

    struct yourstats
    { char classname[80];
    int hp;
    int maxhp;
    int mp;
    int maxmp;
    int attack;
    int defense;
    }stat;

    I want this to be shared so that i can use stat for all of my function, which right now I have 3. Is there a way to pass these without using parameters.

    For example I have in a chooseclass function where it sets all of those variables, i put in a cout statement to make sure it works in that function and it does,

    yourstats stat={"Warrior", 30, 30, 5, 5, 20, 15};

    But when I try to access it from another function

    cout<< "Welcome " << name <<" the "<<stat.classname<<". What would you like to do? "<<endl;

    All i get is a blank. Any suggestions? THanks in advance

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    make the declaration of your variable above the main() that will become a global variable.
    Code:
    struct yourstats 
    { char classname[80]; 
    int hp; 
    int maxhp; 
    int mp; 
    int maxmp; 
    int attack; 
    int defense; 
    }; 
    
    yourstats stat;
    
    int main()
    {
      stat={"Warrior",30,30,5,5,20,15};
    ....
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    178
    thats basically what I have. Well, I know it's kinda long but here's my source, tell me if you can find why it won't work right. If I leave the stat= in the chooseclass function it won't compile and gives me a parse error and basically ends up confusing the compiler. But if i change each of them to yourstats stat = it works fine, it just doesn't keep the values once it leave the function. I edited some of the pointless stuff to save reading.

    int chooseclass();
    int menu();

    char name[80]; //declare a variable to hold your name
    int exp;

    struct yourstats
    { char classname[80];
    int hp;
    int maxhp;
    int mp;
    int maxmp;
    int attack;
    int defense;
    } stat;

    struct badstats
    {
    char badname[80];
    int badhp;
    int badmp;
    int badattack;
    int baddefense;
    int gainexp;
    int gainbons;
    } badstat;

    int main()
    {
    chooseclass();
    menu();

    system("PAUSE");
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////
    int chooseclass()
    {
    int type;

    do
    {
    system("cls"); //clear the screen
    cout << "Choose one of the following: \n" << " (1) Warrior \n";
    cout << " (2) Sorceror \n" << " (3) Elf \n" << " (4) Fairy \n";
    cout << " (5) Ogre \n" << " (6) Cyclops \n" << " (7) Devil \n";
    cout << "\nEnter your choice (for help press 8): ";
    cin >> type;
    }while(type <1 || type >8);

    switch(type) //sets the stats for the various characters
    {
    case 1: //warrior
    {
    stat={"Warrior", 30, 30, 5, 5, 20, 15};
    break;
    }
    case 2: //sorceror
    {
    stat={"Sorceror", 20, 20, 30, 30, 3, 5};
    break;
    }
    case 3: //elf
    {
    stat={"Elf", 15, 15, 15, 15, 10, 10};
    break;
    }
    case 4: //fairy
    {
    stat={"Fairy", 25, 25, 20, 20, 10, 20};
    break;
    }
    case 5: //ogre
    {
    stat={"Ogre", 23, 23, 5, 5, 27, 25};
    break;
    }
    case 6: //cyclops
    {
    stat={"Cyclops", 30, 30, 15, 15, 30, 15};
    break;
    }
    case 7: //devil
    {
    stat={"Devil", 25, 25, 25, 25, 20, 15};
    break;
    }
    } //end switch
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////
    int menu()
    {
    int choice;

    do
    {
    system("cls");
    cout<< "Welcome " << name <<" the "<<stat.classname<<". What would you like to do? "<<endl;
    }while(choice < 1 || choice > 9);
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    change
    int exp; to int experience; //for there is a math func exp()
    change classname to be of type string (so include string.h)
    and in the switch for the choosing of the class change from
    stat={"Warrior", 30, 30, 5, 5, 20, 15};
    to
    Code:
    case 1: //warrior
    {
      stat.classname="Warrior";
      stat.maxhp=30;
      stat.hp=30;
      stat.maxmp=5;
      stat.mp=5;
      stat.attack=20;
      stat.defense=15;
    break;
    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    178
    the problem is i'm trying to keep the coding as short and sweet as possible and have seen it done in this fashion. Everything works and compiles fine except that it loses the value of the structure after it exits the chooseclass function.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    when i change to code to that it runs perfectly
    it prints
    Welcome the Warrior. What would you like to do?

    it could be my compiler but it doesn't take:
    stat={"Warrior", 30, 30, 5, 5, 20, 15};
    I thought you could only do that when you first declare a struct variable not afterwords (could be wrong though)
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    178
    I could be wrong. This is one of my first times attempting to use structures, my last game like this used global variables. I'll give it a shot though and let you know how it works out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM