So I'm a complete beginner in C++ and my problem is not getting the output that I expected. The program should print out the content/data from Animals.txt file line by line and it works well. The only problem I'm having is the name (Boris, Calvin, Dusty and Eddie) is supposed to be next to "Animal Name: " line but it is not. How do I achieve this?
This is the Animals.txt file:
Alice:Anaconda:10323:1.
Boris:Bull:23456:2.
Calvin:Cat:01320:3.
Dusty:Dinosaur:00001:5.
Eddie:Eagle:57429:4.
This is the code:
while (counter < maximumNumbersOfAnimal)
{
clinic.animal[counter].number = counter;
cout << "Animal Number: " << clinic.animal[counter].number << endl;
file.getline(animalInfo, size, ':');
clinic.animal[counter].name = animalInfo;
cout << "Animal Name: " << clinic.animal[counter].name << endl;
file.getline(animalInfo, size, ':');
clinic.animal[counter].type = animalInfo;
cout << "Animal Type: " << clinic.animal[counter].type << endl;
file.getline(animalInfo, size, ':');
clinic.animal[counter].registrationNumber = stoi(animalInfo);
cout << "Animal Registration Number: " << clinic.animal[counter].registrationNumber << endl;
file.getline(animalInfo, size, '.');
clinic.animal[counter].problemNumber = stoi(animalInfo);
cout << "Animal Problem Number: " << clinic.animal[counter].problemNumber << endl;
counter++;
}
This is the output:
Animal Number: 0
Animal Name: Alice
Animal Type: Anaconda
Animal Registration Number: 10323
Animal Problem Number: 1
Animal Number: 1
Animal Name:
Boris
Animal Type: Bull
Animal Registration Number: 23456
Animal Problem Number: 2
Animal Number: 2
Animal Name:
Calvin
Animal Type: Cat
Animal Registration Number: 1320
Animal Problem Number: 3
Animal Number: 3
Animal Name:
Dusty
Animal Type: Dinosaur
Animal Registration Number: 1
Animal Problem Number: 5
Animal Number: 4
Animal Name:
Eddie
Animal Type: Eagle
Animal Registration Number: 57429
Animal Problem Number: 4
What I expect:
Animal Number: 0
Animal Name: Alice
Animal Type: Anaconda
Animal Registration Number: 10323
Animal Problem Number: 1
Animal Number: 1
Animal Name: Boris
Animal Type: Bull
Animal Registration Number: 23456
Animal Problem Number: 2
Animal Number: 2
Animal Name: Calvin
Animal Type: Cat
Animal Registration Number: 1320
Animal Problem Number: 3
Animal Number: 3
Animal Name: Dusty
Animal Type: Dinosaur
Animal Registration Number: 1
Animal Problem Number: 5
Animal Number: 4
Animal Name: Eddie
Animal Type: Eagle
Animal Registration Number: 57429
Animal Problem Number: 4