how to do a simple loop to store strings ??

Discussion in 'C++' started by news.hku.hk, Apr 25, 2004.

  1. news.hku.hk

    news.hku.hk Guest

    is there any way to make a loop to store strings??

    e.g. abc.txt contains 3 lines i.e.
    this is line 1.
    this is line 2.
    this is line 3.

    i want to create a loop that can make the three strings contains the
    corresponding lines,

    string line1; //contains "this is line1."
    string line2; // contains "this is line2."
    string line3; // contains "this is line3."
    int no_of_lines = 3;

    can a simple for loop do this ??
    thanks a lot
    news.hku.hk, Apr 25, 2004
    #1
    1. Advertising

  2. news.hku.hk

    Karthik Guest

    Read "Dynamic memory allocation" in any standard C++ book.
    To be more specific , new - delete should do it for you.

    If you donno in advance the number of lines, STL(Standard Template
    Library) should prove useful - vector , list should do it for you.

    HTH

    news.hku.hk wrote:

    > is there any way to make a loop to store strings??
    >
    > e.g. abc.txt contains 3 lines i.e.
    > this is line 1.
    > this is line 2.
    > this is line 3.
    >
    > i want to create a loop that can make the three strings contains the
    > corresponding lines,
    >
    > string line1; //contains "this is line1."
    > string line2; // contains "this is line2."
    > string line3; // contains "this is line3."
    > int no_of_lines = 3;
    >
    > can a simple for loop do this ??
    > thanks a lot
    >
    >


    --
    Karthik

    ------

    Humans please 'removeme' for my email.
    Karthik, Apr 25, 2004
    #2
    1. Advertising

  3. news.hku.hk

    Carl Muller Guest

    "news.hku.hk" <> wrote in message news:<408b5af2$>...
    > is there any way to make a loop to store strings??
    >
    > e.g. abc.txt contains 3 lines i.e.
    > this is line 1.
    > this is line 2.
    > this is line 3.
    >
    > i want to create a loop that can make the three strings contains the
    > corresponding lines,
    >
    > string line1; //contains "this is line1."
    > string line2; // contains "this is line2."
    > string line3; // contains "this is line3."
    > int no_of_lines = 3;
    >
    > can a simple for loop do this ??
    > thanks a lot


    Individually named string variables might not be the easiest way of
    processing a file. The usual solution for that would be to have a
    vector of strings. If you want to use a for loop and named variables,
    you could use an array of pointers to the strings (now I wonder if you
    can have an array of references, and how you bind them!). But that is
    having the tail wagging the dog - it might be more usual to have an
    array of strings, and then have named references bound to the elements
    of the array.
    Carl Muller, Apr 25, 2004
    #3
  4. news.hku.hk

    Jeff Schwab Guest

    news.hku.hk wrote:
    > is there any way to make a loop to store strings??
    >
    > e.g. abc.txt contains 3 lines i.e.
    > this is line 1.
    > this is line 2.
    > this is line 3.
    >
    > i want to create a loop that can make the three strings contains the
    > corresponding lines,
    >
    > string line1; //contains "this is line1."
    > string line2; // contains "this is line2."
    > string line3; // contains "this is line3."
    > int no_of_lines = 3;
    >
    > can a simple for loop do this ??


    Yes. Look up the std::getline function.
    Jeff Schwab, Apr 25, 2004
    #4
  5. news.hku.hk

    Jon Bell Guest

    In article <408b5af2$>,
    news.hku.hk <> wrote:
    >is there any way to make a loop to store strings??
    >
    >e.g. abc.txt contains 3 lines i.e.
    >this is line 1.
    >this is line 2.
    >this is line 3.
    >
    >i want to create a loop that can make the three strings contains the
    >corresponding lines,
    >
    >string line1; //contains "this is line1."
    >string line2; // contains "this is line2."
    >string line3; // contains "this is line3."
    >int no_of_lines = 3;


    You can't do this with individually named string variables, but you can
    do it with a vector of strings:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main ()
    {
    ifstream inputFile ("abc.txt");
    vector<string> linevec;

    string line;
    while (getline (inputFile, line))
    {
    linevec.push_back (line);
    }

    for (int k = 0; k < linevec.size(); ++k)
    {
    cout << linevec[k] << endl;
    }

    return 0;
    }

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Apr 25, 2004
    #5
  6. news.hku.hk wrote:
    > is there any way to make a loop to store strings??
    >
    > e.g. abc.txt contains 3 lines i.e.
    > this is line 1.
    > this is line 2.
    > this is line 3.
    >
    > i want to create a loop that can make the three strings contains the
    > corresponding lines,
    >
    > string line1; //contains "this is line1."
    > string line2; // contains "this is line2."
    > string line3; // contains "this is line3."
    > int no_of_lines = 3;
    >
    > can a simple for loop do this ??


    Sure!

    #include <sstream>
    #include <string>
    using namespace std;

    string int_to_str(int i) {
    ostringstream o;
    o << i;
    return o.str();
    }

    int main() {
    const int no_of_lines=3;
    string lines[no_of_lines];
    for(unsigned int i=0;i<no_of_lines;++i)
    lines="this is line"+int_to_str(i);
    }


    .... except that you now have lines[0],lines[1],lines[2]
    instead of line1, line2,line3.

    HTH,
    - J.

    PS. What you really need is a textbook.
    Jacek Dziedzic, Apr 25, 2004
    #6
  7. Jacek Dziedzic wrote:

    >> e.g. abc.txt contains 3 lines i.e.


    Oops, missed that line and never noticed you wanted
    these from a file, sorry.

    - J.
    Jacek Dziedzic, Apr 25, 2004
    #7
  8. "news.hku.hk" <> wrote in message
    news:408b5af2$...
    > is there any way to make a loop to store strings??
    >
    > e.g. abc.txt contains 3 lines i.e.
    > this is line 1.
    > this is line 2.
    > this is line 3.
    >
    > i want to create a loop that can make the three strings contains the
    > corresponding lines,
    >
    > string line1; //contains "this is line1."
    > string line2; // contains "this is line2."
    > string line3; // contains "this is line3."
    > int no_of_lines = 3;
    >
    > can a simple for loop do this ??
    > thanks a lot
    >


    You can't do it in a loop, but why would you want to? Like this

    getline(file, line1);
    getline(file, line2);
    getline(file, line3);

    john
    John Harrison, Apr 25, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan
    Replies:
    1
    Views:
    464
  2. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    634
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  3. Gyro
    Replies:
    1
    Views:
    377
    Darryl Pierce
    Feb 26, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    754
    Malcolm
    Jun 24, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page