I am trying to use parallel arrays to store cards information . Firstly use a function that reads all cards information from a text file and insert them to parallel arrays in the main program using a pointer.
My code compiles, but the output is just blank. I am also confused how do I put the names of the respective card into one parallel array when some cards have more than one words. I have searched online for similar solutions but no one had to use pointers to insert them, and no one had a name more than 2 words. Hopefully someone can point me in the right direction with simple stuff (cannot use vector) that an intro to C++ person would be able to use.
my text file looks like this ( I have aligned them so that it is easier to see the information needed to be put into 5 parallel arrays):
eg:(Array1 Array2 Array3 Array4 Array5)
1 2 3 4
Abyss Devolos F0647 Balance NA SpeedStorm
Ace Dragon E7609 Attack NA HyperSphere
Anubion A2 E1057 Defense NA Dual-Layer
Vatryek Wing Accel B9492 Attack NA Burst
verify that you are actually accessing the file by just printing its contents to the screen as you read it in that while loop. If you can't do that, you can't do anything else.
is your file data accurate? It appears to be fixed width fields, in that case: you can read a line of the file (instead of using >> use getline) and slice it up (string substring function) at the correct offsets. String has a substring function, you can slice it with that.
that looks roughly (pseudocode) like this
1 2 3 4 5 6
while (.. getline into somestring)
{
*(name+x) = somestring.substring(offset, width)
*(ID+x) = ... etc
... etc
}
consider a return statement if the file was not read
if(!readFile)
{
cout << "Game database missing"<< endl; return;
}
file.close() will happen for you when the file object is destroyed (the function ends). I don't think you need it (but its ok to have it).
little stuff:
prefer {} to initialize.
const int size {4};
if you put main at the bottom, you don't need the headers for the functions. Saves a little aggravation.
I suspect the pointer junk is a derpy classroom requirement as are the arrays.
Why professors waste 2 - 3 weeks teaching how not to do things remains a mystery.
@jonnin, it never ceases to amaze and frustrate me to see people just starting out learning C++ being subjected to such unnecessary torture.
I know learning how to use pointers is an essential task for a programmer, but ignoring what C++ has in its toolbox is something the Spanish Inquisition would do if it were teaching C++ to noobs.
C ways of doing things is not something a "just rolled off the rutabaga lorry" student should be subjected to.
When someone is an intermediate student, with a lot of C++ knowledge under their belt, getting exposed to the challenges of what legacy code might present then becomes an easier task.
Learning good, modern C++ will make a programmer less likely to use "doing it the C way" as the default way to solve a problem.
my text file looks like this ( I have aligned them so that it is easier to see the information needed to be put into 5 parallel arrays):
No. You should show exactly the content of the file. Not the file contents changed so that it 'looks pretty'. The layout matters especially when dealing with elements that may contain spaces.
If the file is actually as shown with each element starting at a specific column, then that can be dealt with fairly easy. If, however, the actual file is something like:
Abyss Devolos F0647 Balance NA SpeedStorm
Ace Dragon E7609 Attack NA HyperSphere
Anubion A2 E1057 Defense NA Dual-Layer
Vatryek Wing Accel B9492 Attack NA Burst
if the fields are not fixed width and you are not in control of the file data, you have to get clever combined with an exact 'what can be where' description of the expected file format. For example if all the second column are hex numbers, and the first column can't contain hex numbers, you can figure that out, or you can find a reference point (eg if column 3 only has specific allowed words from a list, or whatever the N/A column can be when it is populated, or ???) to split from.
Text files that are not properly thought out when the format is designed absolutely suck, is what it comes down to. Text needs to be formatted with delimiters or fixed widths or something (eg xml tags) else that gives you what you need without having to exert tons of energy to handle it. But when the files lack these things, you do what you must or can with it.