c++ primer 学习之路 一

20150908

cout << i - - ; 语句中,” << 运算符没有明确规定何时以及如何对运算对象求职,因此这个输出表达式是未定义的。“ 



=================================================================================================================================

练习1.10

题目要求在while循环中使用 递减运算符(--),实现代码:

<pre name="code" class="cpp">int i = 10;
while (i >= 0)
{
std::cout << i --<<std::endl;
}

 但要取消语句中的endl,程序将无法正确运行: 

int i = 10;
while (i >= 0)
{
std::cout << i --;
}
语句中少了endl,缓冲区的内容不能正常刷新到屏幕——具体原因不明白。

问了其他小伙伴,有人提议使用数组或字符串保存数据,再打印到屏幕。

…………………………………………………………………………………………………………………………………………………………………………………………………………
练习1.17
课本给出代码:

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
<span style="white-space:pre">	</span>int currVal = 0, val = 0;
<span style="white-space:pre">	</span>if (std::cin >> currVal)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>int cnt = 1;
<span style="white-space:pre">		</span>while (std::cin >> val) //此行代码导致错误
<span style="white-space:pre">		</span>if (val == currVal)
<span style="white-space:pre">			</span>++cnt;
<span style="white-space:pre">		</span>else{
<span style="white-space:pre">			</span>std::cout << currVal << "occurs "
<span style="white-space:pre">					</span>  << cnt << "times" << std::endl;
<span style="white-space:pre">		</span> currVal = val;
<span style="white-space:pre">		</span> cnt = 1;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>std::cout << currVal << "occurs "
<span style="white-space:pre">			</span><< cnt << "times" << std::endl;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>return 0;
}

程序不能打印最后一组数据,且无法退出程序。

在至错行添加“输入是否为回车的判断”

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
<span style="white-space:pre">	</span>int currVal = 0, val = 0;
<span style="white-space:pre">	</span>if (std::cin >> currVal)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>int cnt = 1;
<span style="white-space:pre">		</span>while (std::cin >> val&&std::cin.get()!= '\n') //添加输入是否为回车的判断
<span style="white-space:pre">		</span>if (val == currVal)
<span style="white-space:pre">			</span>++cnt;
<span style="white-space:pre">		</span>else{
<span style="white-space:pre">			</span>std::cout << currVal << "occurs "
<span style="white-space:pre">					</span>  << cnt << "times" << std::endl;
<span style="white-space:pre">		</span> currVal = val;
<span style="white-space:pre">		</span> cnt = 1;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>std::cout << currVal << "occurs "
<span style="white-space:pre">			</span><< cnt << "times" << std::endl;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>return 0;
}

但cin.get()读取输入流中的数据,导致输出结果不正确。即使添加cin.unget() 语句,仍然不正确。

cin的指针往回移动1格,就是刚刚读取操作的读取的最后一个字符重新回到缓冲区



…………………………………………………………………………………………………………………………………………………………………………………………………………

程序中可以用cin.get(); 语句来使程序暂停不秒退。

或者cin.ignor();语句与cin.get();连用,

<span style="white-space:pre">	</span>std::cin.ignore('\n',100);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值