Thread: storing characters read from text file in 2 different string

  1. #1
    Registered User
    Join Date
    Aug 2010

    storing characters read from text file in 2 different string

    I wanted to ask you all a question.
    suppose that my c++ program has read from a text file a set of characters like the one below using the command inData >> temp. i output the characters of the text file and stored all the characters in a string temp.
    this is how it appears in the c++ output window:


    Now i want to store the 'spades','spades','clubs','spades','spades' in a string known as string suit. And i want to store the 'ace','10','3','king','5' in another string to be known as string face.
    This is where i am stuck. I can't seem to understand how to do it. I think that i have to create 2 string array.
    i tried this:
    str= temp;
    //cout << str << endl;
    but it's not working.
    then i tried this:
    //str = temp-'Spades';
    it's definitely not working.
    if someone could plz help me....

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Tomball, TX
    Why don't you first try to save ALL the data? You could use a string array, a class container, a struct container, or a vector, its all up to you. Try saving all the data then cout the data and report back with your output and the code.
    Virtual reality hello world in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    Sep 2010
    You could use just one array and hold the card number, 1 of 52 possibilities, as an integer.

    Let’s say there are 100 hands of 5 cards.

    Int My_hand[100][5];

    8 of Clubs
    King of Diamonds
    3 of Hearts
    Ace of Spades
    10 of spades

    My_hand[1][1] = 8
    My_hand[1][2] = 26 (King of Diamonds)
    My_hand[1][3] = 29
    My_hand[1][4] = 40
    My_hand[1][5] = 49

    Then loop thru 52 if statements:

    if My_hand[x][y] = 27
    print "Ace of Hearts"
    else if My_hand[x][y] = 28
    print "Two of Hearts" etc…

    Or you could use 2 arrays:

    Int My_hand_suit[100][5];
    Int My_hand_face[100][5];

    My_hand_suit[1][1] = 1
    My_hand_face[1][1] = 8

    My_hand_suit[1][2] = 2
    My_hand_face[1][2] = 13 (King of Diamonds) etc…

    Then 2 loops and 13 if statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding word frequency in a text file.
    By aeolusaether in forum C Programming
    Replies: 15
    Last Post: 04-04-2010, 09:59 PM
  2. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 11:52 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM