Pointers and data files.
I am building a Battletech board game simulation and have run into a snag right from the start. Keep in mind that I am a noob .
I have a class called Mech. In this class are all the variables I need to associate with a Mech. With this class I define Mech Player1 and Mech Player2.
Now, I have a txt file for each model of Mech I have in the game. In the text file I have all the values for the variables in the Mech class. Then I have the player choose a mech through a menu and then read the values from the text file into the variable. Pretty basic.
My problem is that I can''t seem to figure out how to have a seperate function for reading in each player''s mech. If I define the variables in the function then they are local to that function. Same if I assign pointers to the variables in the functions. Then the pointer variables are local also. I need to use these values all throughout my program. So, do I HAVE to define the variables in my main function or is there a different way to do it? I know pointers are involved somehow. I just can''t figure out the correct procedure.
To sum up, I need to define some variables in a function other then main and then have them available to the rest of the program. Maybe just use Global variables? But, isn''t that bad programming practice?
I just can''t wrap my brain around it yet . God, this is long. Sorry.
What language ?
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Fruny
What language ?
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
c++ fruny, it''s always c++
Rate me up.
Use a class or a struct and have the function pass back a pointer to it.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Aftermath - many people here only think they program in C++, unfortunately.
// Header
// Source
Or, alternatively
// Header
// Source
Or even
// Header
// Source
And other variations on the theme, taking stream parameters, taking filenames, as member functions, as constructors, as explicit constructors, as static members returning a pointer to a new object ...
It all depends on how you want to call those functions.
HTTP 500 retry 6
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
// Header
#include <iosfwd>#include <string>class Mech{ std::string name; int model int weight; friend std::istream& operator>>( std::istream&, Mech& );};
// Source
#include <iostream>#include <fstream>#include <string>#include "header.h"using namespace std;istream& operator>>( istream& is, Mech& m ){ return is >> m.name >> m.model >> m.weight;}int main(){ ifstream ifs( "data.txt" ); Mech Player1; ifs >> Player1; Mech Player2; ifs >> Player2;}
Or, alternatively
// Header
#include <iosfwd>#include <string>class Mech{ std::string name; int model int weight;public: void Load( const std::string& filename );};
// Source
#include <iostream>#include <fstream>#include <string>#include "header.h"using namespace std;void Mech::Load( const string& filename ){ ifstream ifs( filename.c_str() ); ifs >> name >> model >> weight;}int main(){ Mech Player1; Player1.Load( "data1.txt" ); Mech Player2; Player2.Load( "data2.txt" );};
Or even
// Header
#include <iosfwd>#include <string>class Mech{ std::string name; int model int weight;public: explicit Mech( std::istream& ); // explicit avoids implicit conversion.};
// Source
#include <iostream>#include <fstream>#include <string>#include "header.h"using namespace std;Mech::Mech( istream& is ){ is >> name >> model >> weight;}int main(){ ifstream ifs( "data.txt" ); Mech Player1( ifs ); Mech Player2( ifs );};
And other variations on the theme, taking stream parameters, taking filenames, as member functions, as constructors, as explicit constructors, as static members returning a pointer to a new object ...
It all depends on how you want to call those functions.
HTTP 500 retry 6
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement