<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>飞扬轻狂&#039;s blog &#187; c++</title>
	<atom:link href="http://blog.fallseir.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fallseir.com</link>
	<description>我心飞扬 我意轻狂</description>
	<lastBuildDate>Thu, 06 May 2010 07:13:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><cloud domain='blog.fallseir.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>谨慎的在c++中使用cin进行交互</title>
		<link>http://blog.fallseir.com/2008/10/note_std_cin/</link>
		<comments>http://blog.fallseir.com/2008/10/note_std_cin/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 09:34:28 +0000</pubDate>
		<dc:creator>飞扬轻狂</dc:creator>
				<category><![CDATA[随笔]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[cin]]></category>
		<category><![CDATA[fallseir]]></category>
		<category><![CDATA[learn]]></category>

		<guid isPermaLink="false">http://blog.fallseir.com/?p=107</guid>
		<description><![CDATA[在使用c++ 进行控制台交互时 也许你也容易忽略掉的细节 &#8211; 飞扬轻狂 20081024 fallseir[at]gmail.com http://blog.fallseir.com/2008/10/note_std_cinnote_std_cin/ 注意1： &#8221; cin >> value &#8221; 没有正确的读取换行 注意2： 不匹配的类型 将导致输入流错误 而在其后的调用中自动返回 注意3： 空字符 在使用 >> 赋值给变量时将自动过滤 直到读入非空字符为止 出错的代码 ！！ $ vim 02-05-BasicIO-err.cpp --------------------------------------------- #include using namespace std; int main( int argc, char *argv[] ){ int num; string str,line; cout > num; cout > str; cout]]></description>
			<content:encoded><![CDATA[<p>在使用c++ 进行控制台交互时 也许你也容易忽略掉的细节</p>
<p>&#8211; 飞扬轻狂 20081024 fallseir[at]gmail.com</p>
<p>http://blog.fallseir.com/2008/10/note_std_cinnote_std_cin/</p>
<p>注意1： &#8221; cin >> value &#8221; 没有正确的读取换行<br />
注意2： 不匹配的类型 将导致输入流错误 而在其后的调用中自动返回<br />
注意3： 空字符 在使用 >> 赋值给变量时将自动过滤 直到读入非空字符为止</p>
<p>出错的代码 ！！</p>
<pre>
$ vim 02-05-BasicIO-err.cpp
---------------------------------------------
#include <iostream>
using namespace std;
int main( int argc, char *argv[] ){
  int num;
  string str,line;

  cout << "Please enter a number:" ;
  cin >> num;
  cout << "Please enter a text :" ;
  cin >> str;
  cout << "Please enter some words :" ;
  getline( cin, line );

  cout << endl;
  cout << "number : " << num << endl;
  cout << "text : " << str << endl;
  cout << "some words : " << line << endl;

  return 0;
}
---------------------------------------------
$ g++ 02-05-BasicIO-err.cpp
$ a.out # 貌似正确的输入
----------------
Please enter a number:12<enter>
Please enter a text :message<enter>
Please enter some words :
number : 12
text : message
some words :
----------------
/*
 分析原因：
 第二次输入 "message" 的时候 自动过滤前次的<enter> 并得到了正确的值
 getline 的时候 因为输入流中还有一个 <enter> 没有被读取 所以没有等待用户输入，直接返回了
 */
--------------------
$ a.out # 貌似正确的结果
----------------
Please enter a number:13 message some words in the line <enter>
Please enter a text <img src='http://blog.fallseir.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> lease enter some words :
number : 13
text : message
some words :  some words in the line
----------------
/*
 分析原因：
 cin >> str 的时候 从流中读取了 "message"
 getline 的时候 读取了流中剩下的内容
 */
--------------------
$ a.out # 错误的开始
----------------
Please enter a number:a
Please enter a text <img src='http://blog.fallseir.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> lease enter some words :
number : 6696948
text :
some words :
----------------
/*
 分析原因：
 cin >> num 的时候 从流中读取了不匹配的数据 "a" ，cin 进入异常状态
 cin >> str 和 getline 的时候 因为流异常 所以没有进行读取
 */
--------------------
</pre>
<p>优化的严谨的代码</p>
<pre>
$ vim 02-05-BasicIO.cpp
---------------------------------------------
#include <iostream>
using namespace std;
int main( int argc, char *argv[] ){
  int num;
  string str,line;
  string tmp;

  cout << "Please enter a number:" ;
  cin >> num;
  while(cin.fail()){ // 如果出现错误
    cin.clear(); // 清除错误
    getline(cin,tmp); // 清除缓存
    cerr << "Error input!!,Please try again" << endl;
    cin >> num;
  }
  getline(cin,tmp); // 使用getline清除缓存，因为 >> 操作符不会对末尾的 <enter> 进行读取 

  cout << "Please enter a text :" ;
  cin >> str;
  getline(cin,tmp);

  cout << "Please enter some words :" ;
  getline( cin, line );
  cout << endl;
  cout << "number : " << num << endl;
  cout << "text : " << str << endl;
  cout << "some words : " << line << endl;

  return 0;
}

---------------------------------------------
----------------
$ a.out
----------------
Please enter a number:123 message test words!
Please enter a text :word and message
Please enter some words :some words in the line!

number : 123
text : word
some words : some words in the line!
----------------
</pre>
<p>-- </p>
<p>http://blog.fallseir.com/2008/10/note_std_cinnote_std_cin/</p>
<p>飞扬轻狂<br />
fallseir[at]gmail.com<br />
blog.fallseir.com<br />
2008年10月24日<br />
转载请注明</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.fallseir.com/2008/10/note_std_cin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
