0

Currently I'm trying to run a program where you either pick an integer or a decimal and based on that your number gets manipulated. I was told by my Prof to use function overloading to get this to work. But every time I try to compile it it gives me 101 warnings and 2 errors saying: 'pick' must return a value. Am I not declaring it properly? I followed the template from my textbook and a few I found online but can't get it to work.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
  //Declarations
  int userInt;
  double userFloat;
  char selection, ans;
  static double counterInt, counterFloat;

  //Prototypes
  int pick(int);
  double pick(double);

  //Defaults
  counterInt = 0;
  counterFloat = 0;
  ans = 'Y';

  //Game
  do
  {
    cout << "Please choose an integer or a decimal number by typing in i or d" << endl;
    cin >> selection;

    if(selection == 'i')
    {
      cout << "Please enter an integer" << endl;
      cin >> userInt;
      cout << pick(userInt);
      counterInt++;}
    else
    {
      cout << "Please enter a decimal" << endl;
      cin >> userFloat;
      cout << pick(userFloat);
      counterFloat++;}

    //Play Again?
    cout << "Would you like to play again? Please enter Y or N" << endl;
    cin >> ans;
  }
  while ((ans == 'Y') || (ans == 'y'));

  //End Game
  if ((ans != 'Y') && (ans != 'y'))
  {
    cout << "Thank you for playing! You picked integers: " << counterInt << " times and you picked decimals: " << counterFloat << " times." << endl;
  }

  return 0;
}

//Function Overload
int pick(int number)
{
  if( number % 7 == 0)
  {
    cout << setw(7) << "7777777" << setw(7) << number << endl;
  }
  else
  {
    cout << setw(7) << "*******" << setw(7) << number << endl;
  }
}    

double pick(double number)
{
  cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << pow(number,.5) << endl;
}
djikay
  • 10,450
  • 8
  • 41
  • 52

3 Answers3

1

Both of your pick functions are declared as returning int or double respectively, but neither of them have a return statement.

//Function Overload
int pick(int number)
{
    if( number % 7 == 0)
    {
        cout << setw(7) << "7777777" << setw(7) << number << endl;
    }
    else
    {
        cout << setw(7) << "*******" << setw(7) << number << endl;
    }
    return number; // <<<<<<<<<<<<
}    


double pick(double number)
{
    double sqrt = pow(number,.5);
    cout << setw(6) << "The square root of the number " << number << " is: " << setprecision(3) << sqrt << endl;
    return sqrt; // <<<<<<<<<<<<
}

Or, if they don't actually need to return anything, just change the return types to void:

void pick (int number) { ... }
void pick (double number) { ... }
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • That fixed it! Thank you! It's amazing how leaving one little thing out causes the program to find 101 warnings to give you about the entire piece of code :( – user3763403 Jul 01 '14 at 18:32
  • 1
    Yeah, `C++` can be a bit picky, but it's incredibly powerful. But I see some other style issues and such within your code, I don't think all the warnings were just for the missing return. But this isn't the place to deal with that I'm afraid. I'd suggest [getting a good book](http://stackoverflow.com/q/388242/1171191). – BoBTFish Jul 01 '14 at 18:42
1

You have a function called pick and you tell the compiler that you are returning an integer when the function is finished with its algorithm. In your function you never return anything, you just write a specific value that is saved in the var number. You either have to turn the function void (which means no return value) or return any value / i.e. '1' for success, '-1' for failure.

//Function Overload
int pick(int number)
{
    if( number % 7 == 0)
    {
        cout << setw(7) << "7777777" << setw(7) << number << endl;
        return(1)
    }
    else
    {
        cout << setw(7) << "*******" << setw(7) << number << endl;
        return(-1)
    }
} 

You may also use void if you don't want to return any values or variables

//Function Overload
void pick(int number)
{
    if( number % 7 == 0)
    {
        cout << setw(7) << "7777777" << setw(7) << number << endl;
    }
    else
    {
        cout << setw(7) << "*******" << setw(7) << number << endl;
    }
} 

So everytime you write a type before a function (let it be class, int, double, bool ..) you have to return the value that fits in the spec. type

chriszo111
  • 343
  • 5
  • 17
0

When a function is defined with int as its return type, the function must return an int. Otherwise, the program is likely to have undefined behavior when that function is called.

You can resolve your problem two ways:

  1. Update the functions to return a value.

    int pick(int number)
    {
        if( number % 7 == 0)
        {
            cout << setw(7) << "7777777" << setw(7) << number << endl;
        }
        else
        {
            cout << setw(7) << "*******" << setw(7) << number << endl;
        }
        return 0; // Or something that makes more sense in your program.
    }    
    
  2. Update the functions to return void.

    void pick(int number)
    {
        if( number % 7 == 0)
        {
            cout << setw(7) << "7777777" << setw(7) << number << endl;
        }
        else
        {
            cout << setw(7) << "*******" << setw(7) << number << endl;
        }
    }    
    

You can make similar changes to the other overloaded function.

R Sahu
  • 204,454
  • 14
  • 159
  • 270