EOF (End Of File) Loop help

This is a discussion on EOF (End Of File) Loop help within the C++ Programming forums, part of the General Programming Boards category; My assignment is to use an input file of names, and have a program break the names down in first-last, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    EOF (End Of File) Loop help

    My assignment is to use an input file of names, and have a program break the names down in first-last, and last-first-initial format. And to use an End Of File loop to terminate. My proglem is that Sara-Jessica Parker doesn't have a middle name and so ruins the outputs for the final 2 names. In fact the last name in my file doesn't output. Is there a way that I can use the "/n" to input data? Will that solve the problem?

    Code:
    //******************************************************************
    // PrintName program
    // This program prints a name in two different formats
    //******************************************************************
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    string FIRST;    // Person's first name
    string LAST;      // Person's last name
    char   MIDDLE;        // Person's middle initial
    ifstream inPut;       // Data File
    ofstream outPut;
    int main()
    {
    	inPut.open("names.dat");
    	outPut.open ("output.dat");
    	if ( !inPut )
    	{
             cout << "Can't open the input file.";
             return 1;
        }
     
        inPut >> FIRST >> LAST >> MIDDLE;
        while (inPut)
        {
              string firstLast;    // Name in first-last format
              string lastFirst;    // Name in last-first format
    
              firstLast = FIRST + " " + LAST;
        
              outPut << "Name in first-last format is " << firstLast << endl;
        
              lastFirst = LAST + ", " + FIRST + ", ";
              outPut << "Name in last-first-initial format is ";
              outPut << lastFirst << MIDDLE << '.' << endl;
              
              inPut >> FIRST >> LAST >> MIDDLE;
    }
        inPut.close();
        
        return 0;
    }
    Names.dat file:
    George Bush W
    Madhavi Gandhi D
    Dan Berk G
    Ameka Johnson P
    Rita Hayward C
    Clint Eastwood B
    Sara-Jessica Parker
    Larry Kind L
    Rani Mukharji

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Why don't you use a get() for the middle initial. Extract the last and first name normally, then do a loop to get(MIDDLE) while middle doesn't equal a space. In other words, you'll end up with the middle initial or a newline character. Also you might as well do a check and just dump the initial if it is a newline character.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    Quote Originally Posted by SlyMaelstrom
    Why don't you use a get() for the middle initial. Extract the last and first name normally, then do a loop to get(MIDDLE) while middle doesn't equal a space. In other words, you'll end up with the middle initial or a newline character. Also you might as well do a check and just dump the initial if it is a newline character.
    Would you mind showing me an example?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    while(cin.get(var1) && var1 != ' ');
    
    if (var1 != '\n')
       cout << var1 << endl;
    Last edited by SlyMaelstrom; 02-21-2006 at 09:02 PM.
    Sent from my iPadŽ

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You can also use getline() and just search the line for the number of spaces, when the number returns 2 then you use the middle name fucntion, if it returns 1 then you use the no middle name function. If it returns anything else, then skip the line because it is not properly formated.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    I'm sorry, I'm still confused.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    My proglem is that Sara-Jessica Parker doesn't have a middle name and so ruins the outputs for the final 2 names.
    The number #1 thing to know about reading from files is.....the read statement should be the while conditional:
    Code:
    while(inPut >> FIRST >> LAST >> MIDDLE)
    {
       //do stuff to the input
    }
    If you do that, then the loop will terminate correctly after you read the last line of data in the file.

    However, since you now realize that your current read statement will not work for your data, you have to come up with a more general read statement that will work for all names.

    Is there a way that I can use the "/n" to input data? Will that solve the problem?
    getline() will read until it encounters a '\n'.
    Last edited by 7stud; 02-21-2006 at 09:09 PM.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    we were always taught to use eof by reading the first value in before the eof loop. You can always use eof and in the loop check for if(var.fail()) break;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21