0

how do i assign these files

i have the 2 files wizard.txt and sorceress.txt how do i assign them to appliicabble classes ie wizCard and sor#Card? #include "stdafx.h" #include <iostream> #include <string> #include <fstream> using namespace std; class sorCard { public: int sType; string sName; int sAttk; int sDef; }; class wizCard { public: int wType; string wName; int wAttk; int wDef; }; const int ARRAYROW = 30; const int ARRAYCOL = 4; const int ROUNDSTART = 0; const int SIZE = 30; int main() { ifstream inFile; inFile.open("sorceress.txt");//opens sorceress cards file// if (!inFile) { cout << "Unable to open file"; system("pause"); } else { cout << "Files opened successfully" << endl; } inFile.open("wizard.txt");//opens wizard cards file// if (!inFile) { cout << "Unable to open file"; system("pause"); } else { cout << "Files opened successfully" << endl; } sorCard sorDeck[SIZE];//creates deck for sorceress for (int x = 0; x <= ARRAYROW; x++) { inFile >> sorDeck[1].sType >> sorDeck[1].sName >> sorDeck[1].sAttk >> sorDeck[1].sDef;//loads file into array } while (inFile >> sorDeck->sType >> sorDeck->sName >> sorDeck->sAttk >> sorDeck->sDef) { cout << sorDeck->sType << "," << sorDeck->sName << "," << sorDeck->sAttk << "," << sorDeck->sDef << endl; } inFile.close(); cout << endl << sorDeck[2].sType <<sorDeck[2].sName; wizCard wizDeck[SIZE];//creates deck for wizard for (int x = 0; x <= ARRAYROW; x++) { inFile >> sorDeck[1].sType >> sorDeck[1].sName >> sorDeck[1].sAttk >> sorDeck[1].sDef;//loads file into array } while (inFile >> wizDeck->wType >> wizDeck->wName >> wizDeck->wAttk >> wizDeck->wDef) { cout << sorDeck->sType << "," << sorDeck->sName << "," << sorDeck->sAttk << "," << sorDeck->sDef << endl; } inFile.close(); cout << endl << wizDeck[2].wType << wizDeck[2].wName; int sorHealth = 30; int wizHealth = 30; int round = 30; while (sorHealth > 0 || wizHealth > 0) { //program name cout << "Card Gam

11th Apr 2018, 2:40 PM
Matthew Alan Gibson-Storey
Matthew Alan Gibson-Storey - avatar
1 Resposta
0
You may read the data into an array or any other suitable container of type wizCard and sorCard. All you need to do this is declare a method that can read the data from a stream which we shall be able to pass to the method as argument. This is necessary as we may need to read multiple elements from the same file, and moving the pointer to the respective position before each operation can be difficult. So, for a sample 'wizard.txt' : 1 Gandalf 100 60 2 Ricochet 80 80 The method (to be placed inside the class wizCard, under public scope) will take the form: void Get(fstream& file) { file>>wType>>wName >>wAttk>>wDef; } And a sample read operation in main would look like: ifstream readfile; readfile.open("wizard.txt"); int no_of_cards=2; vector<wizCard> wPack(no_of_cards); for(wizCard& c:Pack) { c.get(readfile); } readfile.close(); A similar merhod and a call in main would be required for the class sorCard. The problem in your code was just that you used a single object to store all data, and the pointer to the first element still points to nothing. which leads to the probable error. I posted the code fragment above to make the reading task simpler for you.
11th Apr 2018, 6:37 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar