PAWN to C++ global variable [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do i write it in c++ ??
// in PAWN ( Procedural Language )
enum e_player_data
{
id, // Integer
username[24] // string
};
new PlayerData[50][e_player_data];
/*
so i can access it like PlayerData[1][id] = 1; or PlayerData[1][username] = "Firstname_Lastname";
*/
Can someone code it to c++ for me?

You can use a std::map:
typedef std::map<int,std::string> Player;
Player p;
// add a player:
int id = 0;
p[id] = "Max Power";

As tobi303 wrote in the comment, you can use a std::map to accomplish this.
struct e_player_data
{
int id,
std::string username
};
std::map<int,e_player_data> PlayerData;
PlayerData[1].id = 1;
PlayerData[2].username = "Firstname_Lastname";

Related

How to do I implement oop with a number of questions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?

How to override the setter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an private property in my class and I want to add some code on its setter, how can I accomplish that?
I might do that using method but I don't want to just to avoid using property.
private:
float damage;
You can replace your plain old data type with a class type to do what you want:
class Damage
{
public:
Damage& operator=(float x) { value = x; /* add code here*/ }
operator float() const { return value; }
private:
float value;
};
Then just replace your float with Damage and you can make it behave how you want.

How to choose among two classes given a condition in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose there are two classes Class1 and Class2. given a condition I have to choose among them in shortest way possible without using if-else.
Means least lines of code.
At compile time only!!!
class class1{};
class class2{};
auto data = (((condition) ? class1 : class2) *)(variable)
Assuming you need to create object at compile time depending on a variable, you can try something like following
class class1{};
class class2{};
int main( int argc, char *argv[] )
{
constexpr bool variable =true;
/* x is object of type class1 or class2 depending on
compile time constant 'variable'
*/
typedef std::conditional<variable, class1, class2>::type x;
//std::cout << typeid(x).name() << '\n';
return 0;
}
See Here

expected unqualified-id before 'public' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've searched everywhere and cant find an answer for this error
"expected unqualified-id before 'public'"
this is my code:
#include <iostream>
using namespace std;
static int playerHP;
static int playerPWR;
public:
{
static int playerHP = 100;
static int playerPWR = 4;
}
int main(){
}
p.s. This is in my main.
I don't know why you wrote this ridiculous code, but I assume you may want this:
class Player
{
public:
static int playerHP;
static int playerPWR;
};
int Player::playerHP = 100;
int Player::playerPWR = 4;
It looks like you're trying to build a class or struct. This public statement would work inside either of those, but not alone.
Try reading this class introduction and see if that makes it clearer.
If you don't want a class, because you're not trying to build a group of similar objects, then you can place these variables inside your main() - in that case, you might want to read about variable scope.

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x