HELP! Can't deconstruct.

I currently have the code for a bowling program in C++ and I am very much a novice in C++ I need to use this program to develop and change it into my own, such as removing stuff too advanced and understanding how it works, but currently I am completely at a loss as there is very little in the main.cpp and something I do not understand at all other than it is checking what the OS the program is on, but I can't work out how to run this as the int main seems to reference it in some way
if possible can someone please point out the obvious as to why I cant remove it, tell me how to remove it, or atleast start me on my way to getting rid of it, I just want the main to intialise the program but as it stands I can't, further more, the program does not seem to want to debug or build regardless of what I do, but I have an .exe file that shows me the program works as its supposed to what am I doing wrong, I am currently using visual studio

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
MAIN.CPP

#include <string>
#include "Program.h"

std::string extractAppPath(const char *fullPath)
{
#ifdef __APPLE__										//Mac
	std::string s(fullPath);
	for (int i = 0; i < 5; ++i)
		s = s.substr(0, s.rfind("/"));
	return s + "/";
#else
	const std::string s(fullPath);
	if (s.find("/") != std::string::npos)				//Linux
		return s.substr(0, s.rfind( "/" )) + "/";
	else if (s.find("\\") != std::string::npos)			//Windows
		return s.substr(0, s.rfind( "\\" )) + "\\";
	return s;
#endif
}

int main(int argc, char *argv[])
{
	std::string path = extractAppPath(argv[0]);
	Program prog(path.c_str());
	if (prog.initialize())
		prog.runGame();
	prog.shutdown();
	return 0;
}


PROGRAM.H

#ifndef PROGRAM_H
#define PROGRAM_H

#include <string>
#include "BowlingGame.h"

class Program
{
public:
	Program(const char *);
	~Program();

	int initialize();
	int runGame();
	int shutdown();

private:
	static const char *_appDirPath;
	bool _initialized;
	bool _running;
	BowlingGame game;
};

#endif


PROGRAM.CPP

#include "Program.h"
#include "BowlingGame.h"

const char *Program::_appDirPath = 0;

Program::Program(const char *_appPath)
{
	_appDirPath = _appPath;
	_initialized = false;
	_running = false;
}

Program::~Program()
{
	if (_initialized)
		shutdown();		//ensure all data is freed correctly
}

int Program::initialize()
{
	if (_initialized)
		return 1;	//already initialized
	game.initialize();
	_initialized = true;
	return 1;		//successful initialization
}

int Program::runGame()
{
	if (!_initialized && !initialize())
		return 0;	//unable to initialize game
	if (_running)
		return 0;	//game already running

	_running = true;
	while (_running)
	{
		if (!game.update())
			_running = false;
	}
	_running = false;	//finish running the game
	return 1;
}

int Program::shutdown()
{
	if (_initialized)
	{
		_initialized = false;
		_running = false;
		return 1;	//successful shutdown
	}
	return 0;	//not initialized
}
  Put the code you need help with here.


BowlingGame.h
#ifndef BOWLING_GAME_H
#define BOWLING_GAME_H

#include <string>
#include <vector>
#include "Player.h"

using namespace std;

class BowlingGame
{
public:
	BowlingGame();
	~BowlingGame();

	int initialize();
	int update();
	int shutdown();

	const vector<Player> getPlayers() const { return _players; }
	int getCurrentTurn() const { return _turn; }
	int getGameNumber() const { return _gameNumber; }
	int getLaneID() const { return _laneID; }

private:
	enum NAME_VALIDATION
	{
		NAME_LENGTH_INVALID,
		NAME_ALREADY_TAKEN,
		NAME_IS_VALID
	};
	int nameIsValid(const string &name);
	int _gameNumber;
	int _laneID;
	vector<Player> _players;
	int _turn;
	int _frame;
};

#endif
Last edited on
Not really clear what you're trying to remove.

The first thing main does is to extract the path to the application object file.
argv[0] contains the fully qualified object file name. How this is isolated into just a path name depends on the specific operating system. Mutliple methods are used to determine this. A compile time toggle is used to determine if the program was compiled on Apple. If not Apple, then whether Linux or Windows depends on whether a / or \ is found in the object file name.

If you want to remove the operating system dependency, then you need to change lines 8-20 accordingly. You may or may not want to remove the extraction of the pathname altogether since the extracted pathname is passed to Program's constructor (line 26), although _appDirPath does not appear to be used.

If this is not what you're asking, then please clarify your question.
I think you've just made it ever so slightly clearer, all the initial function is doing is seeing if the file path (where the program is run from) has a "/" or a "//" because of the differences between windows and mac file systems (to put it verrrrrryyy simply)

basically yes. I want to remove this, I don't want the "operating system dependency" as you've called it because it will only be run on Windows, I'm trying to heavily simplify the program.

I think the issue with just deleting the code is that the result of the function and int main, in main.cpp is then pushed to program.cpp which THEN initializes the "int initialize" function in BowlingGame.cpp which actually begins the program, I don't know what to remove or write to straight up initialize the BowlingGame function as I'm a dolt.
As I said above, the constructor for Program is expecting a path to the program object file, but as far as I can see it never does anything with that value. Since you haven't provided bowling.h, I can't tell if the BowlingGame class needs the application path.

So if the assumption is true that application path is not used, then you can eliminate lines 6-21, 25, 53, 67. Your constructor for Program then becomes:
1
2
3
4
Program::Program ()
{   _initialized = false;
    _running = false;
}


If BowlingGame::Initialize() requires the _appDirPath variable, then the only simplification you can do is in the extractAppPath function.
1
2
3
4
5
6
std::string extractAppPath (const char *fullPath)
{   const std::string s(fullPath);
    if (s.find("\\") != std::string::npos)			//Windows
        return s.substr(0, s.rfind( "\\" )) + "\\";
    return s;
}


I actually don't see any reason that the application needs to isolate the application path. If you attempt to open an unqualified file name, the run time will look in the directory where the object file is.

BTW, it's a good idea to avoid variable names that begin with _. Variables that begin with _ are reserved by the standard for use by the compiler.



I have now included the bowling.h and bowling.cpp if it helps you know what I need to do,

I am trying to remove the program.h/.cpp files completely if possible, as well as move on to remove a couple of the other files, but thats another story, Right now I can't see much use for the progam files other than cmaybe clearing the arrays upon shut down? I don't know?
Also, what would I do with line 26-30 as it's function references path?
Good instinct to remove the Program class. I don't see that it adds any particular value other than to tell you if the program is initialized or running.

Based on what you posted for bowlinggame.h, I would get rid of program.h and program.cpp and change main as folllows:

1
2
3
4
5
6
7
8
9
int main (int argc, char *argv[])
{    BowlingGame  game;

    game.initialize (); 
    while (game.update())   // continue until false is returned
        ;
    game.shutdown ();
    return 0;
}


That is fantastic!! It works completely! I had it working without the path nonsense, but now I can get rid of the progam files completely! Thank you so much!
Is there still any need for the int argc and char? in the title?
Not unless you want to pass something from the command line into your game.
It does require user input so I guess I'll leave it there.
Topic archived. No new replies allowed.