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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nietodConnect With a Mentor Commented:
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
 
gvijay1Author Commented:
Edited text of question.
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.