0

I'm trying to create a game (tic tac toe) and I have to use a library. I used ncurses to make the appearance appealing. However, each time I implement ncurses into my code it runs but DOES NOT run my main code that starts the game.. it either exits out or does not allow me to see the user input.. What can I do? This is the code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include "board.h"
#include "players.h"
#include <ncurses.h>

using namespace std;

/**
 * Controls main function of the game
 */
int main(int argc, char** argv) {
    initscr();
    cbreak();
    refresh();      
    WINDOW* windowTest = newwin(1, 23, 3, 0);
    wprintw(windowTest, "Hello World");

    //refresh();
    wrefresh(windowTest);
    getch();
    delwin(windowTest);
    endwin();
    return 0;

    string name;
    cout << "What's your name: ";
    cin >> name;
    cout << "Nice to meet you, "<< name << ". Welcome to Tic Tac Toe!" << endl;

//Player is a object and does not matter here
    Player game;
    game.play();
    return 0;
};
E_net4
  • 27,810
  • 13
  • 101
  • 139
artuhrop
  • 1
  • 1
  • 4
    The issue should have been found with less than one minute of debugging. Either some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) or actually stepping through the code statement by statement in a debugger. – Some programmer dude May 13 '21 at 06:15
  • studying strategies is more important than studying tactics. – user1095108 May 16 '21 at 09:03

1 Answers1

1

The problem is these three statements in the middle of the main function, before you create your game object:

delwin(windowTest);
endwin();
return 0;

The delwin and endwin calls should be after the game.play() call. And the return statement at this location should be removed.


It seems that you took some "test" program, and just added a few statements at the end of the main function.

You need to do some better planning about your programs and how they work. Preferably starting with a list of requirements, creating an analysis from those requirements, and from the analysis create a design that you then implement.

And while test programs can be very useful, when you start on the actual project I recommend that you start from scratch instead. Then mistakes like this should not happen.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you! Also, yes this is a test program to see if it would work. I've tried everything but using the library is quite difficult. After using your suggestions (thank you!) I somehow got this: In function `main': main.cpp:(.text+0xd6): undefined reference to `Player::play()' /tmp/ccGWQ762.o: In function `Player::Player()': main.cpp:(.text._ZN6PlayerC2Ev[_ZN6PlayerC5Ev]+0x14): undefined reference to `Board::Board()' main.cpp:(.text._ZN6PlayerC2Ev[_ZN6PlayerC5Ev]+0x1f): undefined reference to `vtable for Player' collect2: error: ld returned 1 exit status ... should I compile differently? – artuhrop May 13 '21 at 06:27
  • @artuhrop That seems like you're not building with the source file containing the `Player` implementation. – Some programmer dude May 13 '21 at 06:29
  • @artuhrop Take a look at [this quetion](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) If it doesn't help you it might be better to ask a new question. – Lukas-T May 13 '21 at 06:30
  • It worked!! Oh my goodness!! Thank you so much! One more question, (if that is okay) as I type in for user input, I do not see the name of what I am typing.. is there a reason for this, or something that i'm doing incorrectly? – artuhrop May 13 '21 at 07:02
  • @artuhrop DOn't mix ncurses input/output with standard stream input/output. Use one or the other, but not both. – Some programmer dude May 13 '21 at 07:19