input data from file into string

This is a discussion on input data from file into string within the C++ Programming forums, part of the General Programming Boards category; hi everyone, how can i input the data tat is from input file stream... this is the code tat i ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    input data from file into string

    hi everyone, how can i input the data tat is from input file stream...

    this is the code tat i am doing now ... but have an error.. please let me know and correct for me if someone know the problem... thank you

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    
    
    int main()
    {
    //declare numRows, numColumns and string
    
        const int numRows = 6;
        const int numColumns = 3;
        string hands[numRows][numColumns]; 
        
    //initialise the string and assign each to 'X'
          
        for(int row = 0; row < numRows; row++)
        {
            for(int col = 0; col <numColumns; col++)
            {
                hands[row][col] = 'x';
            }
        }    
        
    //input data from the input.txt and display the string
        
        ifstream myfile ("input.txt");
        if(!myfile)
        {
            cout<<"Cannot open file."<<endl;
            return 1;
        }
            
        for(int row = 0; row < numRows; row++)
        {
            for(int col = 0; col <numColumns; col++)
            {
                myfile>>hands[row][col];
            }
            cout<<endl;
        }
        myfile.close();           
                
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Yes the problem is that you are initializing a string array with variables. You can only do this if the memory is dynamically allocated. Also, const doesn't really mean what the keyword implies; it merely prevents assignments to something new but if you get creative, you can defy const.

    It would have been better just to call const read only but I digress.
    Last edited by whiteflags; 05-02-2006 at 01:25 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by peter_hii
    but have an error..
    What error are you getting? Also, you should be including the <string> header if you wish to use string objects.


    Quote Originally Posted by citizen
    Yes the problem is that you are initializing a string array with variables. You can only do this if the memory is dynamically allocated.
    Huh? There is no problem with initializing a string array with variables. The whole string container would be pretty useless if you could not initialize it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    hands[row][col] = 'x';
    'x' is not a string, just one character.
    Try double qutes instead
    Code:
    hands[row][col] ="x";
    and
    Code:
    #include <string>
    as well
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    well thank you for the problem tat had being listed out. thank you vvery much
    but the problem now is the text would not display out on the screen

    i want those string from input.txt display on each array. but it come out with blank....

    for example:

    string hands[6][3]; // i assign it as X, so it looks like below.

    X X X
    X X X
    X X X
    X X X
    X X X
    X X X

    in my input.txt, i would make it looks like this when the program compile.

    2C 2S 2D
    AH AC AS
    3C 3S 3D
    X X X
    X X X
    X X x

    please help me with my code.
    Attached Files Attached Files
    Last edited by peter_hii; 05-02-2006 at 10:44 AM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by peter_hii
    in my input.txt, i would make it looks like this when the program compile.
    Don't understand what you're saying.
    But maybe you should add some output statement.

    Code:
       for(int row = 0; row < numRows; row++) {
            for(int col = 0; col <numColumns; col++) {
                myfile>>hands[row][col];
                cout<< hands[row][col] << " ";
            }
            cout<<endl;
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thank you. it is the exact way i want...appreciate it

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i got stuck one more time that..

    well, my program called commerce which is European card games.
    this is the rules:

    TRIPLETS: a combinations of three cards of the same value. three Aces is the highest, and three 2s the lowest. (pair have no value in this game).

    SEQUENCE Flushes: a combination of cards, of the same suit , in a sequence; for example 7,8,9 of hearts. Aces can rank as the highest or lowest card, for example, A, K, Q or Diamonds or A,2,3 of spades

    The Point: the greatest point value of two or three cards of the same suit in one hand, counting Ace as eleven and the other court cards as ten each. A single card of a suit does not count for the point.

    NOTE: the best triplet wins the game. if no tripet is present, the best straight flush wins. if there is no straight flush, the best point wins.

    input:

    2C 2S 2D
    AH AC AS
    3C 3S 3D


    my output:

    player two is the winner..

    this is the whole code
    Code:
    /*******************************************************************************
    Commerce games(commerce.cpp)                                                   
    This program will analyse a number of a commerce hands stored in a text        
    file and determine which of the given hands is the winning hand.               
    
    ********************************************************************************/
    
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    //declare numRows, numColumns and string
    
        const int numRows = 6;
        const int numColumns = 3;
        string hands[numRows][numColumns]; 
        
    //initialise the string and assign each to 'X'
          
        for(int row = 0; row < numRows; row++)
        {
            for(int col = 0; col <numColumns; col++)
            {
                hands[row][col] = "X";
            }
        }    
         
    //input from input.txt and display
    
        ifstream myfile ("input.txt");
        if(!myfile)
        {
            cout<<"Cannot open file."<<endl;
            return 1;
        }
            
        for(int row = 0; row < numRows; row++)
        {
            for(int col = 0; col <numColumns; col++)
            {
                myfile>>hands[row][col];
                cout<< hands[row][col] << " ";
            }
            cout<<endl;
        }
        myfile.close(); 
    
    //find the triplet
        
        for(int row = 0; row < numRows; row++)
        {
            for(int col = 0; col <numColumns; col++)
            {
                //still thinking what should be here..
            }
        } 
                
        system("PAUSE");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Sorting data retrieved from input file
    By Minds_I in forum C Programming
    Replies: 3
    Last Post: 07-20-2003, 07:25 PM

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