This should be an easy question...

This is a discussion on This should be an easy question... within the C++ Programming forums, part of the General Programming Boards category; I think what I am looking for, I have already heard about and figured out how to use it (basically). ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    This should be an easy question...

    I think what I am looking for, I have already heard about and figured out how to use it (basically). But I have looked for a while and cannot find what it was! I have a problem with this, learning something and then not being able to remember the names. Anyway, I am thinking along the lines of a game. There can be multiple players, and each player has certain variables, which are all integers. Let's say I wanted it to give each player 3 variables. The variables could be, for example: health, strength, speed, and defense. How could I do this, so that each player had their own variables, without putting them out long in an int list?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could use a struct.

    quick example:

    Code:
    struct player {
      int health;
      int strength;
      int speed;
    };
    i think that's a way to do what your looking for.

    and in main, just declare how many players you want, i.e.

    Code:
    player player1, player2;
    
    //to access each player's characteristic
    
    player1.health = 2;  //for example
    player2.speed = 3;
    hope this helps some.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    This is exactly....

    This is exactly it...Thanks for the help

    But!
    I run into a problem. It says "player1 was not declared in this scope. Did I misunderstand you when you said to put it as:
    Code:
    int main (player1, player2, etc.)
    or am I doing something wrong? Man I am stupid
    Last edited by Razorblade Kiss; 01-07-2005 at 07:37 PM. Reason: I need more help...

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    declare it as you would any other variable in main. the parameter list for main is reserved for special parameters.

    i.e.
    Code:
    int main() {
      player player1, player2;
    
     // etc. etc.
    }
    granted the struct is declared within scope for the program (essentially, and I'm sure you probably know this, that the struct is above main)

    edit: No, you're not stupid.

  5. #5
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    You don't want to do that.

    You just want to do something like this :

    Code:
    struct player
    {
      health;
      strength;
      magic;
    };
    
    int main()
    {
      player player1, player2, player3
      player1.health = 40
      player1.strength = 30
      player2.health = 40
      etc.;
    }
    Edit : And again, I'm beaten to the punch line.

    To the original poster, what are you using to learn C++? I would've thought you'd learned how to use structures if you're making a game.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Where i learn....

    I have been learning from this site, but I have not been doing very much tutorial work lately. Instead, I constantly experiment with stuff I find in the boards here. Once I have covered the main parts of the item I wanted to learn, I am happy so I go to learn something different. After I learned what int, char, cout, and simple things like that were, I learned myself by experimenting with commands, refering to the site for help. Thanks for the help, but I have yet another question. Call me stupid, but I have recently realized that the cout command is simply c out(put). To get input, the c in(put) command is used (at a basic level). What is the c for? why not just in / out?

    and as for edit, I am not stupid, I am ignorant of the topic

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Quote Originally Posted by Razorblade Kiss
    What is the c for? why not just in / out?
    The c stands for console, since the programs its used in are console based.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    makes sense....

    makes sense...now for more help
    I try to do this and I get 76 errors in my program!!It works fine until I try to implement this stuff, so could someone offer completely working syntax proof copy and paste and it will work code? (P.S. I warned you I was stupid )

  9. #9
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Well, if you're having problems with stuff you implement, just post the code here, and we'll be glad to help!

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    wow....this is going to sound really really stupid, but my code is really jumbled. So instead of posting 10 million lines of code(actually morelike 95) I started to make a new program with just the basic problem I was having. Using the breif and sweet(also simple)code, I figured out all of the errors. Mostly semicolons, I figured it out after I thought about it. You guys have been a great help.

  11. #11
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Why use structures one of the advantages C++ has over C is you can use Functions in Classes.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use them in structures also, so what's your point? There's a saying: Keep it simple, stupid.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    that is it

    This is making me so mad....I just pulled hair out of my head. I make a simple program, it works perfectly. I throw the simple program in front of my program(no I didnt have 2 int main's or something like that!) and it gives me over 80 errors! I am going to go to sleep and try it later. I dont want to wake up bald tommorrow. By the way, my hair is about 3/4 of an inch long, so you figure how hard it was to pull it out. Talk to you guys some other day...

    edit:this isnt an edit, just an addition...

    I feel like im on experimental prescription drugs. I'm pullin my hair out, then I'm happy it works. Somebody shoot me with a tranquilizer.
    Last edited by Razorblade Kiss; 01-07-2005 at 09:15 PM.

  14. #14
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    I don't understand how you can have that many errors. Don't you check for errors as you go along?

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    so many errors

    Well, I had an error with the syntax of the struct, so it carried it as a syntax error for every >>, <<, etc. Since I used a lot of these, I had plenty of errors. Once I cleared up the starting error, it all went smooth.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 03:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 12:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21