Hi,

I have a problem where I have to create a class called Employee and calculate things such as pay etc..Part of the problem is to override the stream insertion operator, and I did, but I have a problem with the syntax of printing something out. I have a method in my program called float Pay( float nDay) which calculates the total amount of money the person gets per day, multiplied by the number of days I choose.

For the overriden << operator, this is the statement I have...

stream << cn.ID<<" - "<< cn.getfirstname()[0] << " " << cn.getlastname() << " (Total Pay: $"<< cn.Pay(float n) <<")"<<"\n";

Basically, it should print,

"ID - [First Initial] [Last Name] (Total Pay: $xxx.xx)

...My problem is, what syntax do I use to represent the pay. I would like it to be cn.Pay(float n), where n is a number that I input, but the only way the program works is if I put a specific number in the parentheses. How can I work around this?

(I thought about adding constructors to find the number of days, multiplying by the daily amount earned etc, but I'm not allowed to change the constructors. I hope this is not too vague, I refrained from posting the whole code because it's sort of long, and this looks like an easy fix. Thanks!

Hi,

I have a problem where I have to create a class called Employee and calculate things such as pay etc..Part of the problem is to override the stream insertion operator, and I did, but I have a problem with the syntax of printing something out. I have a method in my program called float Pay( float nDay) which calculates the total amount of money the person gets per day, multiplied by the number of days I choose.

For the overriden << operator, this is the statement I have...

stream << cn.ID<<" - "<< cn.getfirstname()[0] << " " << cn.getlastname() << " (Total Pay: $"<< cn.Pay(float n) <<")"<<"\n";

Basically, it should print,

"ID - [First Initial] [Last Name] (Total Pay: $xxx.xx)

...My problem is, what syntax do I use to represent the pay. I would like it to be cn.Pay(float n), where n is a number that I input, but the only way the program works is if I put a specific number in the parentheses. How can I work around this?

(I thought about adding constructors to find the number of days, multiplying by the daily amount earned etc, but I'm not allowed to change the constructors. I hope this is not too vague, I refrained from posting the whole code because it's sort of long, and this looks like an easy fix. Thanks!

Why do you have cn.cn.Pay(float n). It should just be cn.Pay(n).

I don't know where you're seeing cn.cn ?

Oh, also, when I tried cn.Pay(n), it tells me n is undefined - I defined it in my header files but without initializing it, I get some obscure number

Missed the whole bottom of your post :D

Oh, also, when I tried cn.Pay(n), it tells me n is undefined - I defined it in my header files but without initializing it, I get some obscure number

nevermind about the cn.cn thing. It showed up like that on my screen but appears to be a word-wrapping issue just on my screen.

However it still needs to be cn.Pay(n). You will only have cn.Pay(float n) in the function declaration and implementation. Calling a function like fun(int n) is incorrect. This declares a variable named n and gives it a default value, then passes it to your function (I believe). If you wish to pass a value to your function you just pass the variable itself. You just need to declare n beforehand.

Ok so basically you're saying leave the type name out right?

I did that, and defined n in my header file without initializing, and that's when I get some obscure number. (i.e. I'd have cn.Pay(n) and the output would be -1.4848e0009) ..

From what I understand, this is because it isn't initialized. However, if I initialize n to a specific number, it always stays that way, it does not later assume the number of my input.

Thanks for your help

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.