0
int main()
{
    float num;

    num = GetData;
}

int GetData()
{
    float num;
    cout << "Enter the number you would like to evaluate: \n"
        << "(Number must be between 1,000,000 and -1,000,000.)";
    cin >> num;

    if (num > 1000000 || num < -1000000)
    {
        cout << "Please enter a number between 1,000,000 and -1,000,000.";
        return 0;
    }
    else
    {
        return num;
    }
}

I'm in my first term of programming classes so I'm probably missing something really simple. The error I'm getting is:

Error (active) E0513 a value of type "int (*)()" cannot be assigned to an entity of type "float"

I've tried changing the variable type and the return type of the function but the error persists. Like I said I'm new to this so it's probably something simple I'm overlooking.

jmat35
  • 1
  • 1
  • 3
    `GetData` is a function. You need to call it. Like this: `num = GetData();`. Also there is no point in using `float` in your program. `int` would do. – bolov Jun 10 '21 at 01:27
  • Oh duh. Also it needs to be a float for other parts of the program, haven't gotten that far yet. Thanks! – jmat35 Jun 10 '21 at 01:34
  • If it needs to be float in other parts of your program, you can't define it as returning type `int` in `int GetData()`. You also can't define it as returning `int` and then use `return num` when you've declared `float num` - it's not only incorrect, but it's confusing to others that have to use your code. Use the proper type always. If you declare it as returning `int`, then it should always return a value you've declared as `int`, not `float`. – Ken White Jun 10 '21 at 01:36
  • @41686d6564: Syntactically, it will compile. However, `int GetData()` indicates that the function returns an `int`, and in the function body `float num;` and `return num;` are trying to return a `float` from a function defined as returning `int`. While it will most likely automatically be truncated to return an `int`, the code is confusing when going back to maintain it later or when another coder tries to do so. *Why is this function trying to return float num when it's declared as returning an int?* isn't something that you want people to be asking about your code. – Ken White Jun 10 '21 at 01:47
  • Oh, my bad. I didn't even notice that the OP was _also_ using `float num` inside `GetData()`. I was only talking about the variable in `main`, @KenWhite. You're definitely right about returning a float from a function with an int return type. – 41686d6564 stands w. Palestine Jun 10 '21 at 01:48
  • continued - If you want to do the `return num` when it's `float num` from a function returning `int`, at least do it properly with `return (int) num;`. – Ken White Jun 10 '21 at 01:49
  • @KenWhite fixed, thanks! Changed the function to return a float. – jmat35 Jun 10 '21 at 02:04

1 Answers1

2

You need to call num = GetData() instead of num = GetData, as GetData() is a function (docs here). Also, as you're returning a float with GetData(), your return type should be float as well:

#include <iostream>
using namespace std;

float GetData()
{
    float num;
    cout << "Enter the number you would like to evaluate: \n"
        << "(Number must be between 1,000,000 and -1,000,000.)";
    cin >> num;

    if (num > 1000000 || num < -1000000)
    {
        cout << "Please enter a number between 1,000,000 and -1,000,000.";
        return 0;
    }
    else
    {
        return num;
    }
}

int main()
{
    float num;

    num = GetData();
    cout << num;
}

Result:

Enter the number you would like to evaluate:
(Number must be between 1,000,000 and -1,000,000.)12
12

Another thing, if you are dealing with integers, I suggest using an int.

Also see Why is "using namespace std;" considered bad practice?

silverfox
  • 1,568
  • 10
  • 27