﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-rdu-cpp-随笔分类-论坛拾珠</title><link>http://www.cppblog.com/rdu-cpp/category/9415.html</link><description>杨柳不折</description><language>zh-cn</language><lastBuildDate>Thu, 22 Jan 2009 14:37:15 GMT</lastBuildDate><pubDate>Thu, 22 Jan 2009 14:37:15 GMT</pubDate><ttl>60</ttl><item><title>dangerous c-styled func: gets</title><link>http://www.cppblog.com/rdu-cpp/archive/2009/01/21/72422.html</link><dc:creator>rdu</dc:creator><author>rdu</author><pubDate>Wed, 21 Jan 2009 07:37:00 GMT</pubDate><guid>http://www.cppblog.com/rdu-cpp/archive/2009/01/21/72422.html</guid><wfw:comment>http://www.cppblog.com/rdu-cpp/comments/72422.html</wfw:comment><comments>http://www.cppblog.com/rdu-cpp/archive/2009/01/21/72422.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rdu-cpp/comments/commentRss/72422.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rdu-cpp/services/trackbacks/72422.html</trackback:ping><description><![CDATA[<font size=2>如果您使用cin來取得使用者的輸入字串，則您會發現輸入字串時中間不能包括空白，如果您想要在輸入字串時包括空白，您必須使用gets()函式，例如： <br></font>
<div style="MARGIN-LEFT: 40px"><small><span style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">char str[80]; </span><br style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><span style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">cout &lt;&lt; "輸入字串："; </span><br style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><span style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">gets(str); </span><br style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><span style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">cout &lt;&lt; "輸入的字串：" &lt;&lt; str &lt;&lt; endl;</span><br></small></div>
<small><br>使用gets()函式時有一點必須注意，就是它並不會檢查使用者的輸入是否超出字元陣列的長度，使用時必須小心，有的編譯器會提示警告訊息。 <br></small>
<div style="MARGIN-LEFT: 40px"><small><span style="FONT-WEIGHT: bold; COLOR: rgb(255,0,0); FONT-FAMILY: Courier New,Courier,monospace">: the `gets' function is dangerous and should not be used.</span><br></small></div>
<small><br>对于代码：
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img id=Codehighlighter1_11_88_Open_Image onclick="this.style.display='none'; Codehighlighter1_11_88_Open_Text.style.display='none'; Codehighlighter1_11_88_Closed_Image.style.display='inline'; Codehighlighter1_11_88_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_11_88_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_11_88_Closed_Text.style.display='none'; Codehighlighter1_11_88_Open_Image.style.display='inline'; Codehighlighter1_11_88_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_11_88_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_11_88_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">input&nbsp;a&nbsp;string:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;gets(str);<br></span><span style="COLOR: #008080">6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;puts(str);<br></span><span style="COLOR: #008080">7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<br>在VS 2005中，如果输入了一个长度大于8（9-1）的字符串，VS 2005在编译期不给出上述警告，但是在执行完毕后会跳出对话框提示：<br>Run-Time Check Failure #2 - Stack around the variable 'str' was corrupted.</small> 
<img src ="http://www.cppblog.com/rdu-cpp/aggbug/72422.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rdu-cpp/" target="_blank">rdu</a> 2009-01-21 15:37 <a href="http://www.cppblog.com/rdu-cpp/archive/2009/01/21/72422.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ungetc的用法</title><link>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72396.html</link><dc:creator>rdu</dc:creator><author>rdu</author><pubDate>Tue, 20 Jan 2009 13:54:00 GMT</pubDate><guid>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72396.html</guid><wfw:comment>http://www.cppblog.com/rdu-cpp/comments/72396.html</wfw:comment><comments>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rdu-cpp/comments/commentRss/72396.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rdu-cpp/services/trackbacks/72396.html</trackback:ping><description><![CDATA[<br><br>
<dl class=code>
<dt>C/C++ code
<dd>
<pre>
<div><!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><span style="COLOR: #000000">ungetc　　函数名: ungetc
　　功 能: 把一个字符退回到输入流中
　　用 法: </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> ungetc(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> c, FILE </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">stream);
　　输入参数：c 要写入的字符，stream 文件流指针
　　输出参数：字符c </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> 操作成功，EOF </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> 操作失败
　　程序例:
　　#include &lt;iostream&gt;
&nbsp;&nbsp;&nbsp;using namespace std;
　　</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main( </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> )
　　{
　　</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;
　　</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> ch;
　　puts(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">Input an integer followed by a char:</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">);
　　</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"> read chars until non digit or EOF </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">
　　</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">((ch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> getchar()) </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> EOF </span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000"> isdigit(ch))
　　i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">10</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> ch </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">48</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"> convert ASCII into int value </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">
　　</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"> if non digit char was read, push it back into input buffer </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">
　　</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (ch </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> EOF)
　　ungetc(ch, stdin);
　　printf(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">i = %d, next char in buffer = %c\n</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">, i, getchar());
　　</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;
　　} </span></div>
</pre>
</dd></dl>
<img src ="http://www.cppblog.com/rdu-cpp/aggbug/72396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rdu-cpp/" target="_blank">rdu</a> 2009-01-20 21:54 <a href="http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>位拷贝和值拷贝</title><link>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72393.html</link><dc:creator>rdu</dc:creator><author>rdu</author><pubDate>Tue, 20 Jan 2009 13:05:00 GMT</pubDate><guid>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72393.html</guid><wfw:comment>http://www.cppblog.com/rdu-cpp/comments/72393.html</wfw:comment><comments>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72393.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/rdu-cpp/comments/commentRss/72393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rdu-cpp/services/trackbacks/72393.html</trackback:ping><description><![CDATA[<table class="item reply" cellSpacing=1 csdnid="reply_53439044">
    <tbody>
        <tr>
            <td class=info>
            <dl>
            <dd class=icon><a title=进入用户个人空间 href="http://hi.csdn.net/chenxiong115" target=_blank><img onmouseover="javascript:try{showUserCard(event,'chenxiong115');}catch(ex){}" alt=进入用户个人空间 src="http://avatar.profile.csdn.net/2/F/A/2_chenxiong115.jpg"></a>
            <dd><a href="http://webim.csdn.net/AddFriends/chenxiong115.ashx" target=_blank>加为好友</a>
            <dd><a href="http://hi.csdn.net/guest/profile/post/send/chenxiong115.html" target=_blank>发送私信</a>
            <dd><a href="http://webim.csdn.net/Messages/chenxiong115.ashx" target=_blank>在线聊天</a></dd></dl>
            <ul>
                <li class=username onmouseover="javascript:try{showUserCard(event,'chenxiong115');}catch(ex){}"><img alt="" src="http://webim.csdn.net/UserOnlineFace/Theme1/chenxiong115.ashx"> <a href="http://hi.csdn.net/chenxiong115" target=_blank><var id=topicUserName csdnid="replyUsername">chenxiong115</var></a>
                <li class=nickname><textarea readOnly csdnid="replyNickname">青雄雄</textarea>
                <li class=grade>等级:<img class="grade user1" alt="" src="http://topic.csdn.net/u/ui/images/blank.gif" csdnid="replyUserRank">
                <li csdnid="userPointRank">可用分等级：<label title="截至2009-01-20 07:10:14，用户的可用分等级为：短工">短工</label>
                <li csdnid="userTotalTechPoint">总技术分：<label title="截至2009-01-20 07:10:14，用户的总技术分为：10">10</label>
                <li csdnid="userTotalTechPointRank">总技术分排名：<label title=截至2009-01-18，用户的总技术分排名为：223923>223923</label>
                <li class=medalregion csdnid="userMedal"></li>
            </ul>
            </td>
            <td class=main>
            <table style="HEIGHT: 100%" cellSpacing=0>
                <tbody>
                    <tr>
                        <td class=floor><span>发表于：<var csdnid="replyDate">2008-11-07 18:54:44</var></span><var csdnid="replyLayer">54</var>楼&nbsp;得分:<var csdnid="replyPoint">0</var></td>
                    </tr>
                    <tr>
                        <td class=content id=reply53439044_body csdnid="replyBody" name="reply_body">两个对象经过位拷贝之后，除了这两个对象的地址不同以为，它们成员变量的值绝对相同。 <br><br>要用值拷贝就是说要你在有指针成员变量的时候 一定要写拷贝构造函数和重载=。 <br><br>写程序注意就行。 <br><br>ASSERT(位拷贝==浅拷贝); <br>ASSERT(值拷贝==深拷贝); <br></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<table class="item reply" cellSpacing=1 csdnid="reply_53737970">
    <tbody>
        <tr>
            <td class=info>
            <dl>
            <dd class=icon><a title=进入用户个人空间 href="http://hi.csdn.net/jinder22" target=_blank><img onmouseover="javascript:try{showUserCard(event,'jinder22');}catch(ex){}" alt=进入用户个人空间 src="http://avatar.profile.csdn.net/8/3/7/2_jinder22.jpg"></a>
            <dd><a href="http://webim.csdn.net/AddFriends/jinder22.ashx" target=_blank>加为好友</a>
            <dd><a href="http://hi.csdn.net/guest/profile/post/send/jinder22.html" target=_blank>发送私信</a>
            <dd><a href="http://webim.csdn.net/Messages/jinder22.ashx" target=_blank>在线聊天</a></dd></dl>
            <ul>
                <li class=username onmouseover="javascript:try{showUserCard(event,'jinder22');}catch(ex){}"><img alt="" src="http://webim.csdn.net/UserOnlineFace/Theme1/jinder22.ashx"> <a href="http://hi.csdn.net/jinder22" target=_blank><var id=topicUserName csdnid="replyUsername">jinder22</var></a>
                <li class=nickname><textarea readOnly csdnid="replyNickname">像玫瑰花和亚里士多德一样美丽</textarea>
                <li class=grade>等级:<img class="grade user1" alt="" src="http://topic.csdn.net/u/ui/images/blank.gif" csdnid="replyUserRank">
                <li csdnid="userPointRank">可用分等级：<label title="截至2009-01-20 07:10:14，用户的可用分等级为：富农">富农</label>
                <li csdnid="userTotalTechPoint">总技术分：<label title="截至2009-01-20 07:10:14，用户的总技术分为：27">27</label>
                <li csdnid="userTotalTechPointRank">总技术分排名：<label title=截至2009-01-18，用户的总技术分排名为：167798>167798</label>
                <li class=medalregion csdnid="userMedal"></li>
            </ul>
            </td>
            <td class=main>
            <table style="HEIGHT: 100%" cellSpacing=0>
                <tbody>
                    <tr>
                        <td class=floor><span>发表于：<var csdnid="replyDate">2008-11-21 16:34:17</var></span><var csdnid="replyLayer">89</var>楼&nbsp;得分:<var csdnid="replyPoint">0</var></td>
                    </tr>
                    <tr>
                        <td class=content id=reply53737970_body csdnid="replyBody" name="reply_body">#include &lt;iostream&gt; <br>#include &lt;typeinfo&gt; <br>using&nbsp; namespace&nbsp; std; <br>class CBase <br>{ <br>&nbsp; &nbsp; public: <br>&nbsp; &nbsp; int *p; <br>&nbsp; &nbsp; CBase(){} <br>&nbsp; &nbsp; ~CBase() <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt; &lt;"释放堆地址" &lt; &lt;p &lt; &lt;endl; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete p; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; } <br>&nbsp; &nbsp; CBase(const CBase &amp;cbase) <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; p=cbase.p;//这样跟默认的位拷贝没有区别 <br>&nbsp; &nbsp; //p=new int; <br>&nbsp; &nbsp; //*p = *(cbase.p);&nbsp; // 这样才是深拷贝！！ <br>&nbsp; &nbsp; } <br>&nbsp; &nbsp; CBase(int *p) <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; this-&gt;p = p; <br>&nbsp; &nbsp; } <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; CBase &amp;operator=(const CBase &amp;cbase)//赋值函数 <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; p=cbase.p; <br>&nbsp; &nbsp; }&nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; void print() <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt; &lt;"对象" &lt; &lt;this &lt; &lt;":" &lt; &lt;"地址:" &lt; &lt;p &lt; &lt;"\t" &lt; &lt;"值:" &lt; &lt;*p &lt; &lt;endl; <br>&nbsp; &nbsp; } <br>}; <br>int main() <br>{ <br>&nbsp; &nbsp; //int a=8; <br>&nbsp; &nbsp; //CBase p(&amp;a);//错误,最后p指向了栈内存了 <br>&nbsp; &nbsp; int *a = new int(8); <br>&nbsp; &nbsp; int *b = new int(4); <br>&nbsp; &nbsp; int i =1; <br>&nbsp; &nbsp; int j =2; <br>&nbsp; &nbsp; int *ii,*pp; <br>&nbsp; &nbsp; *ii=2,*pp=4; <br>&nbsp; &nbsp; cout &lt; &lt; *ii &lt; &lt; "&nbsp; " &lt; &lt; ii &lt; &lt; endl; <br>&nbsp; &nbsp; cout &lt; &lt; *pp &lt; &lt; "&nbsp; " &lt; &lt; pp &lt; &lt; endl; <br>&nbsp; &nbsp; //pp=NULL; <br>&nbsp; &nbsp; //pp=ii; <br>&nbsp; &nbsp; *pp=*ii; <br>&nbsp; &nbsp; cout &lt; &lt; *ii &lt; &lt; "&nbsp; " &lt; &lt; ii &lt; &lt; endl; <br>&nbsp; &nbsp; cout &lt; &lt; *pp &lt; &lt; "&nbsp; " &lt; &lt; pp &lt; &lt; endl; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; cout &lt; &lt; i &lt; &lt; &amp;i; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; CBase p(a); <br>&nbsp; &nbsp; p.print(); <br>&nbsp; &nbsp; CBase p1(p); <br>&nbsp; &nbsp; p1.print(); <br>&nbsp; &nbsp; CBase p2(b); <br>&nbsp; &nbsp; p2=p; <br>&nbsp; &nbsp; p2.print(); <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; return 0; <br>} </td>
                    </tr>
                    <tr>
                        <td csdnid="replyModifyInfo"></td>
                    </tr>
                    <tr>
                        <td class=function><!--
                        <span>预留的文字链广告位</span>
                        //--></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/rdu-cpp/aggbug/72393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rdu-cpp/" target="_blank">rdu</a> 2009-01-20 21:05 <a href="http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Console Closing Down</title><link>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72383.html</link><dc:creator>rdu</dc:creator><author>rdu</author><pubDate>Tue, 20 Jan 2009 09:19:00 GMT</pubDate><guid>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72383.html</guid><wfw:comment>http://www.cppblog.com/rdu-cpp/comments/72383.html</wfw:comment><comments>http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72383.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/rdu-cpp/comments/commentRss/72383.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rdu-cpp/services/trackbacks/72383.html</trackback:ping><description><![CDATA[<table class=forsh width=728>
    <tbody>
        <tr>
            <td class=top><a href="http://www.cplusplus.com/member/Zolaboony/"><u><font color=#0000ff><strong>Zolaboony</strong> (1)</font></u></a></td>
            <td class=top align=right>
            <script type=text/javascript>When(1211146232)</script>
            May 19, 2008 at 5:30am<noscript></noscript></td>
        </tr>
        <tr>
            <td class=cont id=i7262 colSpan=2>Hi, i am new to C++ and have just written my "Hello World" program. It worked all right but the console/cmd closes down instantly. If you tell me why it is closing down it would be very helpful. </td>
        </tr>
    </tbody>
</table>
<br><a id=msg7263 name=msg7263></a>
<table class=forsh width=728>
    <tbody>
        <tr>
            <td class=top><a href="http://www.cplusplus.com/member/Duoas/"><strong>Duoas</strong> (1617)</a></td>
            <td class=top align=right>
            <script type=text/javascript>When(1211146859)</script>
            May 19, 2008 at 5:40am<noscript></noscript></td>
        </tr>
        <tr>
            <td class=cont id=i7263 colSpan=2>It is because your IDE is too stupid to know that a console application run from the IDE should have an automatic pause at the end.<br><br>Add the following to the end of your main() function to fix it:<br>
            <table class=snippet>
                <tbody>
                    <tr>
                        <td class=rownum vAlign=top align=right><tt>1<br>2<br>3<br>4<br>5<br>6<br>7<br></tt></td>
                        <td class=code vAlign=top>
                        <pre>  ...
                        std::cout &lt;&lt; <span class=str>"Press ENTER to continue..."</span>;
                        std::cin.ignore( std::numeric_limits&lt;std::streamsize&gt;::max(), <span class=str>'\n'</span> );
                        <span class=kw>return</span> 0;
                        }</pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>You'll need to #include &lt;limits&gt;<br><br>Enjoy.</td>
        </tr>
    </tbody>
</table>
<br>
<table class=forsh width=728>
    <tbody>
        <tr>
            <td class=top><a href="http://www.cplusplus.com/member/ropez/"><strong>ropez</strong> (312)</a></td>
            <td class=top align=right>
            <script type=text/javascript>When(1211694262)</script>
            May 25, 2008 at 1:44pm<noscript></noscript></td>
        </tr>
        <tr>
            <td class=cont id=i7682 colSpan=2>First I would search for the setting in my IDE that says "Keep console window open after program terminates".<br><br>If I didn't find it, I'd send a request for it to be included in the next version.<br><br>In the meantime: All the suggestions so far assumes that the program reach the end of main. Neither of you considered exceptions, or something like this in main:<br>
            <table class=snippet>
                <tbody>
                    <tr>
                        <td class=rownum vAlign=top align=right><tt>1<br>2<br></tt></td>
                        <td class=code vAlign=top>
                        <pre><span class=kw>if</span> (somethingFailed())
                        <span class=kw>return</span> SOME_ERROR_CODE;</pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>I suggest creating this small tool:<br>
            <table class=snippet>
                <tbody>
                    <tr>
                        <td class=rownum vAlign=top align=right><tt>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br></tt></td>
                        <td class=code vAlign=top>
                        <pre><span class=kw>class</span> KeepRunning {
                        <span class=kw>public</span>:
                        ~KeepRunning() {
                        <span class=comm>// Whatever code you prefer to keep the program running, e.g.</span>
                        system(<span class=str>"pause"</span>); <span class=comm>// "Press enter"</span>
                        sleep(5); <span class=comm>// Wait 5 seconds</span>
                        }
                        };</pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>In your main function:<br><br>
            <table class=snippet>
                <tbody>
                    <tr>
                        <td class=rownum vAlign=top align=right><tt>1<br>2<br>3<br>4<br>5<br>6<br></tt></td>
                        <td class=code vAlign=top>
                        <pre><span class=kw>int</span> main() {
                        KeepRunning kr;
                        <span class=comm>// code</span>
                        } <span class=comm>// Program stops here </span></pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>This way the program <em>must</em> keep the console open, even if it doesn't reach the end of main.<br><br>BTW: Of course, remember to remove this code before the program is "released".</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/rdu-cpp/aggbug/72383.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rdu-cpp/" target="_blank">rdu</a> 2009-01-20 17:19 <a href="http://www.cppblog.com/rdu-cpp/archive/2009/01/20/72383.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>