I need to create a program to score a blackjack hand. This is how it needs to be done, i dont need loops or switches, i just need to know why I get really big negative numbers.
#include <iostream>
using namespace std;
int main()
{
int total, cards;
char c1, c2, c3, c4, c5;
total=0;
cout << "Enter the number of cards you have\n";
cin >> cards;
if ((c1>='2')&&(c1<='9'))
{
total+=c1-'0';
}
else if ((c1=='t')||(c1=='j')||(c1=='q')||(c1=='k'))
{
total+=10;
}
else if (c1=='a')
{
total+=11;
}
total=total+(c1-'0');
if ((c2>='2')&&(c2<='9'))
{
total+=c2-'0';
}
else if ((c2=='t')||(c2=='j')||(c2=='q')||(c2=='k'))
{
total+=10;
}
else if (c2=='a')
{
total+=11;
}
total=total+(c2-'0');
if ((c3>='2')&&(c3<='9'))
{
total=c3-'0';
}
else if ((c3=='t')||(c3=='j')||(c3=='q')||(c3=='k'))
{
total+=10;
}
else if (c3=='a')
{
total+=11;
}
total=total+(c3-'0');
if ((c4>='2')&&(c4<='9'))
{
total+=c4-'0';
}
else if ((c4=='t')||(c4=='j')||(c4=='q')||(c4=='k'))
{
total+=10;
}
else if (c4=='a')
{
total+=11;
}
total=total+(c4-'0');
if ((c5>='2')&&(c5<='9'))
{
total+=c5-'0';
}
else if ((c5=='t')||(c5=='j')||(c5=='q')||(c5=='k'))
{
total+=10;
}
else if (c5=='a')
{
total+=11;
}
total=total+(c5-'0');
if ((total>21)&&(c1=='a'))
{
total-=10;
}
if ((total>21)&&(c2=='a'))
{
total-=10;
}
if ((total>21)&&(c3=='a'))
{
total-=10;
}
if ((total>21)&&(c4=='a'))
{
total-=10;
}
if ((total>21)&&(c5=='a'))
{
total-=10;
}
#include <iostream>
usingnamespace std;
int main()
{
int total, cards;
char c1, c2, c3, c4, c5;
total=0;
cout << "Enter the number of cards you have\n";
cin >> cards;
cout << "Enter card values\n";
if (cards==2)
{
cin >> c1 >> c2;
}
elseif (cards==3)
{
cin >> c1 >> c2 >> c3;
}
elseif (cards==4)
{
cin >> c1 >> c2 >> c3 >> c4;
}
elseif (cards==5)
{
cin >> c1 >> c2 >> c3 >> c4 >> c5;
}
// whether you've entered 2 or 5 cards, the following code
// assumes all 5 cards contain relevant values.if ((c1>='2')&&(c1<='9'))
{
total+=c1-'0';
}
elseif ((c1=='t')||(c1=='j')||(c1=='q')||(c1=='k'))
{
total+=10;
}
elseif (c1=='a')
{
total+=11;
}
// the following line shouldn't be here.
total=total+(c1-'0');
if ((c2>='2')&&(c2<='9'))
{
total+=c2-'0';
}
elseif ((c2=='t')||(c2=='j')||(c2=='q')||(c2=='k'))
{
total+=10;
}
elseif (c2=='a')
{
total+=11;
}
// the following line shouldn't be here.
total=total+(c2-'0');
if ((c3>='2')&&(c3<='9'))
{
total=c3-'0';
}
elseif ((c3=='t')||(c3=='j')||(c3=='q')||(c3=='k'))
{
total+=10;
}
elseif (c3=='a')
{
total+=11;
}
// the following line shouldn't be here.
total=total+(c3-'0');
if ((c4>='2')&&(c4<='9'))
{
total+=c4-'0';
}
elseif ((c4=='t')||(c4=='j')||(c4=='q')||(c4=='k'))
{
total+=10;
}
elseif (c4=='a')
{
total+=11;
}
// the following line shouldn't be here.
total=total+(c4-'0');
if ((c5>='2')&&(c5<='9'))
{
total+=c5-'0';
}
elseif ((c5=='t')||(c5=='j')||(c5=='q')||(c5=='k'))
{
total+=10;
}
elseif (c5=='a')
{
total+=11;
}
//the following line shouldn't be here.
total=total+(c5-'0');
if ((total>21)&&(c1=='a'))
{
total-=10;
}
if ((total>21)&&(c2=='a'))
{
total-=10;
}
if ((total>21)&&(c3=='a'))
{
total-=10;
}
if ((total>21)&&(c4=='a'))
{
total-=10;
}
if ((total>21)&&(c5=='a'))
{
total-=10;
}
// the following line shouldn't be here.
// note that this line negates all of the
// previous updates to total, and then does
// something nonsensical to calculate the total
total = c1 + c2 + c3 + c4 + c5;
cout << "Your total score is " << total << "!\n";
}
// whether you've entered 2 or 5 cards, the following code
// assumes all 5 cards contain relevant values.
What's not to get? If I tell the program I want to enter 2 cards, and then enter two cards, the rest of the program treats c3, c4 and c5 the same way as it would if I'd entered 5 cards, and it shouldn't because c3, c4 and c5 have random junk in them.