Thread: Something is wrong.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    Something is wrong.

    Something is wrong with my programme but I'm stangling to find what it is. I've spent two whole days.



    Code:
    int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
        char **board;
        char board_interaction;
        printf("Give the two dimensions of the board.\n");
        do
        {
            do
            {
                scanf("%d",&N);
            }
            while(N<4);
        }
        while(N>15);
        do
        {
            do
            {
                scanf("%d",&M);
            }
            while(M<5);
        }
        while(M>15);
        do
        {
            printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
            scanf("%d",&diff);
        }
        while((diff!=1)&&(diff!=2)&&(diff!=3));
        if(diff==1)
        {
            number_of_enemies=((N*M)*5)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else if(diff==2)
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*10)/100;
        }
        enemies_to_kill=number_of_enemies;
        board=(char**)malloc(N*sizeof(char*));
        for(i=0;i<N;i++)
        {
            board=(char*)malloc(M*sizeof(char));
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                board[i][j]='.';
            }
        }
        board[0][0]='$';
        while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='$'))
        {
            board[rand()%N][rand()%M]='#';
            number_of_obstacles=number_of_obstacles-1;
        }
        do
        {
            a=rand()%N;
            b=rand()%M;
            board[a][b]='1';
        }
        while((board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#'));
        while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
        {
            if(board[a+1][b]=='.')
            {
                board[a+1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            else if(board[a-1][b]=='.')
            {
                board[a-1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            if(board[a][b+1]=='.')
            {
                board[a][b+1]='3';
                number_of_enemies=number_of_enemies-1;
            }
            else if(board[a][b-1]=='.')
            {
                board[a][b-1]='3';
                number_of_enemies=number_of_enemies-1;
            }
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                printf(" %c ",board[i][i]);
            }
        }
        printf("\nLevel money spent:%d \n",money);
        printf("Game money spent:%d \n",total_money);
        printf("Make your move(s):");
        scanf("%c",board_interaction);
        for(i=0;i<M;i++)
        {
            free(board[i]);
        }
        free(board);
        return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    Was it really worth trimming off a couple of lines that prevent people from simply copy/pasting your code to try it?

    Fix all these warnings would be the first thing.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:51:14: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             board=(char*)malloc(M*sizeof(char));
                  ^
    foo.c:106:11: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
         scanf("%c",board_interaction);
               ^
    foo.c:4:115: warning: unused variable ‘position_j’ [-Wunused-variable]
     int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
                                                                                                                       ^
    foo.c:4:102: warning: unused variable ‘position_i’ [-Wunused-variable]
     int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
                                                                                                          ^
    foo.c:4:82: warning: variable ‘enemies_to_kill’ set but not used [-Wunused-but-set-variable]
     int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b,position_i=0,position_j=0;
                                                                                      ^
    foo.c:106:5: warning: ‘board_interaction’ is used uninitialized in this function [-Wuninitialized]
         scanf("%c",board_interaction);
         ^

    Next would be breaking your 100+ line main down into some functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    That is the problem. I cannot fix them and find a solution by myself. That is why I posted my code here.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    Well you ignored my previous advice about just using statically sized arrays while you get the bulk of the code working.

    Instead, you went for broke, tried to do it all in one big hit, and missed.

    > board=(char*)malloc(M*sizeof(char));
    1. There's no need to cast the return result of malloc in a C program.
    2. Study how you call free, which somehow you've managed to do successfully.

    > scanf("%c",board_interaction);
    Don't forget the &
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    It is not that I left out your advice but the moving of the $ is not showing.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,996
    Restart then, and this time develop incrementally. Write a small piece of code, compile and test, then debug and fix as needed. Write some more code, compile and test, then debug and fix as needed. When a function starts getting a little long (say longer than 50 to 70 lines), break it up into smaller functions that each does one thing and does it well. This way, you will have a much easier time finding out your mistakes than trying to hunt for them after writing a large chunk of code. You will even be able to write automated unit tests to help you be more certain that your program is working as expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    I uploaded the program because the terminal said Segmentation fault (core dumped). That is why I cannot find any problems.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    And I've already shown you two places in your code where trouble like segmentation faults will appear.

    Have you fixed them?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Both of them. Now without the warnings but still Segmentation fault.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Now without the warnings but still Segmentation fault.
    Post your modified code. And note without the complete program we will probably be guessing about where the problem lies because we can't see the problem.

    Did you run the program with your debugger? The debugger should tell you exactly where it detects the problem and you should also be able to view the contents of your variables at the time of the crash.

  11. #11
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Code:
    int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b;
        char **board;
        char board_interaction;
        printf("Give the two dimensions of the board.\n");
        do
        {
            do
            {
                scanf("%d",&N);
            }
            while(N<4);
        }
        while(N>15);
        do
        {
            do
            {
                scanf("%d",&M);
            }
            while(M<5);
        }
        while(M>15);
        do
        {
            printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
            scanf("%d",&diff);
        }
        while((diff!=1)&&(diff!=2)&&(diff!=3));
        if(diff==1)
        {
            number_of_enemies=((N*M)*5)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else if(diff==2)
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else if(diff==3)
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*10)/100;
        }
        enemies_to_kill=number_of_enemies;
        board=(char**)malloc(N*sizeof(char*));
        for(i=0;i<N;i++)
        {
            board=malloc(M*sizeof(char));
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                board[i][j]='.';
            }
        }
        board[0][0]='$';
        while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='$'))
        {
            board[rand()%N][rand()%M]='#';
            number_of_obstacles=number_of_obstacles-1;
        }
        do
        {
            a=rand()%N;
            b=rand()%M;
            board[a][b]='1';
        }
        while((board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#'));
        while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
        {
            if((board[a+1][b]=='.')&&((a!=N)||(b!=M)))
            {
                board[a+1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            else if((board[a-1][b]=='.')&&((a!=N)||(b!=M)))
            {
                board[a-1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            if((board[a][b+1]=='.')&&((a!=N)||(b!=M)))
            {
                board[a][b+1]='3';
                number_of_enemies=number_of_enemies-1;
            }
            else if((board[a][b-1]=='.')&&((a!=N)||(b!=M)))
            {
                board[a][b-1]='3';
                number_of_enemies=number_of_enemies-1;
            }
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                printf(" %c ",board[i][i]);
            }
        }
        printf("\nLevel money spent:%d \n",money);
        printf("Game money spent:%d \n",total_money);
        printf("Make your move(s):");
        scanf("%c",&board_interaction);
        for(i=0;i<M;i++)
        {
            free(board[i]);
        }
        free(board);
    I am working on the debugger now.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    So where is the complete code?

  13. #13
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int N,M,i,j,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill,a,b;
        char **board;
        char board_interaction;
        printf("Give the two dimensions of the board.\n");
        do
        {
            do
            {
                scanf("%d",&N);
            }
            while(N<4);
        }
        while(N>15);
        do
        {
            do
            {
                scanf("%d",&M);
            }
            while(M<5);
        }
        while(M>15);
        do
        {
            printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
            scanf("%d",&diff);
        }
        while((diff!=1)&&(diff!=2)&&(diff!=3));
        if(diff==1)
        {
            number_of_enemies=((N*M)*5)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else if(diff==2)
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*5)/100;
        }
        else if(diff==3)
        {
            number_of_enemies=((N*M)*10)/100;
            number_of_obstacles=((N*M)*10)/100;
        }
        enemies_to_kill=number_of_enemies;
        board=(char**)malloc(N*sizeof(char*));
        for(i=0;i<N;i++)
        {
            board=malloc(M*sizeof(char));
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                board[i][j]='.';
            }
        }
        board[0][0]='$';
        while((number_of_obstacles!=0)&&(board[rand()%N][rand()%M]!='$'))
        {
            board[rand()%N][rand()%M]='#';
            number_of_obstacles=number_of_obstacles-1;
        }
        do
        {
            a=rand()%N;
            b=rand()%M;
            board[a][b]='1';
        }
        while((board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#'));
        while((number_of_enemies!=0)&&(board[rand()%N][rand()%M]!='$')&&(board[rand()%N][rand()%M]!='#')&&((a!=N)||(b!=M)))
        {
            if((board[a+1][b]=='.')&&((a!=N)||(b!=M)))
            {
                board[a+1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            else if((board[a-1][b]=='.')&&((a!=N)||(b!=M)))
            {
                board[a-1][b]='2';
                number_of_enemies=number_of_enemies-1;
            }
            if((board[a][b+1]=='.')&&((a!=N)||(b!=M)))
            {
                board[a][b+1]='3';
                number_of_enemies=number_of_enemies-1;
            }
            else if((board[a][b-1]=='.')&&((a!=N)||(b!=M)))
            {
                board[a][b-1]='3';
                number_of_enemies=number_of_enemies-1;
            }
        }
        for(i=0;i<N;i++)
        {
            for(j=0;j<M;j++)
            {
                printf(" %c ",board[i][i]);
            }
        }
        printf("\nLevel money spent:%d \n",money);
        printf("Game money spent:%d \n",total_money);
        printf("Make your move(s):");
        scanf("%c",&board_interaction);
        for(i=0;i<M;i++)
        {
            free(board[i]);
        }
        free(board);
        return 0;
    }
    The complete program I have a problem with is here.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    > board=malloc(M*sizeof(char));
    You didn't fix this.

    board[i]=malloc(M*sizeof(char));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    That solved the Sengmetation problem but now the first row is all filled with $, the second row is filled with 2, the third with 3, the forth with 1 and the 7th,9th,10th,12th row with 1. Also, I give it different dimensions other than 15 and 15 but it continues to ask me the difficulty without ending.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread