inputting strings into a character array

Aug 7, 2012 at 8:38am
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	char str[300]="hello world";

	cout<<str<<endl;//outputs "hello world"
	cin>>str;// input "hello motto"
	cout<<str<<endl; // only outputs "hello"
	
	
	return 0;
}


i was wondering why after the input only records the string up until the first space. ive tried various inputs, and only the first word was put into the array.
Last edited on Aug 7, 2012 at 8:45am
Aug 7, 2012 at 8:46am
because the rest gets sent to the buffer, use getline(cin, str) if you want it to capture whitespace
Aug 7, 2012 at 9:17am
when i used cin.getline it worked!

however, i cant seem to have getline () work...

i have the iostream header, declared using std::getline, yet the error states that there is no instance of the overloaded function that matches to the arguments. i looked up the parameters of getline, and they matched the ones you put. so i dont understand why it wont work.
Aug 7, 2012 at 9:20am
There is no such overloaded function for character arrays. It uses objects of type std::string. So with character array you should use cin.getline.
Last edited on Aug 7, 2012 at 9:21am
Aug 7, 2012 at 9:23am
ohhh i get it now. Thank you guys so much!
Aug 7, 2012 at 10:32am
Ah I didn't know that.
Topic archived. No new replies allowed.