get function returns invalid value

Shaun Peretz

I'm working on a code for a school project which I can't use strings.
I'm having problems getting the value for hourlyPay.
The program's output: 5 Christine Kim 4.94066e-324
Although, the file contains the following:
5 Christine Kim 30.00

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;

//Class decleration
class Employee 
{
  private:
    int id;            //Employee ID.
    char name[21];        //Employee name.
    double hourlyPay;   //Pay per hour.

  public:
    Employee(int initId=0, char [] =0, double initHourlyPay=0.0);  //Constructor.

    bool set(int newId, char [], double newHourlyPay);
    int getId() { return id; }
    const char * getName() { return name;}
    double getHourlyPay() { return hourlyPay;}


};

Employee::Employee( int initId, char initName[], double initHourlyPay)
{
  bool status = set( initId, initName, initHourlyPay);

  if ( !status )
  {
    id = 0;
    strcpy(name, "");
    hourlyPay = 0.0;
  }
}

bool Employee::set( int newId, char newName[], double newHourlyPay)
{
  bool status = false;

  if ( newId > 0)
  {
    status = true;
    id = newId;
    strcpy(name, newName);
    hourlyPay = newHourlyPay;
  }
  return status;
}

const int MAX_SIZE = 100;


int main()
{

    int id;             //Employee ID.
    char newName[21];

    double hourlyPay;   //Pay per hour.


    Employee list[15];  //Array to store


    ifstream masterFile;        //Opens master file.

    masterFile.open("master10.txt");

    int count = 0;
    if (masterFile)
    {
        for (count; count < 2; count++)
        {
            masterFile >> id;
            masterFile.ignore();
            masterFile.getline(newName, 21);
            masterFile >> hourlyPay;
            list[count].set(id, newName, hourlyPay);
        }
    }

    masterFile.close(); //Close master file.

    cout << list[0].getId() << "   " << list[0].getName() << "  " << list[0].getHourlyPay();
}

The original file contains more lines, but I narrowed it down in order to figure out my error.
What am I doing wrong?

Shaun Peretz

I have figured it out.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;

//Class decleration
class Employee 
{
  private:
    int id;            //Employee ID.
    char name[21];        //Employee name.
    double hourlyPay;   //Pay per hour.

  public:
    Employee(int initId=0, char [] =0, double initHourlyPay=0.0);  //Constructor.

    bool set(int newId, char [], double newHourlyPay);
    int getId() { return id; }
    const char * getName() { return name;}
    double getHourlyPay() { return hourlyPay;}


};

Employee::Employee( int initId, char initName[], double initHourlyPay)
{
  bool status = set( initId, initName, initHourlyPay);

  if ( !status )
  {
    id = 0;
    strcpy(name, "");
    hourlyPay = 0.0;
  }
}

bool Employee::set( int newId, char newName[], double newHourlyPay)
{
  bool status = false;

  if ( newId > 0)
  {
    status = true;
    id = newId;
    strcpy(name, newName);
    hourlyPay = newHourlyPay;
  }
  return status;
}

const int MAX_SIZE = 100;


int main()
{

    int id;             //Employee ID.
    char newName[21];

    double hourlyPay;   //Pay per hour.


    Employee list[15];  //Array to store


    ifstream masterFile;        //Opens master file.

    masterFile.open("master10.txt");

    int count = 0;
    if (masterFile)
    {
        for (count; count < 2; count++)
        {
            masterFile >> id;
            masterFile.ignore();
            masterFile.get(newName, 21);
            masterFile >> hourlyPay;
            list[count].set(id, newName, hourlyPay);


        }

    }

    masterFile.close(); //Close master file.

    cout << list[0].getId() << "   " << list[0].getName() << "  " << list[0].getHourlyPay();
}

I only changed getline to get and now it can read in the middle of a line with a limit of 20 chars. I appreciate everyone's attention and help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

val arrayListOf() in companion object returns an invalid value

Function returns lock by value

@Value boolean returns 'invalid boolean value' of value true

Gmail REST API get message function returns invalid historyId

Function returns 'undefined' value

Function always returns meaningless value

Not able to get value from Async function which returns value by calling asyncStorage function

invalid property value scss function

Powershell function returns an invalid object

Graphql Apollo upload in Nestjs returns invalid value {}

mongodb isValid function returns true for invalid ObjectIDs

Nodejs Buffer.from returns invalid value

Function returns nonexpected value

The function returns invalid values

How to get the return value of a async function that returns a promise

Function returns unexpected value

MaxResults returns Invalid integer value for Calendar API

Why jquery returns invalid property value?

OpenProcess function returns invalid handles

BASH - Function returns wrong value

Function returns invalid literal for int() with base 10: ''

QUOTIENT of numbers returns invalid value

How to get the value of a property from a function which returns an object?

Passing a list to a function that returns a dictionary, how to only get the value and not the key

Google sheets, get a single value from a function that returns a table

Javascript returns value function

Function drops invalid values in dataframes but then it returns original dataframes with invalid values

Airflow PythonBranchOperator returns invalid value

AS returns Invalid <color> for given resource value