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);