Thread: Creating and printing and array crash (using curses)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    21

    Creating and printing and array crash (using curses)

    Hello,

    I am trying to create a 2d array, fill it with characters and then print it out in a console window but the console window crashes without any compiler errors from dev-c++

    Code:
    #include <curses.h>
    #include <stdlib.h>
    #define ESC 27 //ASCII for escape
    #define ROCK 178
    
    
    int main() {
        
        //initialize curses
        keypad(initscr(),1);
        curs_set(0);
        
        int mapX = 20;
        int mapY = 20;
        
        int i;
        int j;
        
        char mapChar[mapX][mapY];
        
        for(i=0; i<mapY;i++)
            for(j=0; i<mapX;j++)
                mapChar[i][j] = '#';
        
        while(int run=1){
        
            for(i=0; i<mapY;i++)
                for(j=0; i<mapX;i++)
                    mvaddch(i,j,mapChar[i][j]);
    
                    
            if(getchar() == ESC){
                run = 0;
            }     
        }    
        getch();
        return endwin();
    }
    I'm quite new to C so I'm sorry if it's obvious

    Many thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check lines 22 and 28. You use i when you should use j.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    Thanks, I changed that but it still crashes =(

    edit: does work now lol thanks a lot

    On a releated note I oringinally tried to create the array as a function outside of the main, if I post my attempts do you think you could help on that as well?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I had to fix line 25 because my compiler didn't like declaring and initializing run in the loop condition. After doing that, and making the change I mentioned before, I got no crashes. Did you change all 3 occurrences (1 on line 22 and 2 on line 28)?

    What symptoms tell you it crashed? I don't get a crash, I just don't see output until I press escape a few times.

    EDIT: If you're really new to C programming, I suggest avoiding curses for the time being. Not to discourage you, but it's much easier to learn C without the mess of curses. Find some good regular tutorials, and when you get that down, get a good curses tutorial and come back to it.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    I've done some coding before and I have a project which is similar to a roguelike. And I wanted to learn C since it is so widely used.

    There are a few tutorials for roguelikes but the ones I found are all incomplete

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I still advise not starting off with a curses app. If you have previous programming experience, then learning C should be fairly easy. Curses just adds a layer of complexity on top of your learning, that will probably slow down the overall learning process for C and for curses. I suggest going through something like Amazon.com: Sams Teach Yourself C in 21 Days (6th Edition) (0752063324486): Bradley L. Jones, Peter Aitken: Books first. You could do it in much less than 21 days. Read it and work through the exercises. Each lesson should take a few hours. Along the way, make sure you learn to use a debugger, it's a critical tool/skill to have. AFAIK, Dev-C++ is no longer maintained, so look into a newer compiler/IDE.

    Is your program still crashing? Can you provide more details on exactly what it's doing wrong?

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    Program works now thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array and pointer( crash exe)
    By mohsen in forum C Programming
    Replies: 8
    Last Post: 08-01-2011, 03:03 PM
  2. Printing arrays to curses screen
    By trev456 in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 12:46 AM
  3. output an array to curses, or to another window?
    By trev456 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2007, 10:08 AM
  4. Why this (dynamic array) doesn't crash ?
    By soothsayer in forum C Programming
    Replies: 13
    Last Post: 01-26-2006, 10:01 PM
  5. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM