Create sequential variable names using a loop in C++ - c++

I'm trying to create variable names using a loop.
Specifically, I am using this structure:
struct card{
string rank;
string suit;
};
This is the rest of my code as it stands, and where it says "card+i" is where I need it to say "card1", or "card2", etc.
string aSuit[4] = {" Hearts"," Clubs"," Diamonds"," Spades"};
string aRank[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
string aDeck[52];
int main(int argc, char *argv[])
{
int i = 0;
for (int s=0; s<4; s++) {
for (int r=0; r<13; r++) {
card card+i;
card+i.rank = aRank[r];
card+i.suit = aSuit[s];
cout << card + i.rank << card + i.suit << endl;
i++;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Use arrays instead:
card cards[52];
int main(int argc, char *argv[])
{
int i = 0;
for (int s = 0; s<4; s++) {
for (int r = 0; r<13; r++) {
cards[i].rank = aRank[r];
cards[i].suit = aSuit[s];
cout << cards[i].rank << cards[i].suit << endl;
i++;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

You think this is the best solution, but I can assure you, it is not. Tying your logic to the names of variables is a bad, bad idea, from a logical as well as maintenance standpoibnt. What you really want is a collection which can associate one piece of data (in this case, a string) with another.
Look into a data structure like a map

This isn't possible. When you compile a C++ program, any concept of variable names completely disappears. It can't generate variable names at run time because they just don't exist.
However, there is a way to do this, of course. This is precisely what arrays are for. They give you a whole set of objects that you can index by number (i, in your case). You already have a variable named aDeck but I think you've defined it incorrectly. Did you perhaps want:
card aDeck[52];
Now you'll have a deck of 52 cards. Each card in this deck has suit and rank members. You can access each member of the array with aDeck[i]. So change the inside of your loop to:
aDeck[i].rank = aRank[r];
aDeck[i].suit = aSuit[s];

You need to use arrays for this, they allow a single variable to contain a group of items.
Then you simply use the variable name with an index, such as:
int xyzzy[5];
for (int i = 0; i < 5; i++)
xyzzy[i] = i * 7;
// gives 0, 7, 14, 21, 28
In fact, your code already has arrays representing the suits and card face values, not really that different from what you need here, so I'm not certain why you didn't make the logic-leap to using them for the cards as well. But that's basically how you'd do it.

Related

Initializing multidimensional dynamical array in c++

I'm having problems declaring a multidimensional dynamical array in c style. I want to declare dynamically an array like permutazioni[variable][2][10], the code i'm using is as following (carte is a class i defined):
#include "carte.h"
//other code that works
int valide;
carte *** permutazioni=new carte**[valide];
for (int i=0; i<valide; i++){
permutazioni[i]=new carte*[2];
for (int j=0; j<2; j++) permutazioni[i][j]=new carte[10];
}
the problem is, whenever i take valide=2 or less than 2, the code just stops inside the last for (int i=0; i<valide; i++) iteration, but if i take valide=3 it runs clear without any problem. There's no problem as well if i declare the array permutazioni[variable][10][2] with the same code and any value of valide. I really have no clue on what the problem could be and why it works differently when using the two different 3d array i mentioned before
You show a 3D array declared as permutazioni[variable][10][2] but when you tried to dynamical allocate that you switched the last two dimensions.
You can do something like this:
#include <iostream>
#define NVAL 3
#define DIM_2 10 // use some more meaningfull name
#define DIM_3 2
// assuming something like
struct Card {
int suit;
int val;
};
int main() {
// You are comparing a 3D array declared like this:
Card permutations[NVAL][DIM_2][DIM_3];
// with a dynamical allocated one
int valid = NVAL;
Card ***perm = new Card**[valid];
// congrats, you are a 3 star programmer and you are about to become a 4...
for ( int i = 0; i < valid; i++ ){
perm[i] = new Card*[DIM_2];
// you inverted this ^^^ dimension with the inner one
for (int j = 0; j < DIM_2; j++)
// same value ^^^^^
perm[i][j] = new Card[DIM_3];
// inner dimension ^^^^^
}
// don't forget to initialize the data and to delete them
return 0;
}
A live example here.
Apart from that it is always a good idea to check the boundaries of the inddecs used to access to the elements of the array.
How about using this syntax? Haven't tested fully with 3 dimensional arrays, but I usually use this style for 2 dimensional arrays.
int variable = 30;
int (*three_dimension_array)[2][10] = new int[variable][2][10];
for(int c = 0; c < variable; c++) {
for(int x = 0; x < 2; x++) {
for(int i = 0; i < 10; i++) {
three_dimension_array[c][x][i] = i * x * c;
}
}
}
delete [] three_dimension_array;
Obviously this could be c++ 11/14 improved. Could be worth a shot.

Input from keyboard into a 2d dynamic array

So i'm trying to write a function which would get input from keyboard and store it in the 2d dynamic array. n is the number of lines (tried with 1-4 lines), m is the number of characters per line (256 in my case). I've read plenty about dynamic arrays and the use of new and the code seems totaly fine to me, but i keep getting this error when i try to enter the text: Access violation reading location 0x00000000. Can't figure out why. Please help.
void KeyInput (char **string, unsigned int n, unsigned int m)
{
cout<<endl<<"Input from keyboard"<<endl;
string=new char* [n];
for(unsigned int i = 0; i < n; i++ )
string[i]=new char[m];
for(unsigned int i = 0; i < n; i++ )
gets(string[i]);
}
can you give more information on where you are getting the access violation? I tried the following code (Visual Studio 2010, Window 7 Professional) and did not get an error. Note that I did change the characters per line to 15 instead of 255 as I wanted to test boundary conditions without a lot of typing.
Your function seems to work fine on my machine, however you do have a latent buffer-overflow using gets as it does not check for the length of the string. Remember that gets will append a null-terminator for you, so if in your case you enter exactly 255 characters you will overflow your buffer by one.
void KeyInput(char** string, unsigned int n, unsigned int m);
int _tmain(int argc, _TCHAR* argv[])
{
char* strArray;
KeyInput(&strArray, 4, 15);
return 0;
}
void KeyInput(char** string, unsigned int n, unsigned int m)
{
string = new char*[n];
for(unsigned int i = 0; i < n; i++)
{
string[i] = new char[m];
}
for(unsigned int i = 0; i < n; i++)
{
gets(string[i]);
}
}
(also ignore the hideous _tmain and _TCHAR stuff, they are Windows idiosyncrasies :) ).
Finally, unless this is an assignment (or an exercise for self learning), do what 40two suggested and use STL to make your life easy.
Use a vector of strings, take advantage of the force that STL has (use the force Luke see code below how):
void KeyInput (std::vector<std::string>& str_vec, int const n)
{
std::cout << "\nInput from keyboard" << std::endl;
for (auto i = 0; i < n; i++) {
std::string tmp;
std::getline(std::cin, tmp);
str_vec.push_back(tmp);
}
}
Update or Why your C++ teachers are wrong:
void KeyInput(char ***string, unsigned int n, unsigned int m)
{
std::cout << "\nInput from keyboard" << std::endl;
*string = new char*[n];
for (unsigned int i = 0; i < n; i++)
(*string)[i] = new char[m];
for (unsigned int i = 0; i < n; i++)
std::gets((*string)[i]);
}
int main()
{
char **string = 0;
KeyInput(&string, 4, 100);
for (auto i = 0; i < 4; ++i) std::cout << string[i] << std::endl;
return 0;
}
You need triple pointers in order to pass the 2d array by reference and to be properly filled (OMG!!!).
The user can enter only limited length strings (e.g., 99) don't forget strings have one character at the end (i.e., '/0' the null character).
You have to take care of the memory allocated and deleted later in order to avoid memory leaks.
If you want to shoot your self in the foot continue to program like this.

Can I have a two dimensional array of different types?

// create array
Date** dateList = new Date*[SIZE];
// populate array
for(int i = 0; i < SIZE; i++) {
dateList[i] = new Date[2];
Date date;
date.input();
dateList[i][0] = date;
int n = dateNum(date);
dateList[i][1] = n;
}
I want to store a object of Date type in dateList[i][0] and an int in dateList[i][1]. Is this possible?
Thank you.
No.
A single array must be of a single type. If you want the elements to represent different types of data, then the element in the array needs to be some kind of base class (pointer), or possibly a discriminated type like a variant.
Unless you have an overwhelming reason to do otherwise, it sounds like you want a vector of structs:
struct whatever {
Date date;
int n;
whatever(Date const &d, int i) : date(d), n(i) {}
};
std::vector<whatever> dates;
Date date;
date.input();
dates.push_back(whatever(date, datenum(date));
Short answer: No. Arrays are of one type only. That is also true for multidimensional arrays.
Read about structs or even higher data structures like map to achieve the dessired effect.
There are multiple ways of doing it. The most direct one is to use unions: put the types that you want to share in a single union, then use the member corresponding to the dimension's type in your code. However, this approach is also the most restrictive, because types with constructors / destructors cannot go into unions.
A more convenient way to do this is to build a one-dimensional array of pair<Date,int> objects, like this:
pair<Date,int>* dateList = new pair<Date,int>[SIZE];
// populate array
for(int i = 0; i < SIZE; i++) {
Date date;
date.input();
int n = dateNum(date);
dateList[i] = make_pair(date, n);
}
for(int i = 0; i < SIZE; i++) {
cout << "Date: " date[i].first << " ";
cout << "Int: " date[i].second << endl;
}
This approach lets the compiler check your types much closer, giving you a more robust solution.

C++ iterating a struct

Is it possible to iterate through a struct?
For example
struct team{
int player1;
int player2;
int player3;
int player4;
...
int player99;
int size = 99;
}
then run a for loop to set or access foo 1-4?
i guess pseudocode would look something like
for(int i = 0; i < size; i++){
player i = (i+1);
}
A more simplified explanation if that doesnt make sense is I Just want to be able to go through each variable without having to hard code player1 = 1; player2 =2.
One way is to put the players/elements into an array:
struct Team {
static int const size = 99;
int players[size];
};
And then:
for(int i = 0; i < size; ++i)
int player = players[i];
To answer your question as you've asked it, I believe that you can use the pre-compiler macro Pack (the exact phrase depends on your compiler) to guarantee the structure of the memory used to create an instance of your struct. And then you technically could increment a pointer to move through it... if you're mad. That would be a very poor way to do and not at all guaranteed to work on different compilers or even different days of the week. No what you want is a data structure to do the job for you; they come with a 100% cash-back guarantee!
The most basic structure to do this with is a fixed size array, e.g:
struct team
{
int players[99]; //an array
int manager;
int coach;
string teamName;
//etc etc
}
Then to access your players
team myTeam;
for(int i(0); i < 99; ++i)
{
myTeam.players[i]; //do whatever
}
The limitation of an array is that you cannot change its size once it's created.
So if you try
myTeam.players[99]; //accessing invalid memory - the array values are 0 - 98
More advanced
If you need a data structure that can change size after it's created, e.g you might want to add a few more players to your team at some point in the future. Then you can use a dynamic data structure such as the std::vector or the std::deque or std::list
I would propose to use container instead of many variables, for example you could use std::array or std::vector. This way it will be trivial to iterate, much easier to make a copy. But also it's better from design point of view: in case you decide to change the number of players it will be much easier to change the container rather than add/remove many fields
You can define pointer to member, like pointer to member function:
typedef int team::*member_t;
You can have array of pointers to all your members:
static member_t member[size];
With this approach defining member function to iterate over all members is easy:
template <class F>
void for_each(F f)
{
for (int i = 0; i < size; ++i)
f(this->*member[i]);
}
And with using of preprocessor macro - you can have in one place definition of all members, in other definition of pointer to members - so you will not make any mistake with changing their order. See full code:
struct team {
#define TEAM_MEMBERS(prefix,suffix) \
prefix player1 suffix, \
prefix player2 suffix, \
prefix player3 suffix
int TEAM_MEMBERS(,);
static const int size = 3;
typedef int team::*member_t;
static member_t member[size];
template <class F>
void for_each(F f)
{
for (int i = 0; i < size; ++i)
f(this->*member[i]);
}
};
team::member_t team::member[team::size] = {
TEAM_MEMBERS(&team::,)
};
And some test:
#include <iostream>
int main() {
team t = { 0 };
t.for_each([](int m) { std::cout << m << "\n"; }); // prints 0,0,0
int n = 0;
t.for_each([&n](int& m) { m = n++; });
t.for_each([](int m) { std::cout << m << "\n"; }); // prints 0,1,2
t.player2 = 7;
t.for_each([](int m) { std::cout << m << "\n"; }); // prints 0,7,2
}

3D array C++ using int [] operator

I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this
It's supposed to be a 3D dynamic array using pointers.
I started like this, but got stuck there
int x=5,y=4,z=3;
int ***sec=new int **[x];
It would be enough to know how to make it for a static size of y and z;
Please, I'd appreciate that you help me.
Thanks in advance.
To create dynamically 3D array of integers, it's better you understand 1D and 2D array first.
1D array: You can do this very easily by
const int MAX_SIZE=128;
int *arr1D = new int[MAX_SIZE];
Here, we are creating an int-pointer which will point to a chunk of memory where integers can be stored.
2D array: You may use the solution of above 1D array to create a 2D array. First, create a pointer which should point to a memory block where only other integer pointers are held which ultimately point to actual data. Since our first pointer points to an array of pointers so this will be called as pointer-to-pointer (double pointer).
const int HEIGHT=20;
const int WIDTH=20;
int **arr2D = new int*[WIDTH]; //create an array of int pointers (int*), that will point to
//data as described in 1D array.
for(int i = 0;i < WIDTH; i++){
arr2D[i] = new int[HEIGHT];
}
3D Array: This is what you want to do. Here you may try both the scheme used in above two cases. Apply the same logic as 2D array. Diagram in question explains all. The first array will be pointer-to-pointer-to-pointer (int*** - since it points to double pointers). The solution is as below:
const int X=20;
const int Y=20;
const int z=20;
int ***arr3D = new int**[X];
for(int i =0; i<X; i++){
arr3D[i] = new int*[Y];
for(int j =0; j<Y; j++){
arr3D[i][j] = new int[Z];
for(int k = 0; k<Z;k++){
arr3D[i][j][k] = 0;
}
}
}
// one-liner
typedef std::vector<std::vector<std::vector<int> > > ThreeDimensions;
// expanded
typedef std::vector<int> OneDimension;
typedef std::vector<OneDimension> TwoDimensions;
typedef std::vector<TwoDimension> ThreeDimensions;
(this is tagged c++, after all)
EDIT in response to Joe's question
hello again Joe =) sure. here's the example:
#include <vector>
#include <iostream>
int main(int argc, char* const argv[]) {
/* one-liner */
typedef std::vector<std::vector<std::vector<int> > >ThreeDimensions;
/* expanded */
typedef std::vector<int>OneDimension;
typedef std::vector<OneDimension>TwoDimensions;
typedef std::vector<TwoDimensions>ThreeDimensions;
/*
create 3 * 10 * 25 array filled with '12'
*/
const size_t NElements1(25);
const size_t NElements2(10);
const size_t NElements3(3);
const int InitialValueForAllEntries(12);
ThreeDimensions three_dim(NElements3, TwoDimensions(NElements2, OneDimension(NElements1, InitialValueForAllEntries)));
/* the easiest way to assign a value is to use the subscript operator */
three_dim[0][0][0] = 11;
/* now read the value: */
std::cout << "It should be 11: " << three_dim[0][0][0] << "\n";
/* every other value should be 12: */
std::cout << "It should be 12: " << three_dim[0][1][0] << "\n";
/* get a reference to a 2d vector: */
TwoDimensions& two_dim(three_dim[1]);
/* assignment */
two_dim[2][4] = -1;
/* read it: */
std::cout << "It should be -1: " << two_dim[2][4] << "\n";
/* get a reference to a 1d vector: */
OneDimension& one_dim(two_dim[2]);
/* read it (this is two_dim[2][4], aka three_dim[1][2][4]): */
std::cout << "It should be -1: " << one_dim[4] << "\n";
/* you can also use at(size_t): */
std::cout << "It should be 12: " << one_dim.at(5) << "\n";
return 0;
}
You can try:
for(int i=0;i<x;i++) {
sec[i] = new int *[y];
for(int j=0;j<y;j++) {
sec[i][j] = new int [z];
}
}
And once you are done using this memory you can deallocate it as:
for(int i=0;i<x;i++) {
for(int j=0;j<y;j++) {
delete [] sec[i][j];
}
delete [] sec[i];
}
delete [] sec;
Comprehensive answers.
If you are really writing this in C++ (not rough C) I think you should take another look at this complicated data structure. IMO redesign while keeping in mind what you are trying to do would be better.
What you're trying to do is not idiomatic in C++. Of course, you can use a int***pointer for this, but this is strongly discouraged. In C++ we have better ways to get there.
vector<vector<vector<int> > > foo (5,vector<vector<int> >(4, vector<int>(3)));
This will result in something with the memory layout similar to what you asked for. It supports dynamic resizing and inner vectors to have different sizes just like in your picture. In addition, you don't have to worry about manual allocation / deletion of any of it. Also, the vectors know their size so you don't have to remember it somewhere.
But if you just want a "rectangular" 3D array where all the elements are consecutivly stored in the same memory block, you could use a boost::multiarray.
OK let us take your beginnings
int ***sec = new int**[x];
sec is now an array of int**s of length x, so now I am just going to focus on making the zeroeth element be what you want
sec[0] = new int*[y];
Now sec[0] points to array of int*s of length y, now just need to get the last bit of the tree done, so
sec[0][0] = new int[z];
And finally to get it to the form in your diagram
sec[0][0][z-1] = 0;
This does seem a little like a homework question, make sure you actually understand the answer and why it works.
If it's the actual arrays you'r having problems with look here: Declaring a pointer to multidimensional array and allocating the array
Not sure exactly what you want but you might want to read up on about linked lists.