Firstly, I'm using DEVC++, my goal of this code is to be able to read a text file into an array of structs. My text file is written like this: animalName:animalType:RegistrationNo:ProblemNo.
My issue with the below code is that it only seems to be running the while loop once.
I have looked up similar code but it uses to_string() and stoi but I don't think DEVC++ runs C++11 so I was wondering if there's an easy fix to my existing code or if there's another way to accomplish reading a text file which is made up of strings and ints
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#define MAX 100
using namespace std;
struct Animal
{
string animalName;
string animalType;
int Registration;
int Problem;
};
int main()
{
Animal ani[MAX];
ifstream infile;
int i = 0;
infile.open("Animals.txt");
if (!infile) {
cout << "Unable to open file";
exit(1);
}
int count = 0;
while (infile.good()) {
getline(infile, ani[i].animalName, ':');
getline(infile, ani[i].animalType, ':');
infile >> ani[i].Registration, ':';
infile >> ani[i].Problem, '.';
count++;
i++;
}
infile.close();
for (int i = 0; i < count; i++) {
cout << ani[i].animalName << " ";
}
for (int i = 0; i < count; i++) {
cout << ani[i].animalType << " ";
}
for (int i = 0; i < count; i++) {
cout << ani[i].Registration << " ";
}
for (int i = 0; i < count; i++) {
cout << ani[i].Problem<< " ";
}
return 0;
}