hw02.cpp - else if(warrior1.power == 0&&...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//homework 2, Channi Greenwall, the purpose of this code is to read from a file, and take the information read to create and battle "warriors" #include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; struct Warrior { string name; int power; }; //this function simply displays the strengths and names of each warrior void status(const vector<Warrior>& display){ cout<<"There are:"<<" "<<display.size()<<" warriors"<<endl; for (size_t i=0; i<display.size(); ++i){ cout<<"warrior: "<<display[i].name<<", strength: "<< display[i].power<<endl; } } //this function battles two warriors and re assigns their strengths after the battles void battle(Warrior& warrior1, Warrior& warrior2){ cout<<warrior1.name<<" battles "<<warrior2.name<<endl; if (warrior1.power>warrior2.power){ if (warrior2.power == 0){ cout<<"He's dead "<<warrior1.name<<endl; } else{ warrior1.power -= warrior2.power; warrior2.power = 0; cout<<warrior1.name<<" defeats "<<warrior2.name<<endl; } } else if (warrior1.power<warrior2.power){ if (warrior1.power == 0){ cout<<"He's dead "<<warrior2.name<<endl; } else{ warrior2.power -= warrior2.power; warrior1.power = 0; cout<<warrior2.name<<" defeats "<<warrior1.name<<endl;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } } else if (warrior1.power == 0 && warrior2.power==0){ cout<<"Oh, NO! They're both dead! Yuck!"<<endl; } else if (warrior1.power==warrior2.power){ cout << "Mutual Annihilation: " << warrior1.name << " and "<<warrior2.name<<" die at each other's hands"<< endl; warrior1.power =0; warrior2.power=0; } } int main(){ ifstream file("/Users/channigreenwall/Desktop/C++/hw02/warriors.txt"); if (!file) { cout << "Could not open the file.\n"; } vector<Warrior> AllWarriors; string command; while (file>>command){ if (command == "Warrior"){ string name; int power; Warrior normal; (file>>name);//read into name normal.name=name;//assign name to name (file>>power); normal.power=power; AllWarriors.push_back(normal); } else if(command == "Status"){ status(AllWarriors); } else if (command =="Battle"){ string name1; file>>name1; string name2; file>>name2; size_t index1; size_t index2; for (size_t i=0; i< AllWarriors.size(); i++){ if (AllWarriors[i].name==name1){ index1=i; } if (AllWarriors[i].name==name2){ index2=i; } } battle(AllWarriors[index1], AllWarriors[index2]); } } }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern