-2

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

user4581301
  • 33,082
  • 7
  • 33
  • 54

1 Answers1

1

Because file.getline(animalInfo, size, '.'); specified a delimiter different from the default of newline, '\n', nothing removes the newline at the end of Alice:Anaconda:10323:1..

When Boris:Bull:23456:2. is read, file.getline(animalInfo, size, ':'); finds the left over newline and puts it into animalInfo along with everything else it finds up to the next ':'. animalInfo contains "\nBoris", and when animalInfo is printed the newline does its job and inserts a new line, resulting in

Animal Name:

Boris

Simplest solution is to drop in a file.ignore(numeric_limits<streamsize>::max(), '\n'); (note that <limits> must be included) after file.getline(animalInfo, size, '.'); to consume the newline.

An alternative is to read the whole line into a std::string with a std::getline, place the line in a std::istringstream and then parse the stringstream for the line's worth of data. This makes it easier to detect and recover from mistakes. Use Option 2 of this answer for inspiration.

Don't forget to check the input for validity, and handle (possibly discard) any invalid entries found, as you read the file and add an easy out to

while (counter < maximumNumbersOfAnimal)

should the file end before maximumNumbersOfAnimal is reached. Otherwise you'll find the program is recording garbage.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54