inputting strings into a character array

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
because the rest gets sent to the buffer, use getline(cin, str) if you want it to capture whitespace
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.
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
ohhh i get it now. Thank you guys so much!
Ah I didn't know that.
Topic archived. No new replies allowed.