(ERROR)[C++]C++逻辑错误总结
来源:互联网 发布:小黄鸡peeper软件下载 编辑:程序博客网 时间:2024/06/05 08:14
1.使用循环求50-51的和
#include <iostream>int main(){int sum = 0;int i=50;while(i++ <= 51)sum += i;std::cout<<sum<<std::endl;sum = 0;for(int i = 50;i <= 51;i++)sum += i;std::cout<<sum<<std::endl;return 0;}执行结果:
错误原因:
while()循环中,i++的位置不合适,i++之后,sum += i用到了变量i,每循环一次多余向sum增加了1
修改后的代码:
#include <iostream>int main(){int sum = 0;int i=50;while(i++ <= 51)sum += i;std::cout<<sum<<std::endl;i = 50;sum = 0;while(i <= 51)sum += i++;std::cout<<sum<<std::endl;return 0;}执行结果:
0 0
- (ERROR)[C++]C++逻辑错误总结
- (ERROR)[C++]C++逻辑错误总结
- <C++>error LNK2001错误
- R6034错误,C Runtime Error
- R6034错误,C Runtime Error
- C语言错误 error C2059
- R6034错误,C Runtime Error
- R6034错误,C Runtime Error
- R6034错误,C Runtime Error
- R6034错误,C Runtime Error
- [C++] 错误总结
- C++/mfc错误总结
- C语言总结错误
- C语言总结错误
- C语言总结-关于逻辑短路
- C语言常见错误总结
- Linux C 编程错误总结
- Objective-C学习错误总结
- [Python]Functions, Variables, and the Call Stack
- MySQL批量插入性能优化
- Lucene 分字
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- 主流编程语言的特点分析
- (ERROR)[C++]C++逻辑错误总结
- 最受欢迎的搜索引擎前15名(2014年10月)
- 基于live555的rtsp客户端模块
- IOS 数据持久化之属性列表
- iOS 8.0 毛玻璃效果UIVisualEffect
- 【cocos2d-x 3.0-Mac配置篇】
- js 对象以及其基本操作
- 在Android线程中设置控件的值会报错
- 程序猿最应去的网站有哪些?