简体   繁体   English

输出中的 C++ 奇怪数字

[英]C++ weird numbers in Output

so I tried to recreate Conways Game of Life and i got it pretty much working but i have numbers in my output and i have no idea where they are coming from.所以我尝试重新创建 Conways Game of Life 并且我得到了很多工作,但是我的输出中有数字,我不知道它们来自哪里。 Im fairly new to c++and programming in general i learned python and java before but i have never seen this and google didnt seem to understand my question so i hope you fellow humans do.我对 c++ 和编程还很陌生,我以前学过 python 和 java,但我从来没有见过这个,谷歌似乎也没有理解我的问题,所以我希望你们人类也能理解。

My output looks like this: 1664447571170186994045474010000255652800 /its about this number 0000000000 0011000000 0110000000 0001000000 0000000000 0000000000 0000000000 0000000000 0000000000我的输出如下:1664447571170186994045474010000255652800 /其对这个号码0000000000 00.11亿01.1亿0001000000 0000000000 0000000000 0000000000 0000000000 0000000000

anyone has any idea where they come from cause i certainly don't.任何人都知道他们来自哪里,因为我当然不知道。 sometimes these numbers even "collide" with the board like this:有时这些数字甚至会像这样与棋盘“碰撞”:

0000-1-1-1-116644475711701869940 4547401010000000 791621423110000000 0101000000 0000000000 0000000000 0000000000 0000000000 0000000000 0020000000 0000-1-1-1-116644475711701869940 4547401010000000 791621423110000000 0101000000 0000000000 0000000000000000000

My sphagetthi code:我的 sphagetthi 代码:

#include <iostream>

#include <chrono>
#include <thread> 


using namespace std;
using namespace std::this_thread;
using namespace std::chrono;

int main(){
    
    int x = 10, y = 10, i, j, c, b;
    int field[y][x] =
    
    {{ 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,1,1,0,0,0,0},
     { 0,0,0,0,1,0,1,0,0,0},
     { 0,0,0,0,1,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0}};
     
    int updateField[y][x];
    
    
    
    
    cout << "start";
    for(b = 0;b<=10; b++){
        cout << "main loop: "<<b<<"\n";
        for(y = 0; y < 10; y++)
        {
            
            for(x = 0; x < 10; x++)
            {
            
            c = 0;
            
            if(y == 0 || y == 9 || x == 0 || x == 9){
                
                
                
            }
            else{
            
                /*v v v lebende zelle v v v*/
                if(field[y][x] == 1){
                    
                    if(field[y-1][x-1] == 1){
                        c++;
                    }
                    if(field[y-1][x] == 1){
                        c++;
                    }
                    if(field[y-1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x] == 1){
                        c++;
                    }
                    if(field[y+1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x+1] == 1){
                        c++;
                    }
                    /*regeln v v v*/
                    if(c < 2 or c > 3){
                        updateField[y][x] = 0;
                    }
                    else
                    {
                        updateField[y][x] = 1;
                    }
                    
                    
                    
                    
                }
                /*^ ^ ^ lebende zelle ^ ^ ^*/
                /*v v v tote zelle v v v*/
                
                else if(field[y][x] == 0){
                    
                    if(field[y-1][x-1] == 1){
                        c++;
                    }
                    if(field[y-1][x] == 1){
                        c++;
                    }
                    if(field[y-1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x] == 1){
                        c++;
                    }
                    if(field[y+1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x+1] == 1){
                        c++;
                    }
                    /*regeln v v v*/
                    if(c == 3)
                    {
                        updateField[y][x] = 1; 
                    }
                    else
                    {
                        updateField[y][x] = 0;
                    }
                }
                
            }
            
        }
    }
    cout << "\n";
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            field[i][j] = updateField[i][j];
        }
    }
        
        for(i = 0;i < x; i++){
            for(j = 0; j < y; j++ )
            {
                cout << field[i][j];
                
            }
        cout << "\n";
        }
        sleep_for(nanoseconds(500000000));
    }
    
    
    
}

I'd appreciate any answer.我很感激任何答案。 thanks in advance.提前致谢。

The problem is that you ignore the borders when building updateField (0 or 9 in a component) but then copy updateField including those borders into field .问题是您在构建updateField (组件中的 0 或 9)时忽略了边框,然后将包含这些边框的updateField复制到field The borders in updateField were never set, so they just contain any number which was in the memory before which is not controlled by your program. updateField中的边界从未设置过,因此它们只包含之前内存中不受程序控制的任何数字。

