Thread: Vector & char array problems. :-(

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    Vector & char array problems. :-(

    I had a question on here earlier about why my program was printing funny symbols, instead of numbers, and someone suggested it was because I had my struct declared as strings, and also to use vectors. Using the vectors, it worked fine, but I hadn't the need to pass a vector struct through a function, but in my major assignment, I do! 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. So I tried to not use vectors, but I get those funny symbols instead of the character. I'm sure there is a little function or something so that the actual character is stored and not the symbol, but I can't seem to find it. (If this makes any 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!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I've changed the strings to a char and to an int. So I tried to not use vectors, but I get those funny symbols instead of the character."

    Vectors can be passed to functions just like any other construct or else they wouldn't be very useful--you just have to know how to declare the type for the function parameter.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void print(vector<int>& a)
    {
    	for(int i=0; i<a.size(); i++)
    		cout<< a[i]<<endl;
    }
    
    int main () 
    {
    	vector<int> a;
    	for(int j=0; j<3; j++)
    		a.insert(a.end(),j);
    
    	print(a);
    	
    	return 0;
    }
    A char variable is an int type because all characters entered from the keyboard are stored as integer codes. You can look up the integer codes in an ASCII table, which should be in the back of every C++ book. A char literal is something enclosed in single quotes, like 'a'. So, you could do something like this:

    char ch = 'a';

    Since a char is an integer type, an 'a' is not stored in ch, rather the compiler converts the 'a' into the appropriate integer code and stores it in ch. Conversely, when you display a char, the integer is converted back to its character representation for display.

    As a further example, the char '1' is not stored as 1, it is stored as the integer code 049. On the other hand, the smiley face character is stored as the integer code 001. You do not have to assign a char literal to a char variable, you can assign it an integer directly:

    char ch=1;

    However, what you have done is assign the variable ch the integer code 1, and when you try and display a char type, it's integer code is always converted to the appropriate character--in this case a smiley face. It's a one-to-one mapping. Try the following code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main () 
    {
    	char ch = 1;
    	cout<<ch<<endl;
    
    	ch = '1';
    	cout<<ch<<endl;
    	
    	return 0;
    }
    You can even increment char's:

    char ch='a';
    ch++
    cout<ch<<endl;

    That will display 'b' because b's integer code is one higher than a's. You can even use that fact in an if statement:

    char ch='d';
    if(ch>='a' && ch<='z')
    cout<<"ch is lower case"<<endl;
    Last edited by 7stud; 05-19-2003 at 09:05 PM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM