0

first of all, I understand mostly everything in this program that I copied from this book.
second, i just wanted to see if it worked ,
the only problem is that it says 'expected declaration specifiers before 'main'' and i don't know what it means
ps this is a really long program (300+ lines)

#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1

void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int 
ranksinHand[], int suitsinHand[])
int analyzeHand(int ranksinHand[], int suitsinHand[]);


main()
{
int bet;
int bank = 100;
int i;
int cardRank [5];
int cardSuit [5];
int finalRank[5];
int finalSuit[5];
int ranksinhand[13];
int suitsinhand[4];
int winnings;
time_t t;
char suit, rank, stillPlay;


printGreeting();


do{
    bet = getBet();
    srand(time(&t));
    getFirstHand(cardRank, cardSuit);
    printf("Your five cards: \n\n");
    for (i = 0; i < 5; i++)
    {
        suit = getSuit(cardsSuit[i]);
        rank = getRank(cardRank[i]);
        printf("Card #%d: %c%c\n\n", i+1, rank, suit);
    }

    for (i=0; i < 4; i++)
    {
        suitsinHand[i] = 0;
    }
    for (i=0; i < 13; i++)
    {
        ranksinHand[i] = 0;
    }

    getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand, 
    suitsinHand);

    printf("Your five final cards:\n\n");
    for (i = 0; i < 5; i++)
    {
        suit = getSuit(finalSuit[i]);
        rank = getRank(finalRank[i]);
        printf("Card #%d: %c%c\n\n", i+1, rank, suit);
    }

    winnings = analyzeHand(ranksinHand, suitsinHand);
    printf("You won %d!\n\n", bet*winnings);
    bank = bank - bet + (bet*winnings)
    printf("\n\nYour bank is now %d.\n\n", bank);
    printf("Want to play again? ");
    scanf(" %c", &stillPlay);
}while (toupper(stillPlay) == 'Y');



return;
 }

 /*************************************************************************/


