-3

I'm a beginner to C++ and I am making the card game WAR and I'm trying to make the value of the card a Letter whenever the card number equals 11, 12, 13, 14 etc. Every time I output it enters the value of 74 for A etc. I can't figure out why.

Here is my code below.

#include<iostream>
using namespace std;


int main()
{
    //Title
    int dhori = 205;
    int dline = 186;
    int DTLcorner = 201;
    int DTRcorner = 187;
    int DBLcorner = 200;
    int DBRcorner = 188;
    
    cout << (char)DTLcorner << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori <<
        (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)DTRcorner << endl;
    cout << (char)dline << "The Card Game : WAR" << (char)dline << endl;
    cout << (char)DBLcorner << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori <<
        (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)dhori << (char)DBRcorner << endl;
    
    int line = 179;
    int hori = 196;
    int TLcorner = 218;
    int TRcorner = 191;
    int BLcorner = 192;
    int BRcorner = 217;
    int Heart = 3;
    int Club = 5;
    int Diamond = 4;
    int Spade = 6;
    char J = 74;
    char Q = 81;
    char K = 75;
    char A = 65;
    

    int number;
    int lower = 3, upper = 6;
    srand(time(NULL));
    number = (rand() % (upper - lower +1)) +lower  ;

    int cardnumber;
    int lower1 = 2, upper1 = 14;
    srand(time(NULL));
    cardnumber = (rand() % (upper1 - lower1 + 1)) + lower1;

    int ocardnumber;
    int lower2 = 2, upper2 = 14;
    srand(time(NULL));
    ocardnumber = (rand() % (upper2 - lower2 + 0)) + lower2;

    int cardsym;
    int lower3 = 3, upper3 = 6;
    srand(time(NULL));
    cardsym = (rand() % (upper3 - lower3 + 0)) + lower3;

    cout << (char)J << Q << K << A << endl;
    cout << cardnumber << " " << ocardnumber << endl;

    if (cardsym == 3) {
        cardsym = Heart;
    }
    if (cardsym == 4) {
        cardsym = Diamond;
    }
    if (cardsym == 5) {
        cardsym = Club;
    }
    if (cardsym == 6) {
        cardsym = Spade;
    }

    //Changes Cards to A J K Q
    if (cardnumber == 11) {
        cardnumber = (char)J;
    }
    if (cardnumber == 12) {
        cardnumber = (char)Q;
        cardnumber = (char)cardnumber;
    }
    if (cardnumber == 13) {
        cardnumber = K;
        cardnumber = (char)cardnumber;
    }
    if (cardnumber == 14) {
        cardnumber = A;
        cardnumber = (char)cardnumber;
    }
    if (ocardnumber == 11) {
        ocardnumber = J;
        cardnumber = (char)ocardnumber;
    }
    if (ocardnumber == 12) {
        ocardnumber = Q;
        cardnumber = (char)ocardnumber;
    }
    if (ocardnumber == 13) {
        ocardnumber = K;
        cardnumber = (char)ocardnumber;
    }
    if (ocardnumber == 14) {
        ocardnumber = A;
        cardnumber = (char)ocardnumber;
    }
    
    switch (cardnumber) {
    case 11: cout << (char)J;
    case 12: cout << (char)Q;
    case 13: cout << (char)K;
    case 14: cout << (char)A;
        break;
    }

    if (number == 3) {



        switch (cardnumber) {
        case 11: cout << (char)J;
        case 12: cout << (char)Q;
        case 13: cout << (char)K;
        case 14: cout << (char)A;
            break;
        }

        cout << "Your Card" << " " << "Opponent's Card" << endl;
        cout << (char)TLcorner << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)TRcorner << " " << (char)TLcorner << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)TRcorner << endl;
        cout << (char)line << cardnumber << "     " << (char)line << " " << (char)line << ocardnumber << "      " << (char)line << endl;
        cout << (char)line << "       " << (char)line << " " << (char)line << "       " << (char)line << endl;
        cout << (char)line << "   " << (char)Heart << "   "<< (char)line<< " " << (char)line << "   " << (char)cardsym << "   " << (char)line << endl;
        cout << (char)line << "       " << (char)line << " " << (char)line << "       " << (char)line << endl;
        cout << (char)line << "    " << cardnumber << " " << (char)line << " " << (char)line << "      " << ocardnumber << (char)line << endl;
        cout << (char)BLcorner << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)BRcorner << " " << (char)BLcorner << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)hori << (char)BRcorner << endl;
    
        if (cardnumber == ocardnumber) {
            cout << "You Tied" << endl;
        }
        if (cardnumber > ocardnumber) {
            cout << "You Won!" << endl;
        }
        if (cardnumber < ocardnumber) {
            cout << "You lost!" << endl;
        }

        
    }
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 6
    I know you're a beginner, but you don't need all of this code to simply experiment with `cout` and `char` variables. Just a two or 3 line `main` program is all you need. You waited until you wrote a 60 line program before you realized there is something different about printing char variables. – PaulMcKenzie Oct 18 '21 at 20:44
  • 1
    Handy reading: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 Oct 18 '21 at 20:46
  • 1
    Learn to use character literals instead of numbers, e.g. 'A' replaces 65. – Thomas Matthews Oct 18 '21 at 20:47
  • 1
    There are a number of spots where you do `cardnumber = (char)ocardnumber;` - I'm guessing this is a copy-paste error and you meant `ocardnumber = (char)ocardnumber;`. – 0x5453 Oct 18 '21 at 20:49
  • Instead of having the variables `J, Q, K, A`, use character literals: `'J', 'Q', 'K', 'A'`. – Thomas Matthews Oct 18 '21 at 20:53
  • 1
    See [ASCII Table](https://www.asciitable.com/) Non-whitespace printable values are `33` - `126`. There is no guarantee what "extended" ASCII values your terminal supports. Many no longer support the old line-drawing characters as terminal "windowing" is rarely seen this century (outside a curses implementation) – David C. Rankin Oct 18 '21 at 20:54
  • Each line in `switch/case` statements should end with `break;` (unless you are avoiding that on purpose, I don't understand the game...) – Barmak Shemirani Oct 19 '21 at 01:31

1 Answers1

-1

have you tried to do char(asciiVal) instead of (char)asciiVal? also you should check the values on an ascii table some terminals wont show unicode or symbols they will just display a '?' sign

bonii
  • 71
  • 1
  • 9