Thread: Beginner having getline issues

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Beginner having getline issues

    I am working on a module program that has a function that opens a file
    then the next file is to read the names from txt. file

    HUGH JACKMAN
    LUCY LIU
    HEIDI KLUM
    BONNIE RAIT
    DENZEL WASHINGTON
    AL PACINO
    WOODY ALLEN
    EMMA WATSON
    JOHN BARROWMAN
    PAUL MCCARTNEY
    PAULA ABDUL
    JULIA ROBERTS
    RUPERT GRINT
    DANIEL RADCLIFFE
    ERIC CLAPTON
    JAKE GYLLENHAAL
    MEGAN FOX
    MICKEY MOUSE
    KATIE HOLMES
    CHRISTIAN BALE
    MICHELLE PFEIFFER
    BRUCE SPRINGSTEEN
    MICHAEL KORS
    JOHN CLEESE
    BLAKE LIVELY

    Into an array guestlist there are 25 elements
    I am getting a undeclared identifier error inFile but infile worked fine in my initial function.
    Here is my code
    Code:
     
    /* Global Headers*/
     
    
    #include <iostream>                 // include standard I/O library
    #include <fstream>                                                                      // opening and closing files
    #include <cstdlib>
    #include <string>                                                                      // set string class
    #include <stdlib.h>
    using namespace std;
    
     
    
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    
    /* Global Constants */
    
     
    const int NUM_GUESTS = 25 ;         // number of guests on list
    const string BLANKS = "     " ;     // input spacing
    std::string guestlist[NUM_GUESTS];       // array
     int count;                         //for counter
    
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    
     
    
    /* Function Prototypes */
    
     void PrintIntro();
     void OpenFile(ifstream& inFile);
     void ReadFile ();
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    /* main function */
    
      int main ()
    
     {
    	 
    
    
     ifstream inFile;                                                        // input data file
    
     //Print intro for user
    	 PrintIntro();
    
     //open file that contains stars name
    	OpenFile(inFile);
     //read and file array//
    	ReadFile();
    
     
           return 0;
    
    }
    
    
     
    
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    
    /* Functions */
    
        void PrintIntro()
    
    	{              // Print intro
    
    	 cout << "********************************************************"
    		  << endl;
    	 cout << " *The L.A. Enquirer Welcomes the Top 25 Hollywood Stars*" << endl;
    	 cout << "********************************************************"
    		  << endl;
    
    		 return;
    
    }              // Print intro
    
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    	void OpenFile(ifstream& inFile)
    
    
     {
    	inFile.open("partyGuests.txt");
    	
    	if (!inFile)
    	{
    	
    		cout<< ("This file is unaccessable at this time!")<< endl;
    		exit (1);
    	
    	}
    	
    	else 
    		cout<< "file open" << endl;
    	
    	return;
     }
    /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
    	// read from file and build list//
    
    	void ReadFile(string)
    	 
    
    
    	{while(!inFile.eof())
    	
    	Infile.getline(guestlist,NUM_GUESTS);
    	
    	cout << guestlist << endl;
    
    	//close file
    	inFile.close();
    	
    	return
    	}

    error messages as follows (if I remove final function it compiles and debugs)


    Code:
    1>c:\documents and settings\carol rose\my documents\visual studio 2008\projects\party5\party5\party5.cpp(139) : error C2065: 'inFile' : undeclared identifier
    1>c:\documents and settings\carol rose\my documents\visual studio 2008\projects\party5\party5\party5.cpp(139) : error C2228: left of '.eof' must have class/struct/union
    1>        type is ''unknown-type''
    1>c:\documents and settings\carol rose\my documents\visual studio 2008\projects\party5\party5\party5.cpp(139) : fatal error C1903: unable to recover from previous error(s); stopping compilation


    This is a prject and I have limited knowledge I would like to get over this hump
    and have the array set so I can fill the parallel array
    and do the comparisons.
    This program is to have a user enter a name and test for present or absent
    if absent change to present.

    Hope this post is not to wordy thought it might help to know where it was heading.
    Thanks for any help
    Pistol1

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You probably want to pass the inFile variable as a parameter to the ReadFile function (like you did with OpenFile).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It has nothing to do with getline and everything to do with that you do not understand scope.
    Variables created inside one function is not visible outside it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In general, in addition to the scoping problems as already noted:
    Code:
    void ReadFile ();
    
    ...
    
    void ReadFile(string)
    {
        while(!inFile.eof())
    	
        Infile.getline(guestlist,NUM_GUESTS);
    	
        cout << guestlist << endl;
    
        //close file
        inFile.close();
    	
        return
    }
    #1 The prototype for the function and its actual definition do not match, one has nothing between the parenthesis and the other just has the keyword string which is incorrect.

    #2 The language is case-sensitive. This means that Infile is not the same as inFile which I'm guessing you would rather use.

    #3 You are using eof to control a loop which is bad. This will typically result in the last line being "processed" twice. A better method would be to place the getline call in place of the check against eof and test the return value.

    #4 You're use of guestlist is wrong. It is an array of strings and as such you must use an index to access (either read into or write from) elements contained within it.

    #5 There are two main variants of the getline function. You appear to be attempting to use the one for reading into character arrays but you want the one designed for use with string containers. They look a bit different.

    #6 You need a semicolon after the return statement.

    Also:
    Code:
    #include <cstdlib>
    #include <string>                                                                      // set string class
    #include <stdlib.h>  // Get rid of this
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Help

    Thanks for the input. I am reading everything I can on scope and understand it. I had an idea it was wrong. embarrassed about the sloppy syntax. Is it possible to pass inFile to the next function? Thanks again I will stay up late working on this as it does not come naturally to me but I am tenacious.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Is it possible to pass inFile to the next function?

    Yes, you already did it with OpenFile. Do the same thing with ReadFile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Binary Converter - Various stupid string issues :p
    By polarpete in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2005, 02:46 PM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM