Hi,
I am trying to incorporate a struct and class in the same program to calculate overtime pay and hours for one person. I am having a hard time with these two and especially putting them together. I am not sure what to do to get the program to compile and get the output I want. Please help.

// Overtime Pay Structures.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//Definition of a struct to represent overtime pay calculator
struct overtime
{
	char Name[80];
	int Hours;
	int Wage;
};

class OTpay
{
public:
	double m_hours;
	double m_wage;
	double m_name;
};

OTpay::OTpay()

int _tmain(int argc, _TCHAR* argv[])
{
	
	OTpay person1;
	

	double otXpay = 0.00;
	double otHours = 0.0;

	person1.m_hours = 42.0;
	person1.m_name = ("Dan");
	person1.m_wage = 21.55;

//Calcualte overtime pay and hours for person1
	otXpay = m_wage-1.5;
	otHours = m_hours+40;

		cout<<endl
			<<"Person 1 Name is: " <<person1.m_name;
		cout<<endl
			<<"Person 1 Overtime Hours are: " <<otHours;
		cout<<endl
			<<"Person 1 OVertime Pay is: " <<otPay;
		cout<<endl;

	return 0;
}

You need a semicolon after the definition for the constructor for OTPay (OTPay::OTPay()) and need to declare it within the class but since it doesn't do anything might as well just get rid of it.
You try to set m_name (which is declared as a double) to "Dan" (a char constant) which is an illegal statement
I would need to see stdafx.h to be able to see more if it still doesn't compile

Edited 6 Years Ago by burgercho: n/a

Thank you. I have made the changes you stated and am still getting these compiling errors.
37) : error C2440: '=' : cannot convert from 'const char [4]' to 'char'
(41) : error C2065: 'm_wage' : undeclared identifier
(42) : error C2065: 'm_hours' : undeclared identifier
(49) : error C2065: 'otPay' : undeclared identifier

// Overtime Pay Structures.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//Definition of a struct to represent overtime pay calculator
struct overtime
{
	char Name[80];
	int Hours;
	int Wage;
};

class OTpay
{
public:
	double m_hours;
	double m_wage;
	char m_name;
};


int _tmain(int argc, _TCHAR* argv[])
{
	
	OTpay person1;
	

	double otXpay = 0.00;
	double otHours = 0.0;

	person1.m_hours = 42.0;
	person1.m_name = ("Dan");
	person1.m_wage = 21.55;

//Calcualte overtime pay and hours for person1
	otXpay = m_wage-1.5;
	otHours = m_hours+40;

		cout<<endl
			<<"Person 1 Name is: " <<person1.m_name;
		cout<<endl
			<<"Person 1 Overtime Hours are: " <<otHours;
		cout<<endl
			<<"Person 1 OVertime Pay is: " <<otPay;
		cout<<endl;

	return 0;
}

On lines 42 and 43 m_wage and m_hours are accessed without using the class identifier which is required. Try

otXpay = person1.m_wage-1.5;
otHours = person1.m_hours+40;

Also m_name is declared as a double but you are trying to assign a char to it, instead declare m_name as

char m_name[40];

(or whatever number, as long as it is longer than any name is likely to be) and use

strcat(person1.m_name, "Dan");

to set the name.

One last problem is that otPay (line 49) was never declared, did you mean to do otXpay maybe?

Great! I got it to compile but then I get this nasty error message. I have never seen this before.

Debug Assertion Failed!
Line:32
Expression:(L "String is not null terminated" &&0)

// Overtime Pay Structures.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//Definition of a struct to represent overtime pay calculator
struct overtime
{
	char Name[80];
	int Hours;
	int Wage;
};

class OTpay
{
public:
	double m_hours;
	double m_wage;
	char m_name[40];
};


int _tmain(int argc, _TCHAR* argv[])
{
	
	OTpay person1;
	
	double otXpay = 0.00;
	double otHours = 0.0;

	person1.m_hours = 42.0;
	strcat_s(person1.m_name, ("Dan"));
	person1.m_wage = 21.55;

//Calcualte overtime pay and hours for person1
	otXpay = person1.m_wage-1.5;
	otHours = person1.m_hours+40;

		cout<<endl
			<<"Person 1 Name is: " <<person1.m_name;
		cout<<endl
			<<"Person 1 Overtime Hours are: " <<otHours;
		cout<<endl
			<<"Person 1 OVertime Pay is: " <<otXpay;
		cout<<endl;

	return 0;
}

strcat_s() takes 3 arguments, not 2. And just like strcat(), a precondition is that the string is already zero terminated. Meet the precondition and supply the required arguments and it will work as expected.

person1.m_name[0] = '\0';
strcat_s(person1.m_name, 40, "Dan");

For initialization, strcpy() or strcpy_s() are better choises.

With std::string you can just add them:

It might be best practice to use the string class, but this does not answer the question of why there is a runtime error in the posted code.

This article has been dead for over six months. Start a new discussion instead.