How to define elements of a Struct in the Header File? - c++

I´am new at C++, so heads up.
I want to make a Blackjack Game and have a Struct with two Elements, name and points for the Cards. Than i have an other Struct with the Type of the first Struct. So now my first Question how can i better define the name and points than what i did down there. And Second Question how can i randomly get cards from an array of the structs. I hope anyone gets my Problem :)
struct CardInfos
{
string name;
int points;
};
struct cards
{
CardInfos zwei;
CardInfos drei;
CardInfos vier;
CardInfos fuenf;
CardInfos sechs;
CardInfos sieben;
CardInfos acht;
CardInfos neun;
CardInfos Bube;
CardInfos Königin;
CardInfos König;
CardInfos Ass;
};
Defined in a function in cpp....Better way?
cardIn.zwei.name = "Zwei";
cardIn.zwei.points = 2;
cardIn.zwei.name = "Drei";
cardIn.zwei.points = 3;
cardIn.zwei.name = "Vier";
cardIn.zwei.points = 4;
cardIn.zwei.name = "Fuenf";
cardIn.zwei.points = 5;
cardIn.zwei.name = "Sechs";
cardIn.zwei.points = 6;
cardIn.zwei.name = "Sieben";
cardIn.zwei.points = 7;
cardIn.zwei.name = "Acht";
cardIn.zwei.points = 8;
cardIn.zwei.name = "Neun";
cardIn.zwei.points = 9;

Your cards, look more like a namespace to me, but both can work like this:
namespace cards {
CardInfos zwei{"zwei", 2};
CardInfos drei;//and so on
CardInfos vier;
CardInfos fuenf;
CardInfos sechs;
CardInfos sieben;
CardInfos acht;
CardInfos neun;
CardInfos Bube;
CardInfos Königin;
CardInfos König;
CardInfos Ass;
};

Related

C++ compiler error when running code (seg fault core dumped)

#include <iostream>
using namespace std;
/*
Animal cheapest(string type, Animal a[], int size) that returns the
cheapest animal in a of type type (that is, the cheapest cat or dog).
Note: Assuming you’ve
filled in an array of Animals called shelter, calling cheapest from your
main function would
look like
Animal inexpensive = cheapest(“Dog”, shelter, 20);
*/
struct Animal{
string name = "";
string gender = "";
int age = 0;
int price = 0;
string catOrDog = "";
};
Animal cheapest(string type, Animal a[], int size){
int smallest = a[0].price;
int indexOfSmallest = 0;
for(int i = 0; i<size; i++){
if(a[i].price < smallest && a[i].catOrDog == type){
indexOfSmallest = i;
}
}
return a[indexOfSmallest];
}
void printAnimal(Animal name){
cout<< name.name <<endl;
}
int main(){
Animal arr[1];
Animal one;
one.name = "Chad";
one.gender = "Female";
one.age = 2;
one.price = 1500;
one.catOrDog = "Dog";
Animal two;
two.name = "Brian";
two.gender = "Female";
two.age = 2;
two.price = 1000;
two.catOrDog = "Dog";
arr[0] = one;
arr[1] = two;
Animal inexpensive = cheapest("Dog", arr, 2);
printAnimal(inexpensive);
return 0;
}
Segmentation Error when running code. From what I've googled, this usually happens when you are reading from a file but I am not reading from a file.
What is wrong with my code? This is the first time I've encountered a problem like this so I am completely blank
You declare an array with one element:
Animal arr[1];
Then you access it out of bounds by storing two elements into it:
arr[0] = one;
arr[1] = two;
Only the first index (0) is valid.
You should consider using a std::array or std::vector rather than a C-style array, so you don't have to pass its size separately.

How to delete an element from a struct array in c++

lets say i have a structure like this:
struct customer{
int c1;
int c2;
int c3;
};
customer phone[10];
phone[0].c1 = 1;
phone[0].c2 = 1;
phone[0].c3 = 1;
phone[1].c1 = 2;
phone[1].c2 = 2;
phone[1].c3 = 2;
so my question is how to remove phone[1] from the struct array?
thanks in advance.
The best you can do is to overwrite it.
This is a built-in array. So the other elements are not initialized. In the same way, once you write something to one of the elements it stays there until the array goes out of scope or you write something else there.
It would be best to do the same with a std::vector:
#include <iostream>
#include <vector>
int main()
{
struct customer {
int c1;
int c2;
int c3;
};
customer phone[10];
phone[0].c1 = 1;
phone[0].c2 = 1;
phone[0].c3 = 1;
phone[1].c1 = 2;
phone[1].c2 = 2;
phone[1].c3 = 2;
std::vector<customer> phonev{{1,1,1},{2,2,2}};
phonev.erase(phonev.begin()+1);
return 0;
}
Then you can erase an element.

Adding a struct into an array [closed]

So lets say I have a struct like this:
struct example_structure
{
int thing_one;
int thing_two;
};
I also have an empty array which I am trying to fill with these structs. I am trying to add them as follows, but it doesn't seem to be working:
array[i].thing_one = x;
array[i].thing_two = y;
Instead of this is there a way to declare a variable of type example_structure and then add that to the array?
Use vectors. They can expand as needed.
#include <iostream>
#include <vector>
int main()
{
struct example_structure
{
int thing_one;
int thing_two;
};
std::vector<example_structure> data;
for (int i = 0; i < 3; i++)
{
data.push_back({i, i * 2});
}
for (const auto& x : data)
{
std::cout << x.thing_one << " " << x.thing_two << "\n";
}
}
Live example:
http://ideone.com/k56tcQ
You can write simply
array[i] = { x, y };
Or you can have a separate variable of the type of the structure. For example
struct example_structure obj = { x, y };
array[i] = obj;

