Read file and put content of lines into attribute of object
Pokemon in this example.
ConsoleApplication1.cpp
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
//The class we can read and write files is: fstream
#include <fstream>
#include <string>
#include "Pokemon.h"
using namespace std;
//use command line parameter, also check if you put command line parameter correctly
int main(int argc, char** argv)
{
if (argc != 2) {
cout << "Are you forgetting the command-line parameter?" << endl;
exit(0);
}
ifstream ifs;
ifs.open(argv[1], ios::in);
//check if the file do not exsit, use !ifs,
if (!ifs) {
cout << "Error! Please check filename." << endl;
} else {
string tmp;
//use getline to read, but getline do strings, we will need to convert it to int later
getline(ifs, tmp);
int n = stoi(tmp);
//we are making a array of pokemon pointers
//the first * means we are storing pokemon in pointer, the second * means we are making an array with lenth n
Pokemon** list = new Pokemon*[n];
for (int i = 0; i < n; i++) {
string name;
string evolve;
int hp;
//read the line until you see the symbol " | "
//we don’t need " | " for the last one because the file line format doesn’t contain " | " in the end
getline(ifs, name, '|');
getline(ifs, evolve, '|');
getline(ifs, tmp);
hp = stoi(tmp);
//array stores references of objects, when we instaitate pokemon object, it returns reference, the object itself is somewhere in memory.
//that's why we don't use pointer syntax when we instatiate individual pokemon here
list[i] = new Pokemon(name, evolve, hp);
}
ifs.close();
for (int i = 0; i < n; i++) {
list[i]->printStatus();
}
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Pokemon.cpp
#include "pch.h"
#include "Pokemon.h"
#include <iostream>
using namespace std;
Pokemon::Pokemon()
{
name = "Detective Pikachu";
evolvesFrom = "Made by Mewtwo";
hp = 10;
}
Pokemon::Pokemon(string n, string e, int h) {
name = n;
evolvesFrom = e;
hp = h;
}
void Pokemon::printStatus() {
cout << "My name is " << name << endl;
cout << "I evolve from " << evolvesFrom << endl;
cout << "My HP is " << hp << endl;
}
void Pokemon::setHP(int h) {
hp = h;
}
Pokemon::~Pokemon()
{
//nothing here
}
Pokemon.h
#pragma once
#include <string>
using namespace std;
class Pokemon
{
public:
Pokemon();
Pokemon(string, string, int);
~Pokemon();
void printStatus();
void setHP(int);
private:
int hp;
string name;
string evolvesFrom;
};