使用增量/减少操作员在C 中显示数组的奇怪结果
Strange result of displaying array using increment/decrement operator in C++
#include <iostream>
using namespace std;
int main()
{
double donation[10];
int index = 0;
cout.setf(ios::fixed);
cout << "Enter sum of money for donating: ";
while (index < 10 && cin >> donation[index])
{
cout << "donation #" << 1 + index++ << ": " << donation[index] << endl;
}
return 0;
}
结果
该代码无法显示捐赠的正确值...
我可以检查错误是" 1 索引 ",但我不知道为什么这样做。
为什么我使用'1 index '的代码在下一行中使用'index '时与代码有所不同。
应避免使用惯用方式,而不是试图理解陌生人构造,还应避免将增量与功能/操作员参数混合(请参阅序列点(:
for (int index = 0; index < 10; ++index)
{
if (! std::cin >> donation[index])
break;
std::cout << "donation #" << (1 + index) << ": " << donation[index] << std::endl;
}
这应该做期望的事情,应该通过对C 的知识来理解。它最多运行10次,试图填充输入到数组,并在输入失败时输入起作用或停止时显示。唯一的问题将是对用户输入的更好验证。
相关文章:
- 显示错误输出的简单数组排序程序
- 显示数组中的学生 ID 和最高分
- 代码使用向量成功运行,但使用数组显示错误
- 对象初始化后在C++中显示 char 数组时的异常行为
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- 如何在函数中显示结构数组
- 显示结构数组
- 在数组中显示第一个元素用户输入
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 将 5 个 for 循环压缩为,将数组显示为 1
- 为什么析构函数和复制构造函数使用对象数组显示此行为
- 在VC2010中使用Char数组显示字符串时发生运行时错误
- 将数组显示为表格
- 从boost::p roperty_tree读取数组显示为空白
- c++错误数组显示
- 二维动态数组c++显示问题