-
December 1st, 2011, 11:08 PM
#1
Reading file word by word
Hello I have the following code:
Code:
int main()
{
const int arraySize = 5;
int indexOfArray = 0;
Employee bookList[arraySize];
int tempPrice;//temporary stores price
string tempStr;//temporary stores author, title
string name;
int identificationNumber;
string test;
ifstream inFile ("master8.txt");
if (inFile.is_open())
{
while (! inFile.eof() )
{
for(int x=0; x<1; x++)
{
bookList[x].getName = getline(inFile, tempStr, ' ');
cout << "\n\nI " << x << endl;
}
}
inFile.close();
}
else cout << "ERROR";
return 0;
}
So I have the code above that is supposed to store a name inside bookList[x].getName. I will then be taking whatever X is and writing it to an outfile. The problem is that right now the name can be anywhere from 1-20 characters. I will then be grabbing an ID #, and a bunch of other things and doing the exact same thing with it.
Right now my code does not compile.
Here is the getName function inside the header file
Code:
string Employee::getName(string &name)
{
return name;
}
-
December 1st, 2011, 11:13 PM
#2
Re: Reading file word by word
If it matters each word can be any number of characters long. The name will have a first and a last name as well.
-
December 2nd, 2011, 05:36 PM
#3
Re: Reading file word by word
Originally Posted by Golffor1
Here is the getName function inside the header file
Code:
string Employee::getName(string &name)
{
return name;
}
So you are passing a name to this function, and returning the same name you're passing? What purpose does that serve, except to burn CPU cycles?
Code:
class Employee
{
std::string employee_name;
public:
std::string getName() const
{
return employee_name;
}
};
The code above makes much more sense than what you posted. The getName() function returns the employee name that is internal to the Employee class.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|