question about istream

This is the code i'm playing with.


1
2
3
4
5
6
7
8
9
10
//---------------first part ---------------
	char s [200];
	cout << "enter a string " << endl;
	cin >> s;
	cout << s << endl;
//--------second part ----------------------
	char str [200];
	cout << "enter a line of string " << endl;
	cin.getline(str, 199);
	cout << str << endl;


if i just have the first part OR if i just have the second part, it works perfectly.

But if i have both parts. When the program is run, the top part get run and whatever string i enter get echoed. then the prompt appears but i am not given a chance to enter the line. Is there a reason for this???

Also, right now, i find input/output (especially input) very confusing and "magically" (ie. i don't understand why it works or doesn't work). Is there a book or a tutorial that explains this well without going too deep into implementation or the "hardware stuff"?

Last edited on
operator >> leaves trailing white spaces and newlines.
getline reads until the next newline character ( or whatever you choose )
If you don't remove the newline left by >>, getline will read an empty string
BTW you should better use std::strings instead of char arrays

To understand better the iostream library, you can check the reference: http://www.cplusplus.com/reference/iostream/
Topic archived. No new replies allowed.