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

Dani AI

Generated

A few things are happening in the thread that explain both the compile errors and the runtime assertion.

As pointed out, the compile errors were from small syntax and scoping mistakes (missing semicolon, and using m_wage/m_hours without the instance). The runtime "String is not null terminated" assertion reported by Visual C++ comes from using strcat_s on an uninitialized char[] buffer: strcat/strcat_s expect the destination to already contain a valid C string (i.e., a terminating '\0'). As explained, either initialize the buffer first or use a copying function that does not require a prior terminator. The simplest, safest approach is to avoid raw char arrays for names and use std::string, as suggested.

Overtime logic also needs correcting. Typical rules:

  • overtime_hours = max(0.0, hours - 40.0)
  • overtime_rate = wage * 1.5
  • overtime_pay = overtime_hours * overtime_rate
  • total_pay = min(hours, 40.0) * wage + overtime_pay

Below is a concise, modern example that uses std::string, computes overtime hours, overtime pay, and total pay. It is safe, clear, and avoids C-string pitfalls.

#include <iostream>
#include <string>
#include <algorithm>

struct Employee {
    std::string name;
    double hours = 0.0;
    double wage  = 0.0;

    double overtime_hours() const { return std::max(0.0, hours - 40.0); }
    double overtime_pay()   const { return overtime_hours() * wage * 1.5; }
    double total_pay()      const {
        return std::min(hours, 40.0) * wage + overtime_pay();
    }
};

Quick checklist: initialize members (or use constructors), use person1.m_* when accessing members, prefer std::string to avoid CRT assertions, and use the formula above rather than subtracting a literal from wage or adding 40 to hours. For older compilers that lack C++11 features, initialize in a constructor instead of in-class defaults or brace-init.

Recommended Answers

All 8 Replies

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

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

I recommend you use an std::string instead of a character array for m_name and Name.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.