how to find position of newline in string (getline)

Sep 15, 2014 at 2:34am
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 Sep 15, 2014 at 2:43am
Sep 15, 2014 at 3:09am
I'm confused too. Do you want to know the length of the text that was input?
Sep 15, 2014 at 3:21am
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 :'/)
Sep 15, 2014 at 3:28am
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...
Sep 15, 2014 at 3:32am
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 ;
}
Sep 15, 2014 at 3:38am
i tried doing that, it doesnt find anything when i search for '\n'
Sep 15, 2014 at 3:55am
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 Sep 15, 2014 at 3:56am
Sep 15, 2014 at 3:59am
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?
Sep 15, 2014 at 4:02am
if(input.at(i)=='\n') rot13.append(i,'\n');

i tried this and it and no effect
Sep 15, 2014 at 4:48am
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.
Sep 15, 2014 at 5:29am
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?
Sep 15, 2014 at 5:31am
Post the answer and mark the question as solved.
Topic archived. No new replies allowed.