![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 2
| Beginner having getline issues 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 |
| pistol1 is offline | |
| | #2 |
| Registered User Join Date: Jan 2005
Posts: 7,251
| You probably want to pass the inFile variable as a parameter to the ReadFile function (like you did with OpenFile). |
| Daved is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,869
| 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
}
#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
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is online now | |
| | #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. |
| pistol1 is offline | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 7,251
| >> Is it possible to pass inFile to the next function? Yes, you already did it with OpenFile. Do the same thing with ReadFile. |
| Daved is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| getline() don't want to work anymore... | mikahell | C++ Programming | 7 | 07-31-2006 10:50 AM |
| Binary Converter - Various stupid string issues :p | polarpete | C++ Programming | 7 | 08-21-2005 02:46 PM |
| getline problem | Bitphire | C++ Programming | 5 | 10-18-2004 04:42 PM |
| getline help | ProjectsProject | C++ Programming | 3 | 06-14-2004 11:12 AM |
| getline with string class in Borland C++ | johnnyd | C++ Programming | 6 | 03-08-2003 02:59 PM |