Ok, well it worked for the sample program I did. But for my major assignment I tried using vectors but it didn't work. I read up a little on vectors, and the problem I'm having is that by the looks of it, you cannot pass a vector structure through a function. That probably makes no sense, as I don't quite know how to put it. I've changed the strings to a char and to an int. Now, not using vectors, I still get those funny symbols instead of the character. I can't seem to find anywhere in my textbook how to convert the location to the actual character (if that makes sense!).
This is the code without use of vectors:
Code:
#include <iostream>
#include <fstream.h>
#include <string>
#include <stdlib.h>
#include <time.h>
//#include <vector>
const int maxCards = 5; //no. of cards in a hand
const int minCardValue = 1; //min for random card values
const int maxCardValue = 14; //max for random card values
const int minCardSuit = 1; //min for random card suits
const int maxCardSuit = 4; //max for random card suits
ifstream cardFile;
//a single card in a hand
struct card
{
char suit;
int value;
};
string fileName;
//function prototypes
bool fillUpHandFromFile(card []);
void DisplayHandMenu();
void MainMenuLogic(card []);
void IncorrectResponse(card []);
//void ConvertSuit_Value(card []);
void DisplayHand(card []);
void DisplayTitle();
void Exit();
void selectRandomHand(card []);
char convertRandomSuit(int);
int randomCardValueGenerator(int, int);
void DisplayHandGreeting();
int main()
{
//vector<card> hand(maxCards); //vector of cards
struct card hand[maxCards]; //an array of cards
srand(time(NULL)); //seed random number with system time
DisplayHandMenu(); //show menu
MainMenuLogic(hand); //logic behind the menu
return 0;
}
//function that displays the title
void DisplayTitle()
{
//display title
cout << "\t*************************************************************\n";
cout << "\t* WELCOME TO CARMEN'S CASINO!! *\n";
cout << "\t* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *\n";
cout << "\t*************************************************************\n";
}
//function that displays menu options
void DisplayHandMenu()
{
//clear the screen
system("cls");
DisplayTitle();
//display instructions
cout << "\n\n\tThis program will evaluate a single poker hand. \n";
cout << "\tSimply select one of the hands below, so we can begin playing!\n\n\n";
//display menu items
cout << "\t\t\t______________________________\n";
cout << "\t\t\t______________________________\n\n";
cout << "\t\t\t MAIN MENU\n";
cout << "\t\t\t ~~~~~~~~~\n\n";
cout << "\t\t\t 1. Flush\n";
cout << "\t\t\t 2. Four of a Kind\n";
cout << "\t\t\t 3. Full House\n";
cout << "\t\t\t 4. Pair\n";
cout << "\t\t\t 5. Royal Flush\n";
cout << "\t\t\t 6. Straight\n";
cout << "\t\t\t 7. Straight Flush\n";
cout << "\t\t\t 8. Three of a Kind\n";
cout << "\t\t\t 9. Two Pair\n";
cout << "\t\t\t 10. Random Hand\n";
cout << "\t\t\t 11. Exit\n";
cout << "\t\t\t______________________________\n";
cout << "\t\t\t______________________________\n\n";
cout << "\n\t*************************************************************\n";
//prompt user to make a selection
cout << "\n\tPlease make your selection: ";
}
//function that assigns a filename corresponding to user selection
void MainMenuLogic(card hand[])
{
string option = "textCards"; //whether or not user wants to exit etc.
int selection; //the number the user selects from the menu
//read in the users option
cin >> selection;
switch(selection)
{
case 1: fileName = "Flush"; //if user selects flush
break;
case 2: fileName = "FourKind"; //if user selects four of a kind
break;
case 3: fileName = "FullHouse"; //if user selects full house
break;
case 4: fileName = "Pair"; //if user selects pair
break;
case 5: fileName = "RoyalFlush"; //if user selects royal flush
break;
case 6: fileName = "Straight"; //if user selects straight
break;
case 7: fileName = "StraightFlush"; //if user selects straight flush
break;
case 8: fileName = "ThreeKind"; //if user selects three of a kind
break;
case 9: fileName = "TwoPair"; //if user selects two pair
break;
case 10: option = "random";
selectRandomHand(hand);
break;
case 11: option = "exit"; //if user selects exit
break;
default: IncorrectResponse(hand); //if user enters a value other then 0-9
}
//if user chooses to exit
if (option == "exit")
{
Exit();
}
else if (option == "random")
{
// system("cls");
//DisplayTitle();
// cout << "\n\t\t\tRANDOM HAND\n";
// cout << "\t\t\t-----------\n\n";
// DisplayHand(hand); //display the chosen hand
}
else //if user does not choose to exit or select random hand
{
fillUpHandFromFile(hand); //fill the hand array from file
//ConvertSuit_Value(hand); //convert the no's into words
DisplayHandGreeting();
DisplayHand(hand); //display the chosen hand
}
}
//function that alerts user if they enter an invalid selection
void IncorrectResponse(card hand[])
{
//display alert message
cout << "\n\n\t\t\tYou must enter 0-9!\n\n";
cout << "\t\t\t";
system("pause");
DisplayHandMenu(); //display the menu options
MainMenuLogic(hand); //run the menu
}
//function that fills the hand array from the file
bool fillUpHandFromFile(card hand[])
{
string FileName = fileName + ".txt"; //adds ext .txt to fileName
//open the card textfile
cardFile.open(FileName.c_str());
//if it cannot open the file
if(cardFile.fail())
{
//display message
cout << "No such file name found!";
return false;
}
else //if file opens
{
for(int index=0; index<=maxCards-1; index++)
{
//read value from file and put into array position index
cardFile >> hand[index].suit;
//read next value from file and put into array position index
cardFile >> hand[index].value;
}
cardFile.close(); //close the text file
return true;
}
}
//function that displays the hand of cards
void DisplayHand(card hand[])
{
//Display Cards
for (int a=0; a < maxCards; a++)
{
cout << "\t\t\tCard " << a+1 << ": " << hand[a].value << " of " << hand[a].suit << "\n";
}
cout << "\n\t\t\t------------------------------\n";
cout << "\n\n\t\t\t";
system("pause");
DisplayHandMenu();
MainMenuLogic(hand);
}
//function that exits the program
void Exit()
{
cout << "\n\n\t*************************************************************\n";
cout << "\n\t Thankyou for visiting Carmen's Casino!";
cout << "\n\n\t\t\t";
system("pause");
}
//selects a random hand & stores in array
void selectRandomHand(card hand[])
{
int randomCardValue;
int randomCardSuit;
//char newRandomCardsuit;
for (int i=0; i < maxCards; i++)
{
randomCardValue = randomCardValueGenerator(minCardValue, maxCardValue);
randomCardSuit = randomCardValueGenerator(minCardSuit, maxCardSuit);
//newRandomCardsuit = randomCardSuit;
//newRandomCardsuit = convertRandomSuit(randomCardSuit);
//put into hand array
hand[i].value = randomCardValue;
hand[i].suit = randomCardSuit;
cout << "hand[i].value = " << hand[i].value << "\n";
cout << "hand[i].suit = " << hand[i].suit << "\n";
}
}
//creates a random number within specified range
int randomCardValueGenerator(int minValue, int maxvalue)
{
int range;
int randNum;
int number;
number = rand();
range = maxvalue - minValue + 1;
randNum = number % range + minValue;
return randNum;
}
//converts 1-4, into suit names
char convertRandomSuit(int randomCardSuit)
{
char newSuitValue;
if (randomCardSuit == 1) //if num is 1, return Clubs
{
newSuitValue = 'C';
}
else if (randomCardSuit == 2) //if num is 2, return Spades
{
newSuitValue = 'S';
}
else if (randomCardSuit == 3) //if num is 3, return Diamonds
{
newSuitValue = 'D';
}
else newSuitValue = 'H'; //if num is 4, return Hearts
return newSuitValue;
}
void DisplayHandGreeting()
{
system("cls");
DisplayTitle();
//Display Greeting
cout << "\n\n\t\t\tYou selected " << fileName << "!\n";
cout << "\n\t\t\t------------------------------\n";
//Display Hand
cout << "\n\t\t\tSELECTED HAND\n";
cout << "\t\t\t-------------\n\n";
}
Now the only difference when I was attempting to use vectors with this code is the commenting out lines; #include vector,
'vector<card> hand(maxCards); ' when I was declaring it.
When I tried declaring a function I tried:
for example: MainMenuLogic(vector<card> hand[])
Which is probably really wrong! But can you have both a vector, and use struct, so its a vector AND type card?
Sorry, but I'm soooo new at this!