2 Replies - 194 Views - Last Post: 07 February 2018 - 09:28 PM Rate Topic: -----

#1 tidematic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-January 18

Ifstream help (related to classes)

Posted 07 February 2018 - 07:08 PM

So I am trying to read in a file using private class variables. There might be another way to do this, but this is what I could think of. Note, its my first project using classes and private and public members. Was I on the right path atleast? I keep getting an error for the int main function. How can I fix it?


This is my main:

#include "Record.h"
#include <sstream>

int main ()
{	
	Record employee;

	ifstream myFile;
	myFile.open("Project 3.dat");
	string str;
	int i=0;
	
	if (myFile.is_open())
	{
		while (getline(myFile, str))
		{
			istringstream ss(str);
			ss >> employee.set_name(str) >> 
              employee.set_id(stoi(str)) >> employee.set_rate(stoi(str)) >> 
                         employee.set_hoursWorked(str); // i know this part is completely wrong, i cant figure it out
		}
	}
	
	return 0;
}




This is my header

#include <iostream>
#include <fstream>
#include <string> 
using namespace std;

class Record
{
	private:
			string name;
			int id;
			double rate;
			double hours;
	public: 
			Record();
			Record (string n, int empid, double hourlyRate, double hoursWorked); // constructor
			
			void read_data_from_file();
			double calculate_wage();
			void print_data();
			
			/* ASETTERS AND GETTERS */			
			void set_name (string n);
			string get_name();
			
			void set_id (int empid);
			int get_id();
			
			void set_rate (double hourlyRate);
			double get_rate();
			
			void set_hoursWorked(double hoursWorked);
			double get_hoursWorked();
			/* END OF SETTERS AND GETTERS */			
};






#include "Record.h"
Record::Record():name(), id(0), rate(0), hours(0) {} //default constructor must be implemented first
Record::Record(string n, int empid, double hourlyRate, double hoursWorked)
{
	name = n;
	empid = id;
	hourlyRate = rate;
	hoursWorked = hours;
}
//
void Record::set_name(string n)
{
	name = n;
}
string Record::get_name()
{
	return name;
}
//
void Record::set_id(int empid)
{
	id = empid;
}
int Record::get_id()
{
	return id;
}
//
void Record::set_rate(double hourlyRate)
{
	rate = hourlyRate;
}
double Record::get_rate()
{
	return rate;
}
//
void Record::set_hoursWorked(double hoursWorked)
{
	hours = hoursWorked;
}
double Record::get_hoursWorked()
{
	return hours;
}
//


void Record::read_data_from_file()
{
	
}
double Record::calculate_wage()
{
	return (rate * hours);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ifstream help (related to classes)

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6010
  • View blog
  • Posts: 20,654
  • Joined: 05-May 12

Re: Ifstream help (related to classes)

Posted 07 February 2018 - 07:25 PM

In the future, please post the exact error(s) you are getting. Don't paraphrase or delete information.

For now, I am guessing that the error is on line 18 of your main file.

You need to read the values into variables first, and then call your setters passing in the variables. Trying to pack it all into a single line is not the right approach. Later when you learn about operator overloading, you will learn another technique to read the record with one line.
Was This Post Helpful? 0
  • +
  • -

#3 tony jay  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 42
  • Joined: 31-January 18

Re: Ifstream help (related to classes)

Posted 07 February 2018 - 09:28 PM

Why use sstream at all?

If I understand correctly there is a file with all the data already in it.
Some of the data items are strings and some of the data items are ints and floating points.
Clearly using the function stoi() is not going to work since it produces an int not a double...yes!

Just use the ifstream instead....
Once you have checked that the ifstream is valid the idea is to use myFile (that is the ifstream) to read values into.
Create variables (in this scope) that match what you are expecting - string, int, double, double.
Use these variables to modify or create an object of type Record.

I am not a C++ guru yet, but I took your effort and reproduced it, and then modified it appropriately until it worked. There are probably a fair few variations that will also work. With time and experience I will likely discover even better ways of achieving the same thing.

Understanding streams in general takes a bit of time. Interspersing reading with coding seems to build the understanding the best.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1