void printGreeting();
{

printf("**********************************************************\n\n");
printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
printf("\tHome of the Video Draw Poker");
printf("**********************************************************\n\n");

printf("Here are the rules\n");
printf("You start with 100 credits, and you make a bet from");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and then you choose which ");
printf("cards to keep");
printf("or discard\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair \t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight \t\t\t4 credits");
printf("Flush\t\t\t\t5 credits");
printf("Full House\t\t\t8 credits");
printf("Four of a Kind\t\t\t10 credits");
printf("Straight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}


void getFirstHand(int cardRank[], int cardSuit[]);
{
int i,j;
int carDup;

for(i=0; i < 5; i++)
{
    carDup = 0;
    do{
        cardRank[i] = (rand() % 13);
        cardSuit[i] = (rand() % 4);

        for (j=0; j < i; j++)
        {
            if ((cardRank[i] == cardRank[j] &&
                 cardSuit[i] == cardSuit[j]))
            {
                carDup = 1;
            }
        }
    }while (carDup == 1;);
}
}

char getSuit(int suit)
{
switch
{
    case 0:
        return('C');
    case 1:
        return('D');
    case 2:
        return('H');
    case 3:
        return('S');
}
}

char getRank(int rank)
{
switch (rank)
{
    case 0:
        return('A');
    case 1:
        return('2');
    case 2:
        return('3');
    case 3:
        return('4');
    case 4:
        return('5');
    case 5:
        return('6');
    case 6:
        return('7');
    case 7;
        return('8');
    case 8:
        return('9');
    case 9:
        return('T');
    case 10:
        return('J');
    case 11:
        return('Q');
    case 12:
        return('K');
}
}
int getBet()
{
int bet;
do
{
    printf("How much do you want to bet?(Enter a number");
    printf("from 1 to 5, or 0 to quit the game): ");
    scanf(" %d", &bet);

    if (bet >= 1 && bet <= 5)
    {
        return(bet);
    }
    else if (bet == 0)
    {
        exit(1);
    }
    else
    {
        printf("\n\nPlease enter a bet from 1-5 or ");
        printf("0 to quit the game\n\n");
    }
}while ((bet < 0) || (bet > 5));
}

int analyzeHand(int ranksinHand[], int suitsinHand[])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;


for (suit = 0; suit < 4; suit++)
    if (suitsinHand[suit] == 5)
        flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
    rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
    num_consec++;
if(num_consec == 5) {
    straight = TRUE;
}
for (rank = 0; rank < 13; rank++){
    if (ranksinHand[rank] == 4)
        four == TRUE;
    if (ranksinHand[rank] == 3)
        three == TRUE;
    if (ranksinHand[rank] == 2)
        pairs++;
}
if (straight && flush){
    printf("Straight Flush\n\n");
    return(20);
}
else if (four){
    printf("Four of a kind\n\n");
    return (10);
}
else if (three && pairs == 1){
    printf("Full House\n\n");
    return (8);
}
else if (flush){
    printf("Flush\n\n");
    return (5);
}
else if (straight){
    printf("Straight\n\n");
    return (4);
}
else if (three){
    printf("Three of a Kind\n\n");
    return (3);
}
else if (pairs == 2){
    printf("Two Pairs\n\n");
    return (2);
}
else if (pairs == 1){
    printf("Pair\n\n");
    return (1);
}
else{
    printf("High Card\n\n");
    return (0);
}
}

void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int 
ranksinHand[], int suitsinHand[])
{
int i, j, carDup;
char suit, rank, ans;

for (i=0; i < 5; i++)
{
    suit = getSuit(cardSuit[i]);
    rank = getRank(cardRank[i]);
    printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
    printf("\nPlease answer (Y/N):");
    scanf(" %c", &ans);
    if (toupper(ans) == 'Y')
    {
        finalRank[i] = cardRank[i];
        finalSuit[i] = cardSuit[i];
        ranksinHand[finalRank[i]]++;
        suitsinHand[finalSuit[i]]++;
        continue;
    }
    else if (toupper(ans) == 'N')
    {
        carDup = 0;
        do{
            carDup = 0;
            finalRank[i] = (rand() % 13);
            finalSuit[i] = (rand() % 4);

            for (j=0; j < 5; j++)
            {
                if((finalRank[i] == finalRank[j]) && (finalSuit[i] == 
      finalSuit[j]))
                {
                    carDup = 1;
                }
            }

            for (j=0; j < i; j++)
            {
                if((finalRank[i] == finalRank[j]) && (finalSuit[i] == 
    finalSuit[j]))
                {
                    carDup = 1;
                }
            }
        }while (carDup == 1);
        ranksinHand[finalRank[i]]++;
        suitsinHand[finalSuit[i]]++;
    }

}
}
  • 1
    You should find a book written in the last couple of decades. – rici Oct 05 '17 at 00:02
  • Because the compiler expected declaration specifiers before 'main' – savram Oct 05 '17 at 00:25
  • @rici: You referenced http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1 in a comment to a now-deleted answer. Please refer to http://port70.net/~nsz/c/c11/n1570.html#6.5.3.4p8 in the same document. – Jonathan Leffler Oct 05 '17 at 00:27
  • @johnathan: 1. examples are not normative (http://port70.net/~nsz/c/c11/n1570.html#Forewordp8). 2. empty parameter lists are deprecated (http://port70.net/~nsz/c/c11/n1570.html#6.11.6). Gcc will warn if you use `-Wstrict-prototypes` (not included with either `-Wall` nor `-Wextra`, presumably because the construct is so common.) – rici Oct 05 '17 at 05:12

2 Answers2

0

By convention main() must return an integer. You have to declare it like that:

int main()
{
  // Your method

  return 0;
}
YaatSuka
  • 231
  • 1
  • 9
-1

int mainor void main would do the work. Here, int main is shown to be the C standard. Refer to Return type of main() too.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MLPJ
  • 108
  • 1
  • 12
  • `void main()` is not correct, by convention `main()` must return an integer – YaatSuka Oct 05 '17 at 00:22
  • @YaatSuka: It's a bit more nuanced than that — MS Windows documents `void main()`, sadly, but it is only allowable there, as noted in the first cross-reference (which has answers that quote the standards, and pointers to where MS documents `void main()`, etc.) – Jonathan Leffler Oct 05 '17 at 00:25
  • @YaatSuka yes it is clearly stated in the link given. But it does not give errors in most of compilers. – MLPJ Oct 05 '17 at 00:25
  • I know, it's just conventionnal. If you want to code, you must do it well – YaatSuka Oct 05 '17 at 00:27