how to find position of newline in string (getline)

Write your question here.
I need to find the position where enter where pressed to get newline.
and somehow get that position out as an integer and put it in a vector or some sort of container to use later. (insert newlines in some other string).
If anyone can please help.
1
2
3
4
5
6
7
vector<int> space;
int pos;
/*getline to get the input
the input breaks at a empty line*/
  for(pos=0;pos<input.size();pos++){
        if(input.at(pos)=="\n")  space.push_back(pos);
        }

what way can this be achieved?
I am confused. not even sure if its possible.
Last edited on
I'm confused too. Do you want to know the length of the text that was input?
I am trying to write a decipher code.
my code should encrypt the input by the user and return the reversed value.

one part of the code makes the encryption and creates the new string, but that string does not have any new lines. all the inputs comes inside one line.

so i was thinking if i could find the position of the newlines in the input string and just insert a newline in the new string with
 
string_name.insert(pos_newline,'\n')

I dont know if i should put everything i did. i think that would be alot to look at. ( and also my coding is extremely basic :'/)
http://www.cplusplus.com/reference/string/string/find/

You can find the position of the first \n, save it, then find the position of the first \n after that \n, save it...
so far this is what i came up with, it does the job a little bit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(){
    string input;//user input
    string rot13;//rotated output string
    cout << "Enter the source text: \n<" ;
    while(getline(cin,input)){
        if (input=="") break; //it breaks when entered an empty input
        for (size_t i = 0; i < input.size(); ++i) {
                    if (isalpha(input[i])) {
                        if (input[i] >= 'a' && input[i] <= 'm')// for the first 13 letter add 13 (a-m become n-z)
                            rot13.append(1, input[i] + 13);
                        else if (input[i] >= 'n' && input[i] <= 'z')//for the last 13 letter subtract 13 (n-z become a-m)
                            rot13.append(1, input[i] - 13);
                    }
                    else rot13.append(1, input[i]); //non alphabetic input is unchanged
               }
            }
    for (string::reverse_iterator it=rot13.rbegin(); it!=rot13.rend(); ++it) //this reverses the output
    	cout<< *it ;
}
i tried doing that, it doesnt find anything when i search for '\n'
getline() gets everything up to, but not including, a newline character.

Add rot13 += '\n'; between lines 21 and 22.
(or rot13.append(1,'\n'); if you prefer)

This will unfortunately adds an extra newline at the end, but you can delete that.
Last edited on
Add rot13 += '\n'; between lines 21 and 22.
(or rot13.append(1,'\n'); if you prefer)


but the new line has to be in specific position, like when the user put the new line. how do i make c++ realize that?
if(input.at(i)=='\n') rot13.append(i,'\n');

i tried this and it and no effect
The problem is that when you get a string using getline, it does not store the \n character in the string. It just discards it, so you have to add it back yourself if you want to keep it.
i finally got it working.

i am kinda new to this forum.

so if i find the answer that i was looking for do i have to post it here??

or just archive this thread?
Post the answer and mark the question as solved.
Topic archived. No new replies allowed.