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;
}

Recommended Answers

All 3 Replies

Your loops make no sense.

You ask the user to enter a value into the variable name which you immediately write over from file.

You have a while loop that you break out of on the first time round.

This code:

if (!inputFile)
          {
            cout << "Animal not found.";
          }

makes no sense whatsoever. You're saying if there's something wrong with the input filestream (for example, it didn't open properly), then tell the user than the animal is not found. That's bizarre.

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

Possibly because you only read in the first set of animal data.

Do you see anything wrong with this code:

 inputFile >> name;
 // code I cut out
 cin >> name;

You ask the user to enter a value into the variable name which you immediately write over from file.

If you want to read in all the data from the file, you will need to put it all somewhere. How can you store every animal name into the same variable? It makes no sense.

Your algorithm is bad. How about you get from user the name of the animal to look for, then read the file, and keep reading the data from the file until the name read from file matches what the user entered? Essentially, you need to stop coding, step back, think about what you're trying to do and how to do it, and then code it.

There are quite a few problems with this program as it is written, but all of them can be fixed with some patience. The biggest issue I see is that you are reading in the name of the animal to be found, then overwriting that name repeatedly in the condition of the loop. You want something more like this:

            cout << "Please enter the name of the animal you are searching for: ";
            string petName;
            cin >> petName;

            while (inputFile >> name)
            {
                if (name == petName)
                {
                // get the file data here

Note the comment there, because that brings to light the second problem: you are reading the animal information only for the first animal, exactly once, and then printing out the data repeatedly. What you seem to want is to read the data only once the animal's name has been found, and then print out the results outside of the loop. This would make the code above look like:

            cout << "Please enter the name of the animal you are searching for: ";
            string petName;
            cin >> petName;

            while (inputFile >> name)
            {
                if (name == petName)
                {
                    inputFile >> visited;
                    inputFile >> cost;
                    average = cost / visited;
                    break;
                }
                else
                {
                    inputFile.ignore(1024, '\n');
                }
            }

            if (!inputFile)
            {
                cout << "Animal not found.";
            }
            else
            {
                //Display the animal name, times visited, and cost.
                cout << "The animal " << name << " was checked into the clinic " << visited << " times" << endl;
                cout << "and the total charges were $" << setprecision(2) << fixed << cost << " with the average charge being $" << average << "." << endl << endl;
            }

Here, the program goes through the loop, and if the name is found, it reads in the data and then breaks out of the loop. If it doesn't find it, it eats the data up to the next newline, and continues.

Presumably, you are supposed to loop the program as a whole to repeat the process until the user exits. This is simply a matter of adding a do/while loop around the code above, to ask the user for confirmation whether to continue or not.

            cout << "Continue (y/N)? ";
            cin >> repeat;
        }
        while (toupper(repeat) == 'Y');

There's just one rub, here: you also need to reset the file position and clear the file flags, so that you are starting over from the beginning of the file each time.

        do
        {
            inputFile.clear();
            inputFile.seekg(0);

Putting this all together is let as an exercise.

I can not express my gratutude to you both! Thank you both so much! I got so lost in what I was doing and getting all turned around I lost sight of where I was and where I should be. Thanks so much for helping.

As for having to loop the program, the professor said no to that. But thanks for the head's up on how to. That one of the things I've been wondering for awhile now anyways.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.