Questions on classes and implemention

So I was trying implement a particular class with this declaration:
class Student
{
public:
Student ();
Student (int, int);
~Student ();
void setMod (int, int);
void setId (int);
void setScore (int);
int getId () const;
int getScore () const;
void print () const;
private:
int idNo, score, mod;
};
Student::Student ()
{
idNo = -999;
score = -999;
}
Student::Student (int idNo, int score)
{
this -> idNo = idNo;
this -> score = score;
}
Student::~Student ()
{
static int i = 0;
}
void Student::setMod (int idNo, int size)
{
this -> mod = idNo % size;
}
void Student::setId (int idNo)
{
this -> idNo = idNo;
}
void Student::setScore (int score)
{
this -> score = score;
}
int Student::getId () const
{
return idNo;
}
int Student::getScore () const
{
return score;
}
void Student::print () const
{
cout << idNo << "\t\t" << mod << "\t" << score << endl;
}
And then I have problem with these implementation:
1.
if (table [k].getId () == -999)
{
table [k].setId(idNo);
table [k].setScore(score);
table [k].setMod(idNo, score);
}
2.
Student *table;
int tSize = 20;
table = new Student [tSize];
for (int i = 0; i < tSize; i++)
table [i] (-999, -999);
My problems are:
at 1, I got error: request for member 'method', which is of non-class type 'int'.
at 2, I got error: no match to call (class) (int, int)
can someone help me understand what I'm doing wrong here? Noted that I haven't covered vector yet, so I'm forced to use an array of objects.
The second error is related to this line:
table [i] (-999, -999);
You call Student::operator()(int, int) which does not exist.
I suppose you want to assign a new Student to the array element at i. This could be achieved by
table [i] = Student(-999, -999);
But this would lead to runtime errors because table is not initialized. I would suggest to use an std::vector instead of a dynamic array. Your whole table initialization would look like this:
std::vector<Student> table(20, Student(-999, -999));
Another advantage is that you don't have to worry about freeing the resources (delete[] table; is not necessary)
This is a working main that uses the Student class that you've defined:
int main() {
int totNumStudent = 20;
Student* table = new Student[totNumStudent];
// Your table contains all the students initialized using the constructor
// with no-parameters
for(int i = 0; i < totNumStudent; i++)
std::cout << table[i].getId() << std::endl;
delete [] table; // deallocate the memory for the table
}
I think that you need to read a well-written book for C++. There are tons of them.
I feel to suggest to you Accelerated C++ which is pretty simple and let you to understand the basic C++'s topics.
Change the second example to
#include <vector>
...
std::vector<Student> table;
int tSize = 20;
for (int i = 0; i < tSize; i++)
table.push_back( Student(-999, -999));
or, if you can't use vector:
int tSize = 20;
Student *table = new Student[tSize];
for (int i = 0; i < tSize; i++)
table[i] = Student(-999, -999);
...
delete[] table;
The first one looks OK, if table is declared as in the second.

pointers to a multidimensional array in classes

I'm trying to get to the bottom of an error, I wonder if you could help please? I'm having a bit of an issue with array pointers at the moment. I have three classes, one parent, and two children so to speak. One of the children has a 2D array of type struct, and I'm trying to access the elements of this from the other child.
I was wondering, is this code valid with the correct format/syntax for my array pointers?
OChild1 creates and fills out the 2D array, I'm saving a pointer to that in Parent, and passing that pointer to OChild2, and then plan on using the contents of the array for further processing.
struct BOARDTILE
{
float fPosX;
float fPosY;
BOARDTILES()
{
fPosX = 0.0f;
fPosY = 0.0f;
}
};
class CChild1
{
public:
BOARDTILE BoardTileArray[18][18];
CChild1()
{
}
writeBoardTileArray()
{
for (int i = 0; i <= 17; i++)
{
for (int j = 0; j <= 17; j++)
{
BoardTileArray[i][j].fPosX = (float) i * 5.0f;
BoardTileArray[i][j].fPosY = (float) j * 7.0f;
}
}
}
};
class CChild2
{
public:
BOARDTILE (*pBoardTileArray)[18][18];
float fPosX;
float fPosY;
CChild2()
{
}
void readBoardTileArray()
{
for (int i = 0; i <= 17; i++)
{
for (int j = 0; j <= 17; j++)
{
fPosX = (*pBoardTileArray[i][j]).fPosX;
fPosY = (*pBoardTileArray[i][j]).fPosY;
cout << fPosX;
cout << fPosY;
}
}
}
};
class CParent
{
public:
BOARDTILE (*pBoardTileArray)[18][18];
CChild1 OChild1;
CChild2 OChild2;
CParent()
{
OChild1.writeBoardTileArray();
pBoardTileArray = &(OChild1.BoardTileArray);
OChild2.pBoardTileArray = pBoardTileArray;
}
};
To store address of a two dimensional array, you would need a pointer to pointer,
Without typedef:
You would declare a member pointer as:
BOARDTILE (*pBoardTileArray)[18];
and then in the constructor, you would write:
pBoardTileArray = OChild1.BoardTileArray;
But as you can see it makes the code complex. Using typedef can simplify it:
With typedef: (Recommended way)
With typedef you can write the following in the global scope:
typedef BOARDTILE BOARDTILE_ar18_t[18];
and then you can declare member pointer as:
BOARDTILE_ar18_t* pBoardTileArray;

Resources