Kayti Lee-Sans
Kayti Lee-Sans

Reputation: 1

ERROR "cannot convert 'float' to 'float...'..."

There are probably other little formatting errors in here that I'll need to fix, etc. but what I need help with is what to do with the following:

Lab8pt1.cpp: In function 'float Salary(float, float)':
Lab8pt1.cpp:48: error: assignment of function 'float Salary(float, float)'
Lab8pt1.cpp:48: error: cannot convert 'float' to 'float ()(float, float)' in assignment
Lab8pt1.cpp:50: error: assignment of function 'float Salary(float, float)'
Lab8pt1.cpp:50: error: cannot convert 'double' to 'float ()(float, float)' in assignment
Lab8pt1.cpp:51: error: cannot convert 'float (*)(float, float)' to 'float' in return

I know it's referring to my Salary function, but I'm not sure what the issue with my float is. This is just supposed to be a simple Lab Assignment that teaches us how to use functions (we only have to write the code for the functions, the rest was given to us).

Help, please! Thanks in advance!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std ;

void Header(void) ;            
float Salary(float Hours, float Pay_Rate);  
void Print_it(float Hours,float Pay_Rate,float Sal, float Tax_Rate, string Name);  
void Read(float &hour, float &Pay_R,string &name) ;
bool Verify(float Hours, float Pay_Rate);  

int main ( void )
{
    float   Pay_Rate, Hours, Sal, Tax;
    const float  Tax_Rate= (float)0.09 ;
    string name;

    Header();
    for(int i = 0 ; i < 3 ; i++){
         Read(Hours,Pay_Rate,name);
         Sal = Salary(Hours,Pay_Rate);
         Print_it(Hours,Pay_Rate,Sal, Tax_Rate,name);    
    }
    cout<<"\n\n\n**********\t End of report \t*****\n\n\n\n";
    return 0 ;
}

void Header( void )
{
     string name;
     cout << "Welcome, " << name << ", to the Salary Calculator: a program that will calculate your salary.";
     return;
} 

float Salary(float Hours, float Pay_Rate)
{
     if( Hours <= 40 )
         Salary = Hours * Pay_Rate;
     else if( Hours > 40)
         Salary = Hours * (Pay_Rate * 1.5);
     return(Salary);
}

void Print_it(float Hours,float Pay_Rate,float Sal, float Tax_Rate, string Name)
{
     cout << fixed << setprecision(2);
     cout << "Name: " << left << setw(15) << Name << endl;
     cout << "Hours worked: " << left << setw(15) << Hours << endl;
     cout << "Pay rate: " << left << setw(15) << Pay_Rate << endl;
     cout << "Tax rate: " << left << setw(15) << Tax_Rate << endl;
     cout << "Salary: " << left << setw(15) << Sal << endl;
     return;
}

void Read(float &hour, float &Pay_R,string &name) 
{
     cout << "Please enter your name: ";
     getline(cin, name);
     cout << "Please enter number of hours worked: ";
     cin >> hour;
     cout << "Please enter your pay rate: ";
     cin >> Pay_R;
     return;
}

bool Verify(float Hours, float Pay_Rate)
{
     if( Hours < 0 || Hours > 60 || Pay_Rate < 0 || Pay_Rate > 500)
         return false;
     else
         return true;
}  

Upvotes: 0

Views: 16846

Answers (2)

taskinoor
taskinoor

Reputation: 46027

Salary = Hours * Pay_Rate;

Salary is the function name. You can not assign a float value to it. You need to declare a float variable and return that variable.

float sal;

sal = Hours * Pay_Rate;

return sal;

In fact you don't need this variable. You can directly return within the if-else block.

if( Hours <= 40 )
    return Hours * Pay_Rate;

Note that method and variable names should start with a lowercase letter, class name should start with uppercase. This is widely used convention.

Upvotes: 5

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

its the function you are trying to return.

float Salary(float Hours, float Pay_Rate)
{
     if( Hours <= 40 )
         Salary = Hours * Pay_Rate;
     else if( Hours > 40)
         Salary = Hours * (Pay_Rate * 1.5);
     return(Salary);
}

there is no variable salary defined in this function

Corrected code is:

float Salary(float Hours, float Pay_Rate)
    {
         float salary;
         if( Hours <= 40 )
             salary = Hours * Pay_Rate;
         else if( Hours > 40)
             salary = Hours * (Pay_Rate * 1.5);
         return(salary);
    }

Upvotes: 1

Related Questions