Multiple lines of input

Lets say that the user types the below lines of input in a prompt..

a
KB
One horse sledge
Merry Christmas

The number of input lines can vary and I need to be able to read all input lines and store them in different strings (the first will always be a 1 character input). Also, the line after the last input will be blank to signal the end of the input process. How do I detect this and read the lines of input? Please let me know how to do this.

gvijay1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gvijay1Author Commented:
Edited text of question.
0
nietodCommented:
I would use STL string templates to store the line, since they will resize to store any length line and I would use an STL vector or list class to store the strings since these will resize to store any number of lines.

continues
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:

vector<string> Text; // All the text read.
string CurLin; // Current line.

while (true)
{

    getline(cin,CurLin); // Get the next line.
    if (Text.length() == 0) // If the line is empty, then
      break; // Stop reading the text.
    Text.push_back(CurLin); // Append the line to the end of the container.
}

Let me know if you have any questions.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

gvijay1Author Commented:
Dear Expert,
           I tried out your example. However, it was not working. I might not be including the correct libraries which could be the cause of the errors. Is there any other way to do this?? I need to store the various input lines of the user into an array of strings for me to analyze.
0
nietodCommented:
It would help[ if you posted the code you used and/or the errors you got.

Yes, there are other ways to do this, many other ways.  However, at the moment this seems like the best way.  There may be reasons why other approaches might be better, but you haven't provided any information that might suggest this.

For the code i proposed, You would need to include the <string> and the <iostream> header files.  Also if your compiler supports namespaces you would have to specify "std::" before each identifiter from these libraries, which tends to be painful.  So it might be better to use a using declaration, like

#include <string>
#include <iostream>

using namespace std;

// the code I suggested.
0
gvijay1Author Commented:
Dear Expert,
                   Here is the code..

#include <iostream>
#include <string>

using namespace std;

main()
{
vector<string> Text;
string CurLin;

cout << "Enter the info"<<endl;

while(true)
{

      getline(cin,CurLin);
      if (Text.length()==0)
      break;
      Text.push_back(CurLin);
}

return 0;

}

This is the part that does not work. Also,  how do I access the elements in the container after they have been added to it. Please let me know.
0
nietodCommented:
What part doesn't work?  in what way doesn't it work?  Please be precise.  Does it not compile?  Does it compile but not run right?

When I try to compile it, I find that <vector> is needed and that the if() has the wrong variable in it, try

#include <iostream>
#include <string>
#include <vector>

using namespace std;

main()
{
vector<string> Text;
string CurLin;

cout << "Enter the info"<<endl;

while(true)
{

getline(cin,CurLin);
if (CurLin.length()==0)
break;
Text.push_back(CurLin);
}

return 0;

}
0
gvijay1Author Commented:
Dear Expert,
           Sorry for the confusion. Thanks for the response. Accpreciate your answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.