0

I am trying something like this:

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main()
{

    string inputStr;
    vector <string> strVector;
    cin.getline(inputStr,200);
    int i=0;
    while (inputStr!=NULL){ //unless all data is read.
        strVector[i]=getline(inputStr," ");
        i++;
    }//while.

    for (int j=0; j<strVector.size(); j++){
        cout<< strVector[j];
        cout<<endl;
    }
} //main.

Any one who can help. I am trying to store my input string in vector string and then I can push_back my ith string.

3
  • Try to use push_back method: strVector.push_back(getline(inputStr, " ")); Commented Nov 20, 2012 at 13:56
  • Right... Your syntax is valid for a std::map but not a std::vector. Use push_back. Commented Nov 20, 2012 at 13:57
  • can I ask the apparently not-so-obvious question? what is getline(inputStr," ") doing? If you're looking to simply load a vector with whitespace-seperated strings from cin there are simpler ways to do this. Commented Nov 20, 2012 at 14:03

3 Answers 3

3

Much of your code involving inputString is invalid. There is no getline member of istream that takes a std::string, so this is invalid:

cin.getline(inputStr,200);

What you want there instead is the global getline:

getline(cin, inputStr);

Second, there is no global getline which reads directly from a std::string, so this is invalid:

strVector[i]=getline(inputStr," ");

What you want to use there is an istringstream. Altogether, your code might look something like this:

std::getline(std::cin, inputStr);
std::istringstream iss(inputStr);
std::string word;
// read from the istringstream until failure
while (std::getline(iss,word,' '))
    strVector.push_back(word);

If you want to delimit on whitespace(including tabs) then you can use operator>> instead of getline.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 and/or an istream_iterator around iss with a back_inserter to shove strings into strVector.
@Benjamin Lindley .. can you explain with comments what are your trying to do in 2nd line with istringstream.
@AzkaAhmad: I would, but I have to leave just now. Take a look at this. Or perhaps someone else will explain.
@AzkaAhmad This is pulling a full-line read from (cin), then loading it into a stringstream to extract space-separated content into individual strings that are then pushed into your vector. I hope that made sense. This is what allows you to type multiple words in a single line, hit enter, then have those words split into single strings and collected in your vector.
@WhozCraig Do I need to add a library for this istringstream because in PUTTY I am getting this error "variable âstd::istringstream issâ has initializer but incomplete type"
|
1

I think you are looking for the push_back method of the std::vector template

Comments

0

Here is nother way:

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


using namespace std;



int main(int argc, char *argv[])
{
    vector<string> strVec;
    string str;
    cout<<"Enter # to quit \n\n";
    int i=0;


    while (str!="#")
    {  
        cout<<"Input text No. "<<i+1 <<" here > ";
        cin>>str ;
        strVec.push_back(str);


        i++;
    } 

    cout<<"\nStored text\n----------\n";
    for (int j=0; j<strVec.size()-1; j++)    cout<<j+1<<"  "<< strVec[j]<<"\n";


    cout<<"\n\n";

    return(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.