I am trying to separate a input string by \n !
I used getline function to get input.
e.x if I type "I am going to school\ntomorrow", I want to separate this into two string, I am going to school and tomorrow.
This is the code to separate the part!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
while(getline(cin,line)) {
if(line == "EOF") break;
for (int i = 0; i < line.size(); i++) {
if(line[i] == a) {
str_len = i-init_str;
lines.push_back(line.substr(init_str,str_len));
str_len == 0;
init_str = i+1;
}
elseif(line[i] != a && i == line.size()-1) {
lines.push_back(line.substr(init_str));
}
I asked yesterday about this because I wanted to know why I can't check if new line character \n in string or not...someone said because the getline function discard the new line character...but he did not mention how to solve it..!!
I tried to find how to solve this for 2 days and wrote the code over and over again but could not solve it..anyone have any idea??
This is my project to make a program that breaks down the string into set of words and count the number of words..!!I wrote the code to count the words. so this is only part I have to solve!!!!!
std::string s( "I am going to school\ntomorrow" );
std::istringstream is( s );
std::string t;
while ( std::getline( is, t ) ) std::cout << t << std::endl;
The loop will iterate two times. In the first iteration t will get vaue
I am going to school
In the second iteration t will get value
tomorrow
Is it what you want?
If you include a vector you can rewrite the loop the following way
1 2 3 4
std::vector<std::string>> sentences;
...
while ( std::getline( is, t ) ) sentences.push_back( t );
Thanks.. your code works now!!
but i actually have to get input from the keyboard and i dont know how the istrinstream works!!
I tried to run my code after include the sstream...but it was same!!
if i type "I am going to school\ntomorrow" the program does not separate the string!!
I think if i type it the input by using getline function does not \n as a delimiter.
can u tell me how to do this??
and tried to print out by using for loop!!
lets say if I type "I am going to school\ntomorrow"
it prints out "I am going to school\ntomorrow"
There is no '\n' in the strings in the vector. The only way a '\n' appears in the output is if you insert it yourself into the output stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <vector>
#include <string>
#include <iostream>
int main()
{
std::vector<std::string> lines ;
std::string line ;
const std::string sentinel = "stop" ;
while ( std::getline(std::cin, line) && line != sentinel )
lines.push_back(line) ;
for ( unsigned i=0; i<lines.size(); ++i )
std::cout << lines[i] << ' ' ;
}
When I go to the
loo, sometimes I take my
own toilet paper and sometimes
I just take my chances.
stop
When I go to the loo, sometimes I take my own toilet paper and sometimes I just take my chances.
I read my HW question again and I was misunderstood the question!!
I thought I have get a input with \n and break the string but it was not!!
Thank you very much about your answer!!
I appreciate it!!