Fairly simply question. Below is my code and I'm trying to read the first word for each line on a text file. I am pretty much new to C++ and basically illiterate, so a vivid explanation would be really helpful. Thanks in advance!
The output I want is:
Warrior (5 times), Status, Battle (5 times), Status (separate lines of course)
But what I get is:
Warrior (4 times), Status, Battle (5 times), Status
Here is my code:
int main() {
string readText;
string command;
string firstName;
string skip;
int strength;
ifstream inFile("warriors.txt");
if (!inFile) {
cout << "File will not open" << endl;
}
else {
while (inFile >> readText) {
getline(inFile, command);
inFile >> command;
if (command == "Warrior") {
cout << "warrior" << endl;
}
else if (command == "Battle") {
cout << "battle" << endl;
}
else if (command == "Status") {
cout << "status" << endl;
}
}
}
}
Another question on the side, why is it that when I change:
while(inFile >> readText)
to
while(inFile)
My output is now: Warrior (4 times), Status, Battle(5 times), Status, Status