Ok, my problem is that I'm reading from a file 3 values, a last name, first name and salary, for example: Key Bobby 43000
I have an array of objects for each person, so I neeed to read this file and assign the last name, first name, and salary to the appropriate variable. I've been out of C++ since April. I'm trying to do this for my Dad's business and I'm rusty. I have some debug statements in there to see if I'm actually inputing values. As of now I do not know what to pass to the set functions in my while loop because I don't want it to be to ineffecient. Any help is appreciated, I may be totally wrong with what I'm doing, let me know.

My header so far.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Employee
{
   string first_name, // Employee first name
          last_name;  // Employee last name
   float  salary;     // Monthly salary amount received
public:
   // Employee's constructors
          Employee();

   // Set member variables
   void   set_first_name(string employee_first_name);
   void   set_last_name (string employee_first_name);
   void   set_salary    (float monthly_salary);

   // Get member variables
   string get_first_name() {return first_name;}
   string get_last_name () {return last_name;}
   float  get_salary    () {return salary;}

   void print_employee_info();
};

Employee::Employee()
{
   first_name = '\0';
   last_name  = '\0';
   salary     = 0.0f;
}

void Employee::set_first_name(string employee_first_name)
{
	first_name = employee_first_name;
}

void Employee::set_last_name(string employee_last_name)
{
	last_name = employee_last_name;
}

void Employee::set_salary(float monthly_salary)
{
	if(monthly_salary > 0.0f)
		salary = monthly_salary;
	else
		salary = 0.0f;
}

My main.

#include "stdafx.h"
#include "EmployeeHeader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

#define MAX_EMPLOYEE_SIZE 100

int main()
{
	ifstream indata;
	Employee EmployeeInfo[MAX_EMPLOYEE_SIZE];
	string name;
	int employee_count = 0;

   indata.open("employee.txt");
   if(!indata) 
   { 
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }

   while ( !indata.eof() ) 
   {
	  indata >> EmployeeInfo[employee_count].set_first_name();
	  cout << EmployeeInfo[employee_count].get_first_name(); // debug
	  indata >> EmployeeInfo[employee_count].set_last_name();
	  cout << EmployeeInfo[employee_count].get_last_name(); // debug
	  indata >> EmployeeInfo[employee_count].set_salary();
	  cout << EmployeeInfo[employee_count].get_salary();      // debug
	  employee_count++;
   }
   indata.close();
   cout << "End-of-file reached." << endl;

	return 0;
}
Comments
Congratulations on using code tags on your first post, so few manage it.

Instead of

while ( !indata.eof() ) 
{
    indata >> EmployeeInfo[employee_count].set_first_name();
    cout << EmployeeInfo[employee_count].get_first_name(); 
    // etc
}

try doing this;

string temp;
while ( !indata.eof() && employee_count < MAX_EMPLOYEE_SIZE) 
{
    indata >> temp;
    EmployeeInfo[employee_count].set_first_name(temp);
    cout << EmployeeInfo[employee_count].get_first_name(); 
    // etc
}

The additional check of employee_count stops running off the end of the array.

It is often a poor idea to use a float or double to represent currency figures. Particularly things like using 27.12 to represent $27 and 12 cents, as a floating point variable cannot represent 0.10 or 0.01 (or most combinations thereof) exactly. If you're only going to work with currencies that are dollars and cents, use a struct that represents the dollars and cents as separate fields, and manage the entries accordingly.

instead of an array you can use 'vector' to save unnecessary allocation of memory.

Here is the updated code but the output isn't correct I don't think. First and last name are the same then it loops and first and last name are the same again. The updated code is as follows, the out put is posted after.

#include "stdafx.h"
#include "EmployeeHeader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

#define MAX_EMPLOYEE_SIZE 100

int main()
{
	ifstream indata;
	Employee EmployeeInfo[MAX_EMPLOYEE_SIZE];
	string info;
	int employee_count = 0;

   indata.open("employee.txt");
   if(!indata) 
   { 
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }

   while ( !indata.eof()  && employee_count < MAX_EMPLOYEE_SIZE) 
   {
	  indata >> info;
	  EmployeeInfo[employee_count].set_first_name(info);
	  cout << EmployeeInfo[employee_count].get_first_name();
	  cout << "\n";
	  EmployeeInfo[employee_count].set_last_name(info);
	  cout << EmployeeInfo[employee_count].get_last_name();
	  cout << "\n\n";
	  employee_count++;
   }

And the output is like this. I put the "\n\n" to show whats happening.

Fred
Fred

Smith
Smith

40000
40000

McGee
McGee

Nate
Nate

63100.87
63100.87

63100.87
63100.87

End-of-file reached.

Never test end of file with .eof()!!! It's wrong.

Instead, replace !indata.eof() with (indata>>info) (and of course, remove same line after while)

Ok I still have the same output. I have 3 values per line in my file. Last name, first name and salary. I need it to read the last name and assign it into the object last name, read the first name and assign into the object first name and then read the salary as a float and assign to the object salary. The way I'm doing it now for the salary is reading it as a string and of course I can't do that because in my header it receives a float, not a string, but how can I fix this?

Updated code:

#include "stdafx.h"
#include "EmployeeHeader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

#define MAX_EMPLOYEE_SIZE 100

int main()
{
	ifstream indata;
	Employee EmployeeInfo[MAX_EMPLOYEE_SIZE];
	string info;
	int employee_count = 0;

   indata.open("employee.txt");
   if(!indata) 
   { 
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }

   while ( indata>>info  && employee_count < MAX_EMPLOYEE_SIZE) 
   {
	  EmployeeInfo[employee_count].set_first_name(info);
	  cout << EmployeeInfo[employee_count].get_first_name();
	  cout << "\n";
	  EmployeeInfo[employee_count].set_last_name(info);
	  cout << EmployeeInfo[employee_count].get_last_name();
	  cout << "\n\n";
          EmployeeInfo[employee_count].set_salary(info);
	  cout << EmployeeInfo[employee_count].get_salary();
	  employee_count++;
   }

what is the answer of of n8thatsme's last question about salary string to float conversion??

Edited 5 Years Ago by beset123: n/a

Don't bump a three year old thread to ask an unrelated question. If you can't find the answer to your question by looking on Daniweb or on the web at large, then create a new thread for your question.

And to answer, to convert a string to a float, you do:

float toFloat(const std::string& str) {
  float result;
  std::stringstream(str) >> result;
  return result;
};
This article has been dead for over six months. Start a new discussion instead.