Trying to get a complete text from user

Hi!

Does anyone know how to allow the user to input text (I mean, several sentences, a complete message)?

So far, i´ve tryied:

1
2
3
4
5
6
7
char c;
string text;
cin >> c;
while(cin !='/'){ // I tell the user to finish the text with '/'
  text += c;      // last read char is pushed back into the string
  cin >> c;
}


The problem here is that I loose the spacing between words, i.e. user inputs "Hello, how are you?" and string text ends up "Hello,howareyou?".

I´ve also tried using getline

1
2
3
string text;
getline(cin, text);
}


but then I don´t know how to tell the program when the text is finished, when to stop reading.

Summing up, I need a way to input long strings, respecting the format given by the user (spacing).

Thank you!!!
1
2
3
4
5
6
7
8
#include<iostream>
#include<string>

int main()
{
	std::string s;
	std::getline(std::cin,s);
}
Ok, thank you! And if I wanted the user to be able to separate periods? Is there a way?

I´d like to make several getline and then tell the program to go forward only when, for example, user presses some special character... I mean, if I wanted the string to be something like?

Day 1:

We went to the lab and set up everything for the experiment.


How can I make it become only one string?
you want to read newlines in a string?
Yes, and I want a way of moving to the next step, a way to say "This is it, this is all the text I´m going to input, do your work with it".

I would like user´s interaction with the program to be something like this:

Enter destination file:   


you put the name, this is solved...

Enter your text:


you write what ever you want, sentences, words, periods, spaces, "return" to go down, ...
The program then has to do other things that I have already solved.
Using getline(cin, S);, whenever user presses return, the program moves to the next instruction, and maybe I still want to enter some text, but in the next line.--
For example, in this case:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main(){
string line, text;
while (getline(cin, line)){
    text += line;
    getline(cin,line);
}

cout << text;
}


I can get all the lines I want, but the program never gets out of while loop...
dont think there is a command for that.

you could use if, do while or while to check if the input string has a certain character combination at the end. in that case you stop the input.
Now I´ve got this, which is better:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;

int main(){
string line, text;
getline(cin, line);
while (line != "end"){
    text += line;
    getline(cin,line);
}

cout << text;
}



Now I can ask the user to type "end" to finish the writing. Is there a better way?
Last edited on
i don think this will work and compile.
did you try it?
This is even better, now I can "get" every return and make it be part of the string (in a tricky kind of way):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

int main(){
string line, text;
getline(cin, line);
while (line != "end"){
    text += char (10);
    text += line;
    getline(cin,line);
}

cout << text;
}

Now I´m posting and answering, how crazy am I?
Anyway, thank you Darkmaster, if you knew a better command to accomplish this, I´d be glad to learn.
you forgot to include <string>
If you want to use / as the end of the text you can read the whole text using a single call to getline.

getline(cin, text, '/');
@Darkmaster: it works for me without <string>, I was told that <iostream> includes the call to <string>

@Peter: thank you for the tip!!! Then I shouldn´t need the while... that´s what I was looking for.
Topic archived. No new replies allowed.