How would I indicate the result?

May 13, 2011 at 5:50am
# include<iostream>

using namespace std;

int main ()

{
int card1,card2, card3, result;

cout<<"Enter the value of the first card: ";
cin>>card1;
cout<<"Enter the value of the second card: ";
cin>>card2;
cout<<"Enter the value of the third card: ";
cin>>card3;
cout<<"Your score is: ";

if (card1==1 && card2==1)
cout<<23;

else if (card1==1)
cout<<11+ card1 + card2;

else if (card2==1)
cout<<card1+ 11 + card3;

else if (card3==1)
cout<<card1+ card2+ 11;

else if (result <= 21)
cout<<"Sorry you lose!"<<endl;
else if (result > 21)
cout<<"Congratulations you win!"<<endl;

return 0;
}



How would I indicate the result?
May 13, 2011 at 7:30am
the result variable is being used without being initialized
May 13, 2011 at 5:47pm
You need to assign something to result, it will always be empty, until you put something in it. So you will have to change the last 2 'else if' statements, and you could do the calculations "11+card1+card2" into a different int, instead of in the cout function so something like this:

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
55
56
57
58
59
60
61
# include<iostream>

using namespace std;

int main ()
{
	int card1,card2, card3, add0, add1, add2, add3, result;

	cout<<"Enter the value of the first card: ";
	cin>>card1;
	cout<<"Enter the value of the second card: ";
	cin>>card2;
	cout<<"Enter the value of the third card: ";
	cin>>card3;
	cout<<"Your score is: ";

	/* I added all the add0-3 into the if statements, and then made result equal to the
         * add integers the addition of all the card1-3 and numbers you were adding inside
         * the cout function have now been done in the add0-3 integers, so that the result
         * can go into the result integer.
	 */

	if (card1==1 && card2==1)
	{
		add0=23;
		cout<<23;
		result=add0;
	}	

	else if (card1==1)
	{	
		add1=11+card1+card2;
		cout<<add1;
		result=add1;
	}

	else if (card2==1)
	{
		add2=card1+11+card3;
		cout<<add2;
		result=add2;
	}

	else if (card3==1)
	{	
		add3=card1+card2+11;
		cout<<add3;
		result=add3;
	}

	if (result <= 21) // I changed this to an if statement, because it doesn't go along
        // with the other else if statements above
	cout<<"Sorry you lose!"<<endl;
	else if (result > 21)
	cout<<"Congratulations you win!"<<endl;
	else
	cout<<"there was an error" // I added this so it could tell you if there was a problem
        // with the above two statements.

	return 0;
}


This code is not guaranteed to work, I didn't run it, just added onto it, I haven't even compiled it either, I will in a bit and tell you if it comes out.
Last edited on May 13, 2011 at 5:50pm
Topic archived. No new replies allowed.