How cin or scanf works

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

How cin or scanf works

Post by A1 »

Lets i have a code like this:

Code: Select all

#include <iostream.h>

int main()
{
	int x;
	char s[100];

	cin>>x;
	cin>>s;
	cout<<x<<" "<<s<<endl;
	return 0;
}
And my input is:

Code: Select all

123nospace
The Output of this program is:

Code: Select all

123 nospace
Here my question is how cin or scanf() stop reading immediately before a non numeric character when it reads a number?
Does they change input pointer?
(Actually i want to implement this kind of function in java, is it possible?)
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

I belive some body knows some thing about this :-? :roll:
or any links that can help me..
Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post by Moha »

They have a buffer and see the next character.
Also you can see the next character without extracting it from buffer.
Post Reply

Return to “C++”