You must pay better attention to the
type of things. Take line 32 for example:
marks[fCounter] = coordX;
The
types of the things you are manipulating are:
string = unsigned int
...which does not do what you think it does. (You
should be getting a compile error.)
You should be aware, also, that you are not making good use of your 'marks' array -- its values are not homogenous. If a mark needs to contain the following information:
· string 'name'
· string 'type' (whatever that is)
· unsigned 'x coordinate'
· unsigned 'y coordinate'
then you should put them in a structure:
1 2 3 4 5 6 7
|
struct mark_t
{
string name;
string type;
unsigned x;
unsigned y;
};
|
and then a list of marks is:
1 2
|
// A list of, at max, 400 marks
mark_t marks[ 400 ];
|
I recommend you use a
deque or
vector instead of a simple array. They have many advantages.
1 2
|
#include <deque>
deque <mark_t> marks;
|
In any case, you then need to make your routine properly handle a mark. You can create a constructor for your mark:
1 2 3 4 5 6 7
|
struct mark_t
{
...
mark_t( unsigned x, unsigned y, const string& name, const string& type ):
name( name ), type( type ), x( x ), y( y )
{ }
};
|
Adding a mark to the 'array' is then simple:
53 54
|
cin >> coordY;
marks.push_back( mark_t( coordX, coordY, markName, type ) );
|
Your file handling is also specious. Lines 13 and 14 create new, unassigned file streams. 'inFile' will never be open on line 16. You don't need 'inFile' anyway. (BTW, you also fail to return a value from the function.) Here is an example function that takes an existing file (supposedly already opened) and writes a mark to it. It outputs the same way your function does.
1 2 3 4 5 6 7
|
void write_mark_to_file( ofstream& file, mark_t mark )
{
if (!file.is_open()) ...
file << mark.name << " - " << mark.type << endl;
file << mark.x << ", " << mark.y << endl;
}
|
This creates a file like:
mark 1 - bubble
10, 72
markus - top dog
99, 100
|
Notice that the marks are spread over two lines in your text file. A simple CSV kind of layout might suit you better...
If you overload your 'mark_t' operators properly, you can read and save an entire set of marks, or just one mark, easily. Here are a couple of CSV file handling links that may help you, in order of most relevance.
http://www.cplusplus.com/forum/beginner/28109/#msg151349
http://www.cplusplus.com/forum/general/17771/#msg89751
Obviously, you can easily change how a mark is read from and written to file, if you want to keep your two-line method. Also keep in mind that when you let the user name your mark and its type without any validation, the user can type things in that will make it very hard to separate the mark name and type when you read it back from file.
Hope this helps.