Any help is appreciated.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Any help is appreciated.

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    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

    <code>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;

    int main ()
    {
    ifstream inFile;
    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.open(filename.c_str());

    if (inFile)
    {
    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;

    inFile.close();
    }
    }
    else
    {
    cout << "You did not enter a valid text file. \n" << endl;
    }
    return 0;
    }
    </code>

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Any help is appreciated.

    Quote Originally Posted by Nick007 View Post
    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.
    Are you sure it reads the second line? Maybe you should look closer.

    Did you write down the steps that the program should take before writing any code? If so, did you step through your program with the debugger to check that each step is performed correctly? That's the approach you need to take to write working programs. The first step is to explain the steps of your program in normal English.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,741

    Re: Any help is appreciated.

    Before posting code, please format it properly with indents etc and use code tags. These are [ code] and [ /code] (without the space!). Go Advanced, select code then click '#'

    If there are 100 lines in the file and the name of the animal occurs, say, on line 92, how would you keep reading lines from the file until the animal's name is found or end of file is reached? (Hint - you need a loop).

    As D_Drmmr said, you first need to design the program and check the program design is correct by working through it and making changes to the design as needed. Only when you are happy with the design do you then start coding the program from the design. You then use the debugger to make sure the program works as per the design. If the design is correct and the code correctly implements the design then the program will work.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center