keggy
keggy

Reputation: 53

error: cannot convert `void (*)()' to `float'

I have a problem converting void (*)() to float. Here is my code:

#include <iostream>

using namespace std;

void instructions();
void numDays();
void med();
void sur();
void lab();
float room(int days);
float misc(float meds, float surgical, float lab);
float total(float room, float misc);

int main()
{
  int days;
  float meds;
  float surgical;
  instructions();
  numDays();
  med();
  sur();
  lab();
  room( days);
  misc(meds, surgical, lab);
  total( room, misc);
}

void instructions()
{
  cout<<"this program will ask you some questions. After that it will print out all of the users charges and the tital\
 amount that is owed. "<<endl;
}

void numDays()
{
  float days;
  cout<<"Enter in users length of stay. "<<endl;
  cin>> days;
}

void med()
{
  float meds;
  cout<<"Enter int the users medication charges. "<<endl;
  cin>> meds;
}

void sur()
{
  float surgical;
  cout<<"Enter in the users surgical charges. "<<endl;
  cin>> surgical;
}

void lab()
{
  float lab;
  cout<<"Enter in the users lab fees. "<<endl;
  cin>> lab;
}

float room(float days)
{
  float room;

 room = (days*450);

}

float misc(float meds, float surgical, float lab)
{
  float misc;
  misc = (meds + surgical + lab);
  return 0;
}

float total(float room, float misc)
{
 float total;
 total = (room + misc);
  return total;
}

These are the error messages I keep getting:

:24: error: cannot convert 'void (*)()' to 'float' for argument `3' to `float misc(float, float, float)'
:25: error: cannot convert 'float (*)(int)' to 'float' for argument `1' to `float total(float, float)'

Upvotes: 0

Views: 3009

Answers (3)

Roland Illig
Roland Illig

Reputation: 41625

Ok, let’s start at the beginning. A computer program like your example consists of two different things:

  1. Functions: they are active, they do something when they are called.
  2. Variables: they are passive, they cannot do something themselves.

In your program you have two different things (one variable, and one function), and both of them are called med. This will lead to confusion. A typical style guide therefore recommends that function names have a verb in their name, for example read_med, so you know what the function is supposed to do. On the other hand, variable names are often nouns, such as med, days, total.

The function then looks like this:

float read_med() { // this is a function (active part)
  float med; // and this is a variable (passive part)
  cout << "Enter the medication amount: ";
  cin >> med;
  return med;
}

You can then call this function like this:

float med = read_med(); // the round parentheses mean “call the function, run the code”
float days = read_days();

float total = med * days;
cout << "Total: " << total << endl;

Upvotes: 2

sashkello
sashkello

Reputation: 17871

  1. In your room() function I assume you forgot return statement.

  2. total(room, misc); - so, here I suppose you expect to have a result of your room and misc functions. This will work if you do

    float misc = misc(meds, surgical, lab);
    float room = room(days);
    float total = total(room, misc);
    
  3. The void functions you have are not doing what you expect. You create local variables inside these functions and they are not the same as variables you have in main. You can make those functions to return the entered result and then do float lab = lab().

  4. Your misc function always returns 0 - I believe this is not what you expect.

  5. In general, if you have function returning something assign it to a variable, like shown in 2. - otherwise it will not do anything useful and you won't be able to reuse it.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

For example in this statement

misc(meds, surgical, lab);

argument lab is a function pointer. I did not look through your code but maybe you meant

misc(meds, surgical, lab());

However even in this case you will get an error because function lab has return type void. It seems you do not understand what you are doing.

Upvotes: 3

Related Questions