954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with strings

how can a user be able to type what ever he wants such as " i went to the park...", i tried to do it but with my string variable it only read the first word typed in, such as i in this case.

please help.

Mayank23
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Use getline().

Here's an example of usage.

http://www.cplusplus.com/reference/string/getline/

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

thank you for the reply, i ahve another question to it, how can i get a prargraph or more than one line?

Mayank23
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

call the function multiple times.

If you are reading the input from a file, you can run a while loop that checks for EOF.

If you are reading from user input, make a sentinel to end the while loop.

while(string_name != "Done"){ // or change the string to whatever you want, "Exit" "Finished", "0"

}

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 
/*
	Purpose: Have user input lines of code until sentinel value has been found. Output results
	Name: Saith
	Date: 2/4/11
*/

#include<iostream>
#include<string>
using namespace std;


int main() {
	
	string line, paragraph;		// strings are initialized as empty, no need to assign them.
	
	while(line != "done"){
		paragraph += line;
		cout << "Enter newest line.\n";
		getline(cin, line);
	}
	cout << paragraph;

	return 0;
}


Here is a sample that I just created using user input.

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

thanks a lot i really appreciate it.

Mayank23
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: