Only part of if statement working

So I made a program that's like a little poker game. 5 cards are dealt, user picks which of those cards they want to replace with a new one, and then the program says what the hand is. My program works fine except for the part where I ask to swap cards. When a new card is given, the face value of the card(2,3,4...jack, queen, etc.) is randomly generated just fine but the suit of the card(hearts, spades,clubs,diamonds) stays the same even though I am using the same method. The problem is obviously with my if statement so any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
string newcardval1, newcardval2, newcardval3, newcardval4, newcardval5;
	string newcardsuit1, newcardsuit2, newcardsuit3, newcardsuit4, newcardsuit5;
	int a,b,c,d,e;
	cout<<"Swap card 1? yes=1 no=2 ";
	cin>>a;
	cout<<endl;
	if(a==1){
		newcardval1=cardval[rand()%13];
		newcardsuit1=suit[rand()%4];}
	else if(a==2)
		newcardval1=card1val;
		newcardsuit1=card1suit;
	cout<<"Swap card 2? yes=1 no=2 ";
	cin>>b;
	cout<<endl;
	if(b==1){
		newcardval2=cardval[rand()%13];
		newcardsuit2=suit[rand()%4];}
	else if(b==2)
		newcardval2=card2val;
		newcardsuit2=card2suit;
	cout<<"Swap card 3? yes=1 no=2 ";
	cin>>c;
	cout<<endl;
	if(c==1){
		newcardval3=cardval[rand()%13];
		newcardsuit3=suit[rand()%4];}
	else if(c==2)
		newcardval3=card3val;
		newcardsuit3=card3suit;
	cout<<"Swap card 4? yes=1 no=2 ";
	cin>>d;
	cout<<endl;
	if(d==1){
		newcardval4=cardval[rand()%13];
		newcardsuit4=suit[rand()%4];}
	else if(d==2)
		newcardval4=card4val;
		newcardsuit4=card4suit;
	cout<<"Swap card 5? yes=1 no=2 ";
	cin>>e;
	cout<<endl;
	if(e=1){
		newcardval5=cardval[rand()%13];
		newcardsuit5=suit[rand()%4];}
	else 
		newcardval5=card5val;
		newcardsuit5=card5suit;
	cout<<"New Hand:"<<endl;
	cout<<newcardval1<<" "<<newcardsuit1<<endl;
	cout<<newcardval2<<" "<<newcardsuit2<<endl;
	cout<<newcardval3<<" "<<newcardsuit3<<endl;
	cout<<newcardval4<<" "<<newcardsuit4<<endl;
	cout<<newcardval5<<" "<<newcardsuit5<<endl;
too add to this, the program can decipher if the user wants to swap cards or not just fine, but when it does swap cards, a new suit isnt generated
You forgot to place braces. For example here

1
2
3
	else if(a==2)
		newcardval1=card1val;
		newcardsuit1=card1suit;
typo? that is what I have. I messed with doing the { brackets on both parts of the statements and that didnt help.

I just noticed that I forgot to do the last one also, I forgot to change it back when I was trying something
Vlad was giving you an example of a place you're missing braces, not fixing it for you. You're missing braces on else if (a==2), else if (b==2), else if (c==2), else if (d==2), and else. Put braces around all these code blocks and you should be fine.
yea it worked, thought I tried that. Thanks for the help
Also, this:

if(e=1){

should be this:

if(e == 1){

Use of the equality operator, not assignment.

I format my braces like this:

1
2
3
4
5
6
7
8
if(e == 1) {
	newcardval5=cardval[rand()%13];
	newcardsuit5=suit[rand()%4];
}
else {
	newcardval5=card5val;
	newcardsuit5=card5suit;
}


This makes it easier to see the closing brace.

The swapping of cards should be a function that takes 2 cards as arguments, that you can call, rather than repeating your code multiple times.

Hope all goes well.

Topic archived. No new replies allowed.