You should either set the borders of updateField to zero or do not copy the borders into field iterating from 1 to excluding 9 instead of from 0 to 10.您应该将updateField的边界设置为零,或者不要将边界复制到从 1 到排除 9 而不是从 0 到 10 迭代的field

While I hope that this solves your problem I would also like to share some ideas how to refactor your code.虽然我希望这能解决您的问题,但我也想分享一些如何重构代码的想法。 For instance you could use arrays of boolean values instead of integers as the values clearly can only be 0 or 1 according to the games logic.例如,您可以使用布尔值数组而不是整数,因为根据游戏逻辑,这些值显然只能是 0 或 1。 Scanning the neighborhood of a cell also adds a lot of redundant code which can be made shorter.扫描一个单元格的邻域也增加了许多可以缩短的冗余代码。 Just see the following as a general idea about some improvements which can be done:只需将以下内容视为有关可以完成的一些改进的一般想法:

#include <iostream>

#include <chrono>
#include <thread> 


using namespace std;
using namespace std::this_thread;
using namespace std::chrono;

int main(){
    
    int x = 10, y = 10, i, j, c, b;
    bool field[y][x] =
    
    {{ 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,1,1,0,0,0,0},
     { 0,0,0,0,1,0,1,0,0,0},
     { 0,0,0,0,1,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0}};//too lazy to rewrite to true/false, should also work this way
     
    bool updateField[y][x];
    
    
    
    
    cout << "start";
    for(b = 0;b<=10; b++){
        cout << "main loop: "<<b<<"\n";
        for(y = 0; y < 10; y++)
        {
            
            for(x = 0; x < 10; x++)
            {
            
            c = 0;
            
            if(y == 0 || y == 9 || x == 0 || x == 9){
                
                updateField[y][x] = false;
                
            }
            else{   
                for(i=-1;i<2;i++){
                    for(j=-1;j<2;j++){
                        if((i != 0 || j != 0) && field[y+i][x+j] == 1){// exclude i == j == 0
                            c++;
                        }
                    }
                }
                    
                if(field[y][x]){
                    if(c < 2 or c > 3){
                        updateField[y][x] = false;
                    }
                    else
                    {
                        updateField[y][x] = true;
                    }
                }else{
                    if(c == 3)
                    {
                        updateField[y][x] = true; 
                    }
                    else
                    {
                        updateField[y][x] = false;
                    }
                }
                
            }
            
        }
    }
    cout << "\n";
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            field[i][j] = updateField[i][j];
        }
    }
        
        for(i = 0;i < x; i++){
            for(j = 0; j < y; j++ )
            {
                cout << field[i][j];
                
            }
        cout << "\n";
        }
        sleep_for(nanoseconds(500000000));
    }
    
    
    
}

If you initialize updateField as well, the weird numbers will be gone.如果您也初始化 updateField,那么奇怪的数字就会消失。 Because of this if statement, the edge of updatefield doesn't update:由于这个 if 语句,updatefield 的边缘不会更新:

                if (y == 0 || y == 9 || x == 0 || x == 9) ;
                    else {

///long statements
                        if (c < 2 || c > 3) {
                            updateField[y][x] = 0;
                        }
                        else
                        {
                            updateField[y][x] = 1;
                        }
                  }

Also, you should be more consistent with your usage of variables, for example you define your map to be x*y sized, but later in the for loops you specify the maximum value of x and y to be 10.此外,您应该更一致地使用变量,例如您将地图定义为 x*y 大小,但稍后在 for 循环中您将 x 和 y 的最大值指定为 10。

What you're seeing is the contents of uninitialized memory.你看到的是未初始化内存的内容。 When you declare int updateField[y][x];当你声明int updateField[y][x]; the compiler reserves an appropriate space of memory for you, but it doesn't initialize the memory for you, so it contains whatever random junk was already there.编译器为您保留了适当的内存空间,但它不会为您初始化内存,因此它包含任何已经存在的随机垃圾。

To fix this, initialize updateField to be identical to field before running the loop:要解决此问题,请在运行循环之前将updateField初始化为与 field 相同:

for (int i = 0; i < y; ++i) {
    for (int j = 0; j < x; ++j) {
        updateField[i][j] = field[i][j];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM