Giorgi.lm13
Giorgi.lm13

Reputation: 11

Solitaire in C- problem with card symbols

I am an engineering student. As an exam test, the professor asks us to implement solitaire in C. Being a beginner, I'm having some difficulty displaying the suits of the cards. If I convert the decimal number to an ASCII symbol, the output returns a question mark "?"; Is there any way to show the sign instead of the question mark? I'm trying to get a more graphic symbol, using ASCII. Here are the codes that I have compiled. During compilation, it does not return any errors but the executable is empty; it does not return anything. The compiler I'm using is Dev-C ++. Could it be that my console font doesn't support this character set? If yes, is it possible to modify it to make it compatible?

Test 1:

#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>

typedef enum { cuori, quadri, fiori, picche }  semi;
typedef enum { coperta, scoperta, eliminata }  status;

typedef struct _card
{
    int valore;
    semi seme;
    status stato;
} card;


const wchar_t visSemi[][2] = { L"\u2665", L"\u2660", L"\u2663", L"\u2666" };


void visCard(card *c)
{
    _setmode(_fileno(stdout), _O_U16TEXT);

    if (c->stato == scoperta)
    {
        switch (c->valore)
        {
        case 1:
            printf ("A\n");
            break;
        case 11:
            printf ("J\n");
            break;
        case 12:
            printf("Q\n");
            break;
        case 13:
            printf("K\n");
            break;
        }

        printf("\n", visSemi[c->seme]);
    }
    else
    {
        if (c->stato == coperta)
            printf("??\n");
    }

    _setmode(_fileno(stdout), _O_TEXT);
}

int main()
{
    card pila[8] = {
                     {  1, cuori, scoperta },
                     { 11, quadri, scoperta },
                     { 12, fiori, scoperta },
                     { 13, picche, eliminata },
                     {  1, quadri, scoperta },
                     { 11, fiori, scoperta },
                     { 12, picche, scoperta },
                     { 13, cuori, coperta },
                   };

    int i;
    for (i = 0; i < 8; i++)
        visCard(&pila[i]);

    return 0;
}

Test 2 (more generally):

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main (void){
  _setmode(_fileno(stdout),_O_U16TEXT);

  wprintf(L"\x043a\x043e\x0448\x043a\x0430\x65e5\x672c\x56fd\n");
  return 0;
}

Upvotes: 1

Views: 158

Answers (1)

This use of printf():

printf("\n", visSemi[c->seme]);

is incorrect by its intention, although the argument of visSemi[c->seme] is evaluated but ignored:

"If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored."

Source: C18, §7.21.6.1/2

To print a wchar_t with printf() is not correct.

Use wprintf() to print a wchar_t:

wprintf(L"%lc\n", visSemi[c->seme]);

instead.

Upvotes: 2

Related Questions