这个字符数组代码有什么问题?

发布于 2024-12-23 10:12:31 字数 1929 浏览 2 评论 0原文

我是 C++ 新手,只编程了几天,所以这可能看起来很愚蠢,但是你能找出为什么我的数组不能正常工作吗?这是我正在设计的一个程序的开始,该程序将解决数独难题,但我用来解决它的二维数组无法正常工作。

#include <iostream>
#include <string>
using namespace std;

int main () {
    char dash[9][9];
    for (int array=0; array<9; array++) {
        for (int array2=0; array2<9; array2++) {
            dash[array][array2]=array2;
            cout << dash[array][array2];
        }
    }
    cout << dash[1][4] << endl; //This is temporary, but for some reason nothing outputs when I do this command.
    cout << "╔═══════════╦═══════════╦═══════════╗" << endl;
    for (int count=0; count<3; count++) {
        for (int count2=0; count2<3; count2++) {
            cout << "║_" << dash[count][count2*3] << "_|_" << dash[count]    [count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
        }
            cout << "║" << endl;
    }
    cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
    for (int count=0; count<3; count++) {
        for (int count2=0; count2<3; count2++) {
            cout << "║_" << dash[count][count2*3] << "_|_" << dash[count]    [count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
        }
        cout << "║" << endl;
    }
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=0; count<3; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╚═══════════╩═══════════╩═══════════╝" << endl;
return 0;

另外,

我知道可能有更简单的方法来构建数独板,但我已经在脑海中看到了这个方法将如何工作,如果它失败了,那么,唯一的学习方法就是通过失败。我只想知道数组出了什么问题。

I'm new to C++, only been programming a few days so this might seem stupid, but can you spot why my arrays are not working correctly? This is the start of a program I'm designing that will solve Sudoku puzzles, but the 2D array that I'm using to solve it isn't working correctly.

#include <iostream>
#include <string>
using namespace std;

int main () {
    char dash[9][9];
    for (int array=0; array<9; array++) {
        for (int array2=0; array2<9; array2++) {
            dash[array][array2]=array2;
            cout << dash[array][array2];
        }
    }
    cout << dash[1][4] << endl; //This is temporary, but for some reason nothing outputs when I do this command.
    cout << "╔═══════════╦═══════════╦═══════════╗" << endl;
    for (int count=0; count<3; count++) {
        for (int count2=0; count2<3; count2++) {
            cout << "║_" << dash[count][count2*3] << "_|_" << dash[count]    [count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
        }
            cout << "║" << endl;
    }
    cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
    for (int count=0; count<3; count++) {
        for (int count2=0; count2<3; count2++) {
            cout << "║_" << dash[count][count2*3] << "_|_" << dash[count]    [count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
        }
        cout << "║" << endl;
    }
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=0; count<3; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╚═══════════╩═══════════╩═══════════╝" << endl;
return 0;

}

Also I'm aware that there may be easier ways to build the Sudoku board, but I can already see in my mind how this one will work, and if it fails, well, the only way to learn is by failure. All I want to know is what is wrong with the arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-12-30 10:12:31

您已将数字数据存储在 char 数组中,这很好,但 cout 尝试将其打印为字符。尝试在输出期间转换为整数:

cout << (int)dash[count][count2*3]

另一个选项是将字符存储在数组中:

for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2] = '0' + array2;
    }
}

You've got numeric data stored in your char array, which is fine, but cout tries to print it as a character. Try casting to integer during output:

cout << (int)dash[count][count2*3]

The other option is to store characters in the array:

for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2] = '0' + array2;
    }
}
无人接听 2024-12-30 10:12:31

您试图将字符显示为整数。嗯,从技术上讲,它们是,但它们不显示为整数。要么将 char 数组更改为 int 数组(非常简单),要么每次显示数据时将其转换为 int(乏味)。

You're trying to display chars as if they were integers. Well, technically, they are, but they don't display as integers. Either change your char array to an int array(very easy), or every time you display the data, cast it to int(tedious).

小瓶盖 2024-12-30 10:12:31

char dash[9][9] 更改为 int dash[9][9]。您可以为 dash[i][j] 分配较小的数字,因为 char 它们大多是不可打印的控制字符,因此不会打印任何可理解的内容。作为 int ,它们会按照您的预期打印。

Change char dash[9][9] to int dash[9][9]. You assign small numbers to dash[i][j], as chars they are mostly unprintable control characters, so nothing intelligible gets printed. As ints they are printed as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文