Not getting the desired output

Hi
i wonder if anyone can help me.
i am not getting the desired output for the if statements that have to deal with the aces function.

i want it to output "2 or 22" if aces+aces is the input etc.

please help



#include <iostream>
#include <string>

using namespace std;

int cardVal()
{
string card;
cin >> card;

if(card == "Kc" || card == "Kd" || card == "Kh" || card == "Ks" ||
card == "Qc" || card == "Qd" || card == "Qh" || card == "Qs" ||
card == "Jc" || card == "Jd" || card == "Jh" || card == "Js" ||
card == "10c" || card == "10d" || card == "10h" || card == "10s")
{
return 10;
}

else if(card == "9c" || card == "9d" || card == "9h" || card =="9s")
{
return 9;
}

else if(card == "8c" || card == "8d" || card == "8h" || card =="8s")
{
return 8;
}

else if(card == "7c" || card == "7d" || card == "7h" || card =="7s")
{
return 7;
}

else if(card == "6c" || card == "6d" || card == "6h" || card =="6s")
{
return 6;
}

else if(card == "5c" || card == "5d" || card == "5h" || card =="5s")
{
return 5;
}

else if(card == "4c" || card == "4d" || card == "4h" || card =="4s")
{
return 4;
}

else if(card == "3c" || card == "3d" || card == "3h" || card =="3s")
{
return 3;
}

else if(card == "2c" || card == "2d" || card == "2h" || card =="2s")
{
return 2;
}

}

int aces()
{
string card;
cin >> card;

if(card == "Ac" || card == "Ad" || card == "Ah" || card == "As")
{
return 1;
}
}

int main()
{
int sum = 0;

if(sum = cardVal() + cardVal())
{
cout << sum << endl;
}

else if(sum = aces() + aces())
{
cout << sum << " or " << sum + 20 <<endl;
}

else if(sum = aces() + cardVal())
{
cout << sum << " or " << sum + 20 << endl;
}

else if(sum = cardVal() + aces())
{
cout << sum << " or " << sum + 20 << endl;
}

else if(sum == 21)
{
cout << "Blackjack !" <<endl;
}

return 0;
}




1
2
3
4
if(card == "Kc" || card == "Kd" || card == "Kh" || card == "Ks" ||
card == "Qc" || card == "Qd" || card == "Qh" || card == "Qs" || 
card == "Jc" || card == "Jd" || card == "Jh" || card == "Js" || 
card == "10c" || card == "10d" || card == "10h" || card == "10s"


rather than have such an expression you can split the card entry into suit and rank from one string – suppose rank comes first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    std::cout << "Enter card value, rank first: \n";
    std::string card_value{};
    getline(std::cin, card_value);
    std::string card_rank = (card_value.size() == 2 ) ? card_value.substr(0,1) : card_value.substr(0, 2);
    std::string card_suit {card_value.back()};

    std::cout << "Card value: " << card_value << "\n";
    std::cout << "Card rank: " << card_rank << "\n";
    std::cout << "Card suit: " << card_suit << "\n";
}


you could then wrap this code into a function with data validation so that incorrect entries for rank and/or suit are rejected
Topic archived. No new replies allowed.