0

I have a problem printing my string using PDcurses. Compiler shows no error, but when I open my program it immediately stops working. When I print it using cout it works normally.

main.cpp

#include "Item.h"
#include "Character.h"
#include "Inventory.h"
#include<iostream>
#include <curses.h>

using namespace std;

int col = 0;
int row = 0;

int main() {
    Character player("Roland", 1, 500, 200, "Knight");
    char* playerChar = (char*)alloca(player.drawCharacter().size() +1);
    memcpy(playerChar, player.drawCharacter().c_str(), player.drawCharacter().size() + 1);
    //cout << playerChar;

    //playerChar = "dasdasdasdsadasdsadsad";

    initscr();
    getmaxyx(stdscr, row, col);
    mvprintw(6,6,playerChar);
    getch();
    endwin();

    return 0;
}

my function

string Character::drawCharacter() {
    ifstream file ("Character/Knight_axe.txt");             //Open file

    string lines = "";                                  //Store all lines

    if (file){                                          //Check if everything is good
        while (file.good ()){
        string TempLine;                                //Temp line
        getline (file , TempLine);                      //Get temp line
        TempLine += "\n";                               //Add newline character

        lines += TempLine;                              //Add newline
        }
    } else {
        lines = "ERROR File does not exist.";           //Return error
    }

    file.close ();                                      //Close file
    return lines;                                       //Print it to the screen
}
jastka4
  • 63
  • 2
  • 3
  • First of all I don't see the need to use `alloca` (or any other dynamic allocation) or a temporary variable for a `char*`. All you need to do is pass `player.drawCharacter().c_str()` directly to the `mvprintw` function (or better yet pass it as an argument to the `"%s"` format). Also please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Your loop with `file.good()` have the same problems. – Some programmer dude Jun 12 '17 at 13:09
  • As for your question and problem, can you please elaborate on the whole " when I open my program it immediately stops working"? Do you get a *crash*? Do you get wrong results or output? Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Also please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Jun 12 '17 at 13:11
  • It just crashes and shows nothing. I also tried to debug it and it only shows that my source is not available (pdcurses). And I tried to do it using c_str() but it showed me some random values. Also player view cannot be a constant, because I will need to change it few times while the program is working. – jastka4 Jun 12 '17 at 13:19
  • A quick side-note: If the debugger says that your source is missing then you are not building with debug information. For GCC add the `-g` flag when building. – Some programmer dude Jun 12 '17 at 13:29
  • Also, every time you call `drawCharacter` you will load the data from the file. With the code you show that will be *three times* you open the file and read the data. You should probably do it in a separate step *first* and store the data in a member variable that `drawCharacter` just returns. – Some programmer dude Jun 12 '17 at 13:32
  • I know that, it was just for a test, because I don't know why is it not working. I try to solve it for two days now :( – jastka4 Jun 12 '17 at 13:42
  • I've just switched to Linux and it works fine, even `c_string` function. I won't use Win for C++ anymore ;) – jastka4 Jun 12 '17 at 15:10
  • You should say what compiler and what version of PDCurses you're using. – William McBrine Jun 13 '17 at 20:23

0 Answers0