简体   繁体   中英

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. 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.

My output looks like this: 1664447571170186994045474010000255652800 /its about this number 0000000000 0011000000 0110000000 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

My sphagetthi code:

#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 . 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.

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.

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. 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. Because of this if statement, the edge of updatefield doesn't update:

                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.

What you're seeing is the contents of uninitialized memory. When you declare 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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