program crashes

I can not figure out why this code is compiling but crashing. Any help would be awesome! I am trying to make a full deck of cards and put each card into a struct so I can use any of the info I need.

#include <iostream>

using namespace std;
string faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};


int x = 0;
int y = 0;
int z = 0;
struct buildDeck
{
int vaule;
string face;
string suite;

};

buildDeck card[52];


void Frist()
{
for(int i = 0; i<13; i++)
{
card[i].vaule = i;
card[i].face = faces[i];
card[i].suite ="HEARTS";
}
}

void second()
{
for(int j =13; j<26; j++)
{
x++;
card[j].vaule = x;
card[j].face = faces[x];
card[j].suite ="SPADES";
}
}

void thrid()
{

for(int k=26; k<39; k++)
{
y++;
card[k].vaule=y;
card[k].face = faces[y];
card[k].suite ="DIAMONDS";
}

}

void fourth()
{
for(int l=39; l<52; l++)
{
z++;
card[l].vaule=z;
card[l].face = faces[z];
card[l].suite="CLUBS";
}
}

int main()
{

Frist();
second();
thrid();
fourth();






string go = "go";
while(go !="Stop")
{


int response;
cout << "Pick a Card any card 0 - 52\n";
cin>>response;
cout<<"card value is: "<<card[response].vaule<<"\n";
cout<<"cards face is: "<<card[response].face<<"\n";
cout<<"cards suite is: "<<card[response].suite<<"\n";
}






}
it will crash because x and z are incremented before they are used. Put the increment at the end of the loop.

By the way: You will never get out of the while(go !="Stop") loop because when you enter anything but 0 - 52 it will crash and response is int not string


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
#include <iostream>

using namespace std;
string faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};


static int x = 0;
static int y = 0;
static int z = 0;
struct buildDeck
{
int vaule;
string face;
string suite;
};

buildDeck card[52];


void Frist()
{
for(int i = 0; i<13; i++)
{
card[i].vaule = i;
card[i].face = faces[i];
card[i].suite ="HEARTS";
}
}

void second()
{
for(int j =13; j<26; j++)
{

card[j].vaule = x;
card[j].face = faces[x];
card[j].suite ="SPADES";
x++;
}
}

void thrid()
{

for(int k=26; k<39; k++)
{

card[k].vaule=y;
card[k].face = faces[y];
card[k].suite ="DIAMONDS";
y++;
}

}

void fourth()
{
for(int l=39; l<52; l++)
{

card[l].vaule=z;
card[l].face = faces[z];
card[l].suite="CLUBS";
z++;
}
}

int main()
{

Frist();
second();
thrid();
fourth();

string go = "go";
while(go !="Stop")
{
int response;
cout << "Pick a Card any card 0 - 52\n";
cin>>response;
cout<<"card value is: "<<card[response].vaule<<"\n";
cout<<"cards face is: "<<card[response].face<<"\n";
cout<<"cards suite is: "<<card[response].suite<<"\n";
}
return 0;

}
Above code will complile
in first function you are starting with 0
and in others you are starting with 1
program in crashing when it reaches j=25.
And two extra lines in while loop will helps to control the flow of program

while(go !="Stop")
{
int response;
cout << "Pick a Card any card 0 - 52\n";
cin>>response;
cout<<"card value is: "<<card[response].vaule<<"\n";
cout<<"cards face is: "<<card[response].face<<"\n";
cout<<"cards suite is: "<<card[response].suite<<"\n";
cout<<"Enter go to Select next card and Stop to end"<<endl;
cin>>go;
}

#include <iostream>

using namespace std;
string faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};


static int x = 0;
static int y = 0;
static int z = 0;
struct buildDeck
{
int vaule;
string face;
string suite;
};

buildDeck card[52];


void Frist()
{
for(int i = 0; i<13; i++)
{
card[i].vaule = i;
card[i].face = faces[i];
card[i].suite ="HEARTS";
}
}

void second()
{
for(int j =13; j<26; j++)
{

card[j].vaule = x;
card[j].face = faces[x];
card[j].suite ="SPADES";
x++;
}
}

void thrid()
{

for(int k=26; k<39; k++)
{

card[k].vaule=y;
card[k].face = faces[y];
card[k].suite ="DIAMONDS";
y++;
}

}

void fourth()
{
for(int l=39; l<52; l++)
{

card[l].vaule=z;
card[l].face = faces[z];
card[l].suite="CLUBS";
z++;
}
}

int main()
{

Frist();
second();
thrid();
fourth();

string go = "go";
while(go !="Stop")
{
int response;
cout << "Pick a Card any card 0 - 52\n";
cin>>response;
cout<<"card value is: "<<card[response].vaule<<"\n";
cout<<"cards face is: "<<card[response].face<<"\n";
cout<<"cards suite is: "<<card[response].suite<<"\n";
}
return 0;

}
Above code will complile
in first function you are starting with 0
and in others you are starting with 1
program in crashing when it reaches j=25.


Thanks for your help. Now I have done this


___________________

work at http://magentopos.org/
Thanks so much for your help on this moving the counter variables to the end of the for loop did the trick. I am hoping that this can become a set-up function that is general enough to be used in all types of card games.
Topic archived. No new replies allowed.