Convert string to char or use 2d array of char?

This is a discussion on Convert string to char or use 2d array of char? within the C++ Programming forums, part of the General Programming Boards category; I'm making a pacman video game and I would like to display the game board using the printw command from ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    13

    Convert string to char or use 2d array of char?

    I'm making a pacman video game and I would like to display the game board using the printw command from the ncurses library (Linux). I am not able to do this as the printw function only accepts a const char input and my map is a string type.

    I've been trying to make the map a 2d array type char array but have been unsuccessful. I've also tried to convert string type to char but no luck.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include "pacman.h"
    #include <ncurses.h>
    
    using namespace std;
    
    string map[31] = {
        "┌────────────┐┌────────────┐",
        "│⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅││⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│",
        "│⋅┌──┐⋅┌───┐⋅││⋅┌───┐⋅┌──┐⋅│",
        "│∙│  │⋅│   │⋅││⋅│   │⋅│  │∙│",
        "│⋅└──┘⋅└───┘⋅└┘⋅└───┘⋅└──┘⋅│",
        "│⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│",
        "│⋅┌──┐⋅┌┐⋅┌──────┐⋅┌┐⋅┌──┐⋅│",
        "│⋅└──┘⋅││ └──┐┌──┘⋅││⋅└──┘⋅│",
        "│⋅⋅⋅⋅⋅⋅││⋅⋅⋅⋅││⋅⋅⋅⋅││⋅⋅⋅⋅⋅⋅│",
        "└────┐⋅│└──┐ ││ ┌──┘│⋅┌────┘",
        "     │⋅│┌──┘ └┘ └──┐│⋅│     ",
        "     │⋅││          ││⋅│     ",
        "     │⋅││ ┌──────┐ ││⋅│     ",
        "─────┘⋅└┘ │      │ └┘⋅└─────",
        "      ⋅   │      │   ⋅      ",
        "─────┐⋅┌┐ │      │ ┌┐⋅┌─────",
        "     │⋅││ └──────┘ ││⋅│     ",
        "     │⋅││          ││⋅│     ",
        "     │⋅││ ┌──────┐ ││⋅│     ",
        "┌────┘⋅└┘ └──┐┌──┘ └┘⋅└────┐",
        "│⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅││⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│",
        "│⋅┌──┐⋅┌───┐⋅││⋅┌───┐⋅┌──┐⋅│",
        "│⋅└─┐│⋅└───┘⋅└┘⋅└───┘⋅│┌─┘⋅│",
        "│∙⋅⋅││⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅││⋅⋅∙│",
        "└─┐⋅││⋅┌┐⋅┌──────┐⋅┌┐⋅││⋅┌─┘",
        "┌─┘⋅└┘⋅││⋅└──┐┌──┘⋅││⋅└┘⋅└─┐",
        "│⋅⋅⋅⋅⋅⋅││⋅⋅⋅⋅││⋅⋅⋅⋅││⋅⋅⋅⋅⋅⋅│",
        "│⋅┌────┘└──┐⋅││⋅┌──┘└────┐⋅│",
        "│⋅└────────┘⋅└┘⋅└────────┘⋅│",
        "│⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│",
        "└──────────────────────────┘"
    };
    
    int main()
    {
    
        // ncurses libary functions
        initscr();
    
        // Draw Map
        for (unsigned int i = 0; i < (sizeof(map) / sizeof(map[0])); i++)
        {
            mvprintw(i, 25, map[i]); // unable to do this because map is not a const char
        }
    
        refresh();
        getch();
        endwin();
        
        return 0;
    }
    If anyone has any suggestions could they please post the code so I get get a better idea as I am new to C++.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    You may leave the array's length if its elements are being initialised in the way you did it. So you can write: string map[] = ...
    You can still access string as a c-string - string::c_str - C++ Reference.
    You should use std::vector instead of raw array, this will enable you to make variable-sized levels and simplify memory management.
    And remember to use std::wstring for wide-characters.
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    Quote Originally Posted by kmdv View Post
    You may leave the array's length if its elements are being initialised in the way you did it. So you can write: string map[] = ...
    You can still access string as a c-string - string::c_str - C++ Reference.
    You should use std::vector instead of raw array, this will enable you to make variable-sized levels and simplify memory management.
    And remember to use std::wstring for wide-characters.
    Thank you for the quick response.

    I haven't learned much about templates or explored the vector area yet... So far I have:

    Code:
    int main()
    {
        char * cstr;
        cstr = new char [100];
    
        // ncurses libary functions
        initscr();
    
        // Draw Map
        for (unsigned int i = 0; i < (sizeof(map) / sizeof(map[0])); i++)
        {
            strcpy (cstr, map[i].c_str());
            mvprintw(i, 25, cstr);
        }
    
        refresh();
        getch();
        endwin();
        
        return 0;
    }
    This code compiles fine but spits out garbage when ran, likely because I have not made the proper adjustments to display the wide characters.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    This causes a memory leak.
    You should give a beginner's book a try before you properly begin to use C++.
    I'd recommend you at least read through Accelerated C++ to get a feeling on what the various algorithms and containers, as well as language features, there are in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    Quote Originally Posted by Elysia View Post
    This causes a memory leak.
    You should give a beginner's book a try before you properly begin to use C++.
    I'd recommend you at least read through Accelerated C++ to get a feeling on what the various algorithms and containers, as well as language features, there are in C++.
    I understand I haven't deleted the pointer so it'll leak... I've read about half of the book 'C++ Primer Plus' and have not yet seem this topic covered. I find that I benefit from programming instead of just reading. That's why I'm making this small game. It's to the point where I think I'll have read about 3 books and still not have the proper knowledge to display a wide character using ncurses.

    It's frustrating.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    It's because you've (unfortunately) been reading the wrong books.
    It always pays to have a basic understanding of the language before attempting a big project (your so-called game is probably going to get big, fast).
    Accelerated C++ is the only beginner's book one would need to begin working with C++ in my opinion, so that's why I recommended it.

    I would suggest to avoid ncurses since it's basically a C library, but I am hesitant to suggest so since I don't know much about Linux (seems to be very C oriented).

    In any case, the fact that you don't have a basic understanding of a vector makes me hesitant to recommend you to continue. You should probably be aware of the basic containers and algorithms first, so give Accelerated C++ a try. You'll be ready afterwards, I think. You can even practice applying your knowledge from the book into your program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    13
    Quote Originally Posted by Elysia View Post
    It's because you've (unfortunately) been reading the wrong books.
    It always pays to have a basic understanding of the language before attempting a big project (your so-called game is probably going to get big, fast).
    Accelerated C++ is the only beginner's book one would need to begin working with C++ in my opinion, so that's why I recommended it.

    I would suggest to avoid ncurses since it's basically a C library, but I am hesitant to suggest so since I don't know much about Linux (seems to be very C oriented).

    In any case, the fact that you don't have a basic understanding of a vector makes me hesitant to recommend you to continue. You should probably be aware of the basic containers and algorithms first, so give Accelerated C++ a try. You'll be ready afterwards, I think. You can even practice applying your knowledge from the book into your program.
    Thanks Elysia for telling me this. It's something I need to hear. I just took a look at the beginning parts of this book and it looks like something I can digest. I'm going to try to make programs that are more suited to my experience level.

    I'll come back to this post when I am more ready.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 04:52 AM
  4. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 06:48 AM
  5. convert string to char array
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2006, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21