For one of my projects, I have to read in this given textfile into a vector thro
ID: 3732215 • Letter: F
Question
For one of my projects, I have to read in this given textfile into a vector through C++
Aegis Dragon,fire,5,9
Aeon Wyvern,water,4,7
Albino Dragon,ice,5,10
Almerald Dragon,wind,5,10
Anagallis Dragon,wind,4,7
Antarean Dragon,acid,4,8
Aria Dragon,water,3,6
Arsani Dragon,fire,1,1
Astrapi Xenowyrm,wind,1,1
Azure Glacewing Dragon,wind,3,5
Baikala Dragon,fire,4,8
Balloon Dragon,water,4,8
Bauta Dragon,acid,4,8
Black Dragon,acid,5,10
Black Tea Dragon,ice,5,10
Blacktip Dragon,water,5,10
Bleeding Moon Dragon,fire,5,10
Blue-Banded Dragon,ice,1,1
Bolt Dragon,acid,3,5
Bright Pink Dragon,water,5,10
Bright-Breasted Wyvern,fire,1,1
Brimstone Dragon,ice,3,6
Brute Dragon,acid,3,6
Caligene Dragon,fire,3,6
Candelabra Dragon,wind,2,3
Canopy Dragon,wind,5,9
Carina Dragon,ice,4,8
Carmine Wyvern,acid,5,10
Cassare Dragon,fire,1,2
Cavern Lurker Dragon,acid,4,7
Celestial Dragon,wind,4,7
Cheese Dragon,water,1,1
Chrono Xenowyrm,fire,4,8
Common Pygmy,wind,3,5
Copper Dragon,fire,2,4
Dark Green Dragon,fire,1,2
Dark Lumina Dragon,ice,1,2
Dark Myst Pygmy,fire,4,8
Day Glory Drake,wind,1,1
Daydream Dragon,acid,1,2
Deep Sea Dragon,ice,4,8
Desipis Dragon,acid,3,5
Diamondwing Dragon,fire,3,5
Dorsal Dragon,acid,3,5
Duotone Dragon,water,4,7
Dusk Pygmy,acid,4,7
Electric Dragon,acid,5,10
Ember Dragon,fire,3,5
Falconiform Wyvern,ice,1,1
Fell Dragon,ice,5,10
Fever Wyvern,wind,2,4
Fire Gem Dragon,fire,1,1
Flamingo Wyvern,water,1,1
Frilled Dragon,fire,3,5
Frostbite Dragon,ice,4,7
Gaia Xenowyrm,acid,5,9
Garland Dragon,ice,2,4
Gemshard Dragon,wind,4,7
Geode Dragon,fire,1,1
What code would be needed to achieve this? All of this should be in one vector and each individual element should be accessible
Explanation / Answer
#include #include #include #include using namespace std; struct Data { string name; string power; int n1; int n2; }; int main() { ifstream in("data.txt"); // open file to read string line; vector vec; if(in.is_open()) { // if file was succesfully opened while(getline(in, line)) { // read from file line by line // load line into Data object and add it into vector object here Data data; // prepare data here vec.push_back(data); } } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.