尝试打印数组不会打印任何内容
Attempt to print an array prints nothing
我正在尝试创建一个打印数组的程序。我希望这个程序的输出是:
10000
00000
00000
00000
00000
事实并非如此。相反,它只是什么都不打印。没有编译错误。我有Microsoft Visual Studio 2010。
#include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
int a [5] [5] = {0};
a [1] [1] = 1;
int xcount = 0;
int ycount = 0;
while(xcount < 6);
{
cout << a [xcount] [ycount];
xcount = xcount + 1;
if(xcount = 6)
{
ycount = ycount + 1;
xcount = xcount + 1;
if(ycount = 6)
{
exit(0);
}
}
}
return 0;
}
如有任何帮助,我们将不胜感激。
如果我理解正确,您将尝试按列输出数组。您的代码包含许多错误,包括while语句末尾的分号。正确的程序可能看起来像
#include "stdafx.h"
#include <iostream>
int main()
{
const size_t N = 5;
int a[N][N] = { 1 };
int xcount = 0;
int ycount = 0;
while ( true )
{
std::cout << a[xcount][ycount];
if ( ++xcount == N )
{
std::cout << std::endl;
xcount = 0;
if ( ++ycount == N )
{
break;
}
}
}
return 0;
}
if (xcount=6) // This sets xcount to 6
将其更改为:
if (xcount==6) // this compares xcount with 6
现在编辑;又回到了问题中:
由于没有任何更改xcount-删除;
while(xcount < 6);
将无限循环删除分号:
while (xcount<6);
除此之外,算法是错误的。更换第二个
xcount=xcount+1;
通过
xcount=0;
循环的附言在这里会更好。它会更干净,因为你知道循环的范围:
for (int ycount=0; ycount<6; ++ycount)
{
for (int xcount=0; xcount<6; ++xcount)
cout << a[xcount][ycount];
cout << endl;
}
请注意,在原始解决方案中并没有换行符。
p.p.S.在比较中使用==。
您应该使用for循环,而不是while试试这个:
for(int xcount=0; xcount<5; xcount++)
{
for(int ycount=0; ycount<5; ycount++)
{
cout<< a[xcount][ycount];
}
cout<<endl;
}
a[0][0] = 1;
相关文章:
- 打印时有二叉树问题.用户输入不打印任何内容
- main() 中的 std::cout 在调试期间不会在调试控制台中打印任何内容
- cout 不打印任何内容到控制台
- 为什么字符串没有更新并且最终没有打印任何内容
- 我如何处理 TMP 以避免打印任何东西
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 泛型函数反向打印任何矢量,编译器出错
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- Cout 一个字符串可以打印任何内容到屏幕上
- "cout<<count<<endl;"没有打印任何内容
- C++ 中是否有任何内置阶乘函数?
- 为什么循环迭代不打印任何东西?
- 函数不打印任何 c++
- 下面的代码没有打印任何东西
- 我的 c++ 程序中的字符数组不会打印任何内容
- 如果否则错误:如何使其打印任何数字
- 在 C++ 中声明向量后无法打印任何内容
- 在模板功能中打印任何结构
- 如果找不到解决方案,我如何留下递归循环,而不会打印任何东西
- 是否有任何内置函数可以告诉编译器分支是否可预测