error initializer before int

I'm currently working on a final project for a class. It is a five card stud game and the error that is:

final project.cpp:59:1: error: expected initializer before 'if'

The code is kinda messy as I am in process of just making it work,

Any assistance is greatly appreciated,

Thank you for your time.

#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;
using std::istream;
int choice;
int cardsPerSuit = 13;
int numCards = 52;

int sizeOfHand = 5;
int sizeOfHand2 = 10;

string suits[] = {"Spades", "Hearts", "Clubs", "Diamonds"};

int main()
/*
Enter a number of hands to generate:
> 2

Ace of Diamonds, 6 of Hearts, 4 of Spades, 2 of Spades
Queen of Hearts, Queen of Hearts

Save generated hands to file? (yes/no)

> yes

File saved as Hands.txt

*/
{
cout << "This program will simulate a five card stud poker \n"
<< "hand placing you against the computer. The deck of \n"
<< "cards will be shuffled and you will be dealt five \n"
<< "random cards as your hand and the computer player \n"
<< "will also be dealt five random cards. You will then \n"
<< "be asked if you won or lost based on the rules of \n"
<< "which will be displayed. The program will then ask \n"
<< "you if you would like to save your hand for later \n"
<< "reflection\n\n"
<< "Would you like to continue? \n"
<< "Press 1 if you would like to continue.\n"
<< "Press 2 if you would like to exit the program.\n";

cin >> choice;
{
if (choice = 2)
{
cout << "The program will now end. Have a nice day! Goodbye!\n";
return 0;
}
}
}
void initializeDeck (int *cards)

if (choice = 1)
{
for (int i = 0; i < numCards; i++)
{
*cards = i;
cards++;
}


void swapCards (int *cards, int a, int b)
{
int temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}

void shuffle (int *cards)
{
srand(time(NULL));

for (int i = 0; i < numCards; i++)
{
int randomPosition = rand() % numCards;
// cout << "random position = " << randomPosition << "\n";
swapCards(cards, i, randomPosition);
}
}

void printCardName(int cardIndex)
{
int cardNumber = cardIndex % cardsPerSuit + 1;
string cardNumberString;

if (cardNumber == 1)
{
cardNumberString = "Ace";
}
else if (cardNumber == 10)
{
cardNumberString = "10";
}
else if (cardNumber == 11)
{
cardNumberString = "Jack";
}
else if (cardNumber == 12)
{
cardNumberString = "Queen";
}
else if (cardNumber == 13)
{
cardNumberString = "King";
}
else
{
cardNumberString = string(1, '0' + cardNumber);
}

cout << cardNumberString << " of " << suits[cardIndex / 13];
}

void printHand (int *cards)
{
cout << "YOUR CARDS" << "\n";
for (int i = 0; i < sizeOfHand; i++)
{
printCardName(cards[i]);
cout << "\n";
}

cout << "\n\nCOMPUTER PLAYERS CARDS" << "\n";
for (int i = 0+5; i < sizeOfHand2; i++)
{
printCardName(cards[i]);
cout << "\n";
}
}

int main()
{
int deck[52];
initializeDeck(deck);
shuffle(deck);

printHand(deck);

//for (int i = 0; i < numCards; i++)
//{
// cout << "For card of index " << deck[i] << ": ";
// printCardName(deck[i]);
// cout << "\n";
//}
}
}
You're formatting is really bad, and can you put you code in tags please?
Because of your formatting and lack of indenting it's difficult to spot stuff like this:

1
2
3
4
5
6
7
8
9
10
11
12
void initializeDeck (int *cards)    // <-- you should have an opening brace there

	if (choice = 1)  // <-- you want a double equals here, not just one
	{
		for (int i = 0; i < numCards; i++)
		{
			*cards = i;
			cards++;
		}

...
...


stuff like that.

Get your code formatted and indented properly and i'm sure you'll spot issues like this more easily

edit:
you also have 2 main functions, which is not good either.
Last edited on
Thank you very much sir, I have cleaned up the brackets a bit and I am now trying to get the final piece of my project. I must have it write the data to a text file. The //(commented out code) is causing the error. I it is saying that the function was not declared and I figure that is the problem exactly. I need to figure out how to pull my card hands form the function and write them to my Handofcards.txt file? Do you have any suggestions. Your input would be greatly appreciated. Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;


int cardsPerSuit = 13;
int numCards = 52;
int sizeOfHand = 5;
int sizeOfHand2 = 10;

string suits[] = {"Spades", "Hearts", "Clubs", "Diamonds"};

void initializeDeck (int *cards)
{
    for (int i = 0; i < numCards; i++)
    {
        *cards = i;
        cards++;
    }
}

void swapCards (int *cards, int a, int b)
{
    int temp = cards[a];
    cards[a] = cards[b];
    cards[b] = temp;
}

void shuffle (int *cards)
{
    srand(time(NULL));

    for (int i = 0; i < numCards; i++)
    {
        int randomPosition = rand() % numCards;
        // cout << "random position = " << randomPosition << "\n";
        swapCards(cards, i, randomPosition);
    }
}

void printCardName(int cardIndex)
{
    int cardNumber = cardIndex % cardsPerSuit + 1;
    string cardNumberString;

    if (cardNumber == 1)
    {
        cardNumberString = "Ace";
    }
    else if (cardNumber == 10)
    {   
        cardNumberString = "10";
    }
    else if (cardNumber == 11)
    {       
        cardNumberString = "Jack";
    }
    else if (cardNumber == 12)
    {
        cardNumberString = "Queen";
    }
    else if (cardNumber == 13)
    {   
        cardNumberString = "King";
    }
    else
    {
        cardNumberString = string(1, '0' + cardNumber);
    }
   
    cout << cardNumberString << " of " << suits[cardIndex / 13];
}

void printHand (int *cards)
{
    cout << "YOUR CARDS" << "\n";
    for (int i = 0; i < sizeOfHand; i++)
    {
        printCardName(cards[i]);
        cout << "\n";
    }

    cout << "\n\nCOMPUTER PLAYERS CARDS" << "\n";
    for (int i = 0+5; i < sizeOfHand2; i++)
    {
        printCardName(cards[i]);
        cout << "\n";
     }
}

int main()
{
   cout << "This program will simulate a five card stud poker \n"
        << "hand placing you against the computer.  The deck of \n" 
        << "cards will be shuffled and you will be dealt five \n"
        << "random cards as your hand and the computer player \n"
        << "will also be dealt five random cards. You will then \n"
        << "be asked if you won or lost based on the rules of \n"
        << "poker which will be displayed. The program will then \n"
        << "save your hand for later reflection.\n\n"
        << "Pless press enter to continue.\n";
        
   cin.get();  

    {
    int deck[52];
    initializeDeck(deck);
    shuffle(deck);
    printHand(deck);
    }
    {
    cout << "Your hand has been saved to the file named Handofcards.txt\n"
          << "Thank you for playing the game!\n";
    
     //ofstream myfile;
     //myfile.open ("Handofcards.txt");
     //myfile << CardName(cards[i]);
     //myfile.close();     
    }
     
return 0; 
}
On line 122 your compiler is telling you there is no such "CardName" method.

Here you want something similar to your "void printHand (int *cards)" method, whereby you pass your cards in and rather print to the screen you write to a file.
I'm at a wall. lol I have tried moving around all the different types I know and learned but all keep creating same kind of error. Its like the function is not being recognized. Thank you very much for your assistance. It's greatly appreciated.
Its like the function is not being recognized.

Yes. It is like the function CardName doesn't exist. Can you point out the definition in your code above (with a line number, pehaps?)
Topic archived. No new replies allowed.