Reading file word by word
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Reading file word by word

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    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;
    }

  2. #2
    Join Date
    Nov 2011
    Posts
    3

    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Reading file word by word

    Quote Originally Posted by Golffor1 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center