c++ primer 学习之路 一
2015-09-03 18:41
531 查看
20150908
cout << i - - ; 语句中,” << 运算符没有明确规定何时以及如何对运算对象求职,因此这个输出表达式是未定义的。“
=================================================================================================================================
练习1.10
题目要求在while循环中使用 递减运算符(--),实现代码:
但要取消语句中的endl,程序将无法正确运行:
语句中少了endl,缓冲区的内容不能正常刷新到屏幕——具体原因不明白。
问了其他小伙伴,有人提议使用数组或字符串保存数据,再打印到屏幕。
…………………………………………………………………………………………………………………………………………………………………………………………………………
练习1.17
课本给出代码:
程序不能打印最后一组数据,且无法退出程序。
在至错行添加“输入是否为回车的判断”
但cin.get()读取输入流中的数据,导致输出结果不正确。即使添加cin.unget() 语句,仍然不正确。
把cin的指针往回移动1格,就是刚刚读取操作的读取的最后一个字符重新回到缓冲区
…………………………………………………………………………………………………………………………………………………………………………………………………………
程序中可以用cin.get(); 语句来使程序暂停不秒退。
或者cin.ignor();语句与cin.get();连用,
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);
相关文章推荐
- C++ Primer 学习笔记_98_特殊工具与技术 --优化内存分配
- C++ Primer 学习笔记_99_特殊工具与技术 --优化内存分配[续1]
- Effective C++——条款17(第3章)
- C++基础---string类的clear/erase/pop_back
- C++基础---string类的find/find_first_of/find_first_not_of/rfind/find_last_of/find_last_not_of
- C++基础---string类的operator==/operator!=/operator</operator<=/operator>/operator>=/compare
- C++函数中那些不可以被声明为虚函数的函数
- C++基础---string类的operator=/assign
- C语言中链表怎么删除结点?
- C语言中链表怎么删除结点?
- C++基础---string类的operator<</operator>>/getline
- Trie
- C++基础---string类的capacity/max_size/size/length/empty/reserve/resize
- C++基础--string类的operator[]/at/back/front
- C++基础---string类的data/c_str/copy
- C++基础---string类的构造函数
- 来看看C语言的-1>1并且小有1的问题
- C++基础---字符串(string型字符串)
- c语言实现顺序二叉树的非递归的三种遍历算法
- 【C语言】结构体