integers storing as symbols in arrays

This is a discussion on integers storing as symbols in arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, in the following program, the random numbers once stored in the array, aren't storing as integers!! I have no ...

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

    integers storing as symbols in arrays

    Hi, in the following program, the random numbers once stored in the array, aren't storing as integers!! I have no idea why. I tried testing it by printing to the screen what the random number was before it went in the array, then printed it from the array, and it was different. The random number was generating fine, but once it was printed from the array it came up with weird symbols like hearts, diamonds, smiley faces, crosses etc. Has anybody any idea what this is?!! I tried looking on the forum, but I had no idea what to searhc for!!

    Heres the code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <fstream.h>
    #include <string>
    
    int randomCardValueGenerator(int, int);
    void DisplayWelcome();
    
    //a single card in a hand
    struct card
    {
       string suit;
       string value;
    };
    
    //min & max for card values
    const int minCardValue = 1; 
    const int maxCardValue = 14;
    
    //min & max for card suits
    const int minCardSuit = 1;
    const int maxCardSuit = 4;
    
    int main()
    {  
       struct card hand[5]; //an array of cards
       int randomCardValue;
       int randomCardSuit;
    
       DisplayWelcome();
       
       srand(time(NULL)); //seed random number with system time                      
       
       for(int i = 0; i < 5; i++)
       {
          randomCardValue = randomCardValueGenerator(minCardValue, maxCardValue);
          randomCardSuit = randomCardValueGenerator(minCardSuit, maxCardSuit);
    
          hand[i].value = randomCardValue;
          hand[i].suit = randomCardSuit;
    
          cout << "Card 1: " << randomCardSuit << " " << randomCardValue << "\n";   
          cout << "hand[i].value = " << hand[i].value << ", hand[i].suit = " << hand[i].suit << "\n";
       }
    
       return 0;
    }
    
    //creates a random card value
    int randomCardValueGenerator(int minValue, int maxvalue)
    {   
       int range;
       int randNum;
       int number;
       
       number = rand();
       range = maxvalue - minValue + 1;  
       randNum = number % range + minValue;
    
       return randNum;
    }
    
    
    
    void DisplayWelcome()
    {
       cout << "\n\n\t\t\tWelcome to my random number game!\n";
       cout << "\t\t\t---------------------------------\n\n";
    
    }

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    You are using strings to store the values. The program will print out characters, that's what you are seeing.

    You could try looking up vectors, they are in <vector>.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    for example...

    ascii for 1 or 2 is a smiley face...

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

    Thankyou!

    Thanks! It's all working now. :-)

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    7
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. Storing values in arrays
    By Ripper1 in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2003, 11:04 AM
  4. Replies: 6
    Last Post: 08-04-2003, 10:57 AM
  5. c++ builder - storing text from edit boxes into arrays
    By learning110 in forum Windows Programming
    Replies: 0
    Last Post: 05-26-2003, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21