Hello all. I am working on a program for class and having a bit of a problem. The program is supposed to ask the user to enter a text file in which read from. Then asks the user for the name of the animal you are searching for. Then if all goes right, tells the user how many times the animal has checked into the vet's office, total cost of visited followed by the avgerage. That's where things go haywire. I have the txt file made just fine. And when I go through the program when I was testing it, it only replies with the one animal in the list with the first set of numbers. I have been going round and round in my text book and the internet, and I have gotten myself so completely lost. If someone could point me in the right direction or tell me exactly what I am doing wrong I would really appreciated it!
here's what is in the text file.
Talon 3 30
Leo 4 45
Mandy 6 150
Phantom 9 175
Cody 13 300
Here's the code.
//This program is designed to take an input file and search for a word in
//said file and any associated data with that word.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
ifstream inputFile;
string filename, name;
int number, visited;
float cost, average;
//Get the file name from the user.
cout << "Welcome to The Happy File Data Reader!\n";
cout << "Please enter the file name you wish to use.\n";
cin >> filename;
//Code to open the file.
inputFile.open(filename.c_str());
if (inputFile)
{
inputFile >> name;
inputFile >> visited;
inputFile >> cost;
average = cost / visited;
cout << "Please enter the name of the animal you are searching for. \n";
cin >> name;
while (inputFile >> name)
{
//Display the animal name, times visited, and cost.
cout << "The animal " << name << " was checked into the clinic " << visited << " times\n";
cout << "and the total charges were $" << setprecision(2) << fixed << cost << " with the average charge being $" << average << ".\n" << endl;
break;
}
if (!inputFile)
{
cout << "Animal not found.";
}
inputFile.close();
}
else
{
cout << "You did not enter a valid text file. \n" << endl;
}
return 0;
}