Thread: Files Program... really lost

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    16

    Files Program... really lost

    Hello everyone, I'm having some trouble with a program with this description:

    Write a program that prompts the user to enter a name, searches a file (teacherlist.txt) for that name, and outputs to the screen the teacher’s name and room number. .

    Note that I already have a huge text file filled with names and room numbers. If you would like to see it, i will be glad to show you.

    Basically, i have some code down, though for the most part I am lost. If anyone could guide me a bit, i would truly appreciate.... im not sure how to go about this at a....


    *This is what i have so far...

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstring>
    #include <math.h>
    
    void main()
    {
    
    ifstream in_file;
    
    int a, name_length, count;
    string find_teacher;
    char list;
    
    // Opens the teacherlist.txt file
    in_file.open("teacherlist.txt");
    
    
    
    cout << "Enter in the teacher's last name to obtain room information" << endl;
    cin  >> find_teacher;
    
    name_length = find_teacher.length();
    getline(in_file, list);
    
    /*
    for(int i=0; i<name_length; i++)
    {
    cout << find_teacher[i] << endl;
    }
    */
    
    // Sets All array slots to 0
    a=0;
    
    /* Sets Count to zero so it can be used to find whether or not i have found the
       name or not */
    count=0;
    
    while(!in_file.eof())
    {
    
    while(list!=',')
    {
    
    if(list[a]==find_teacher[a])
    {
    a++;
    count++;
    }
    
    if(list[a]!=find_teacher[a])
    {
    // Since it found a letter that doesn't match, it tries a new line
    // The file marker does not go back to the beginning of the File
    getline(in_file, list);
    a=0;
    count=0;
    }
    
    }
    
    
    
    
    
    
    }
    }

    Any help would be great... THank you all in advance for even reading such a post.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Since any help would be great, here is some help to make your code more clear and more correct in general so you can work on figuring out what the real problems are.
    [list=1][*]Use code tags, they leave the code in its original format so that people can read it more easily and help you with your problems.[*]You are close to using your headers correctly. Since you are using the standard string class, the proper header for that is <string>, not <cstring>. Also, you might as well use <cmath> instead of <math.h>, although in this program I don't know why you need it at all. And finally, since all the goodies in those headers are in the std namespace, you should probably add using namespace std; below the includes. So, your headers should look more like this:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <cmath> // Delete this line if you aren't using it.
    using namespace std;
    [*]Use int main() instead of void main(). Just because your teacher/tutorial/book/compiler uses void doesn't make it ok. If you are using Visual C++ 6.0 you might want to add a return 0; at the end of your main function.[/list=1]I'd suggest making those changes first. Getting the headers correct will eliminate some strange bugs and compiler errors in your program.

    Once you do that, I'd then suggest responding in this thread with a description in English of what you are trying to code. For example:

    1. Open the file "teacherlist.txt".
    2. Ask the user for the name of a student to search for.
    3. Read a line of the input file and save the name as the current teacher and room number.
    4. ... whatever ...
    5. ... whatever ...
    6. Repeat steps 3-5 until the student name is found.
    7. Output the teacher's name and room number.

    Note: this is not a correct way to solve the problem, but it should give you an idea of how to write out what your plan is. Once we know what you meant to code we can help you figure out if you did what you want.

    Hope that helps.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I agree with everything in the above post, plus have some error checking. For example if the file opened correctly. Do a search on this board as there are a BUNCH of threads dealing with very similar programs.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Multiple source files for one program
    By gflores in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2004, 02:32 AM
  4. Multi - File Program not finding files
    By johnh444 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2004, 01:48 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM