Thread: Basic data processing

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Basic data processing

    I am very new to C++, or any kind of programming for that matter. I am having difficulty figuring out how to read and process specific data within an input file. Some values that I need to read are in the middle of the file or line.

    Here is the input file I am working with. The file is from a hand history file playing blackjack online:

    Stage # B506558848 - Blackjack 2009-02-13 14:51:22 (ET)
    ********** ( $30.00 in chips )
    -----------------------------------------------------------------------
    Hand 1-1: Bet $0.00 + Side Bet $0.00
    Hand 2-1: Bet $5.00 + Side Bet $0.00
    Hand 3-1: Bet $0.00 + Side Bet $0.00
    -----------------------------------------------------------------------
    Dealt to Hand 2-1: [ 5h 2s ]
    Dealt to Dealer: [ 2s H ]
    -----------------------------------------------------------------------
    Hand 2-1: HIT
    Dealt to Hand 2-1: [ 8s ]
    Hand 2-1: STAND
    -----------------------------------------------------------------------
    Dealer: Hit
    Dealt to Dealer: [ 3c ]
    Dealer: Hit
    Dealt to Dealer: [ 5s ]
    Dealer: Hit
    Dealt to Dealer: [ Ks ]
    -----------------------------------------------------------------------
    *** RESULT ***
    Dealer [ 2s 3c 5s Ks ] (20)
    Total Bet( $5.00 ), Win/Loss( -$5.00 )
    Hand 2-1 [ 5h 2s 8s ] (15) - Loses $5.00

    What I am trying to accomplish is to have the program determine whether the hand was a win, a loss, or a push. The last line contains the value "Loses", which I want to use as the value to determine a win, loss, push. I was thinking of using the ignore function, but a string value cannot be used as a parameter. If anyone could point me in the right direction I'd appreciate it!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's something that you may want to try:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        // You could open a file and read from it . . . I use cin for simplicity.
    
        string line;  // the line we've read from the file
        while(getline(cin, line)) {
            if(line.find("Loses") != string::npos) {
                cout << "--- A loss!\n";
            }
        }
    
        return 0;
    }
    That code just reads in lines of text and searches each line for a specific string.

    When I put your data into loss.txt and run the program, the output is:
    Code:
    $ ./loss < loss.txt
    --- A loss!
    $
    If you want to do more than this, e.g., figure out how much was lost, you could use a stringstream to parse the line that contained the word "Loses". Or you could save the return value of the find() function (it indicates the point in the line at which the text was found), and then read characters from there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    sscanf() is an option, too, if you need something a little bit more flexible.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Thanks, I will try those suggestions out! I'll have to do reading on how to use sscanf. I've never heard of it before. I appreciate the help.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    OK,
    so here's what I have so far.

    Code:
    #include "header.h"
    
    using namespace std;
    
    	//Variables
    ifstream infile;
    ofstream outfile;
    char file[101];
    string line;
    int w = 0;
    int l = 0;
    int p = 0;
    
    	//Main Program
    int main()
    {
    	//Open files
     cout << "Please enter the location of the";
     cout << " history.";
     cout << endl;
     cin >> file;
     infile.open(file);
     while (!infile)
     {
    	 cout << "Cannot open the input file.  Please re-enter."; 
    	 cout << endl;
    	 cin >> file;
    	 infile.open(file);
     }
    	 
     outfile.open("results.txt");
    
     while (getline(infile, line))
     {
    	 if (line.find("Collects" != string::npos)
    		 w++;
    	 if (line.find("Loses" != string::npos)
    		 l++;
    	  if (line.find("Push" != string::npos)
    		 p++;
     }
    
     outfile << "Wins = " << w << endl;
     outfile << "Losses = " << l << endl;
     outfile << "Pushes = " << p << endl;
    
     infile.close();
     outfile.close();
    	
     return 0;
    }
    I'm getting this error when I debug, and like I said I am still learning and I have no clue what it's trying to tell me.

    ***\processor.cpp(35) : error C2446: '!=' : no conversion from 'const unsigned int' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    .\processor.cpp(35) : error C2040: '!=' : 'const char [9]' differs in levels of indirection from 'const unsigned int'***

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    if (line.find("Collects" != string::npos)
    You're missing closing parentheses.
    Code:
    if (line.find("Collects") != string::npos)
    A word of advice: don't call a variable "l" (lowercase L).

    Also:
    Code:
     cout << "Please enter the location of the";
     cout << " history.";
     cout << endl;
     cin >> file;
     infile.open(file);
     while (!infile)
     {
    	 cout << "Cannot open the input file.  Please re-enter."; 
    	 cout << endl;
    	 cin >> file;
    	 infile.open(file);
     }
    Consider a do-while loop, that's the kind of situation they're meant for. (Unless you want a different message each time, I suppose.)

    Finally, there's no need to make all of your variables global.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  4. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM