about string and getline!!

Hi i am trying to make a program that gets string and divide it by new line which is \n. i noticed that if i assign a string into a variable and run the program it works fine. However if i get input from getline and assign it, the code does not work. anyone knows why??
the code looks like this. I think if i get string from getline function, the if statement can't recognize where '\n' is..!!
and This is not complete code!! i just posted the part i get error!!
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(getline(cin,line)) {
	
        for (int i = 0; i < line.size(); i++) {
		if(line[i] == '\n') {
		str_len = i-init_str;
		lines.push_back(line.substr(init_str,str_len));
		str_len == 0;
		init_str = i+1;
	}
	else if(line[i] != '\n' && i == line.size()-1) {
		lines.push_back(line.substr(init_str));

	}

}
Last edited on
It's because the new line character is discarded be getline(). Read this:

http://www.cplusplus.com/reference/string/getline/
Last edited on
Do you know how to fix this?? I mean is there a way that i can break down the string by \n??
1
2
while ( getline(cin,line) )
    lines.push_back(line) ;

thank you very much!! now i got it!!
Topic archived. No new replies allowed.