CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2003
    Location
    Manila
    Posts
    82

    put std string into array

    Hello. I need your help on placing std strings into array.

    I used the following in declaring array of string but none of them seem to work. I have 10 std strings and I want to put them in an array.



    Code:
    string mystringcollection[10]; //I don't know which of the following
    char *mystringcollection[10]; //is the correct declaration
    Also how do I go back, or read in a part in file after the while statement below. Because when I try to read in a line after the while statement, it doesn't read in anything ( I think it has already reached the end of file) but I need to get access to that part of the file.


    Code:
    	while (getline(infile,line)) { //get next line of input file
    	//search for something in file and do something
    }
    //go back to next part of file
    getline(infile, line); outfile<<line; //outputs nothing
    Thanks in advance for your suggestion/help.

  2. #2
    Join Date
    Nov 1999
    Location
    Copenhagen, Denmark
    Posts
    265
    Why do you want to put your std::strings into an array? Why not use one of the STL container classes? Anyway, declaration number one will probably do the trick:

    Code:
    	
        std::string ar[2];	
        ar[1] = "Hello";
    Best regards,
    S. Bro

    "I would be happy to deal with my problems one at the time if they would only line up!"
    -Paul Cilwa, "Borland C++ Insider".

    Other useful fora some of which I ruthlessly clipboarded from other peoples footers.

    MSDN: http://search.microsoft.com/us/dev/default.asp
    WIN 32 Assembler: http://board.win32asmcommunity.net/
    RDBMS: http://www.dbforums.com
    Robert's Perl Tutorial: http://www.sthomas.net/roberts-perl-tutorial.htm
    Merriam-Webster Online: http://www.m-w.com/home.htm
    Writing Unmaintainable Code: http://mindprod.com/unmain.html

  3. #3
    Join Date
    Jul 2003
    Location
    Manila
    Posts
    82
    Thanks sbrothy for your help. I decided to put my strings into a vector.

    Anyway I got stuck in another problem in my code. I need to construct a loop that does something until program finds "loc" character. Here's the code.



    Code:
    getline(infile,line);
    int i=0;
    string::size_type locpos = line.find(loc[i]); //search for "loc" string in line
    while () { //do this until find character "loc"		
    	int i=0;
    	string::size_type calpos = line.find(cal[i]); /look for "cal" string in line	
            if(calpos != std::string::npos) {
    		string calext(".CAL"); 
    		string calfile = line.substr(0, calpos)+calext; //add .cal to string filename
    		calvec.push_back(calfile); //the part where I store strings inside vector
    	}
    }
    Last edited by ambc; July 8th, 2003 at 11:17 PM.

  4. #4
    Join Date
    Jul 2003
    Location
    Manila
    Posts
    82
    I figured it out. To those who may encounter the same problem
    Code:
    string::size_type locpos;
    	do {
    //do something
    	}while (locpos == std::string::npos);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured