April 7th, 2013, 01:34 AM
Any help is appreciated.
Ok, so I have this programming assignment to do. I'm having a program with it. I'm not asking to do the work for me in anyway. Just hoping on a little help to tell me what I am doing wrong. Basically the program is supposed to ask the user for an input file. Then if the input file loads successfully, then ask user to input the name of the animal. (Kind of a vet program) Then it responds with telling the user how many times the animal checked in, total cost, and avg of the cost per times visited. This was the easy part. I also made a program to make the text file. Everything runs great until you ask for the animal's name to read from the file. No matter what I put in for the name, it only reads the 2nd line from the file.
this is what the text file reads as:
Talon 3 30
Leo 4 45
Mandy 6 150
Phantom 9 175
Cody 13 300
so the line I get when I enter the name "Talon" for example gets me "The animal Leo was checked into the clinic 3 times and the total charges were $30.00 with the average charge being $10.00." which is great if I was trying to get Leo's info, but not if I try for anything else.
Here's my code. Any help for be appreciated. Thanks
using namespace std;
int main ()
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.
inFile >> name;
inFile >> visited;
inFile >> cost;
average = cost / visited;
cout << "Please enter the name of the animal you are searching for. \n";
cin >> name;
//To read what's in the file.
inFile >> 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;
cout << "You did not enter a valid text file. \n" << endl;
Click Here to Expand Forum to Full Width