OUTPUT "MUST RETAIN A VALUE"

Ok so i am supposed to write a program to calculate someone's height from feet and inches to meters through a programmer defined program. I got the errors down to : "error C4716: 'output' : must return a value" and i have not been able to figure out why.

If anyone could help me that would be much appreciated.




/***************************************************compiler directives*************************************************/
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;

// functions
void input(int heightFt, float heightIn);
void height(int heightFt, float heightIn, float heightMe);
float output(float& heightMe);


/**************************************************main*******************************************************************/

int main()
{
char userChoice;
int heightFt;
float heightIn;
float heightMe;

do
{
input(heightFt, heightIn);
height(heightFt, heightIn, heightMe);
output(heightMe);

cout << "claculate another height? (Y/N) ---> " << endl;
cin >> userChoice;
}
while ((userChoice == 'Y') || (userChoice == 'y'));

system("pause");

return 0;
}

void input(int heightFt, float heightIn)
{
cout << "Please enter your height in Feet ---> " << endl;
cin >> heightFt;

cout << "Please enter your remaining height in inches ---> " << endl;
cin >> heightIn;
}

void height()
{
int heightFt;
float heightIn;
float heightMe;

(heightIn/12);
heightMe = ((heightIn+heightFt)*(0.3048));
}

float output(int heightFt, float heightIn, float heightMe)
{

cout << "Your height in meters is ---> " << output(heightMe);

}


1
2
3
4
5
6
float output(int heightFt, float heightIn, float heightMe)
{

cout << "Your height in meters is ---> " << output(heightMe);

}


"float" at the start is the return type; this means it has to return a variable of type float but you don't return anything. Change it to "void" (which means it won't returna value) and it will work.

I see three problems right off the bat:

1) Your function prototype doesn't match your function body.

1
2
float output(float& heightMe);  // your prototype (1 parameter)
float output(int heightFt, float heightIn, float heightMe)  // your body (3 parameters) 

One of them is wrong (looks like your function body.

2) Output calls itself repeatedly (infinite recursion). See this:

cout << "Your height in meters is ---> " << output(heightMe); // <- you're calling output() here

If output calls itself... then when you call output, it will call output, which calls output, which calls output, which calls output, etc, etc, etc. It's like an infinite loop.


3) You gave output a return type. See how it returns float:

float output(float& heightMe); // <- see the first 'float' here? That means you must return a float from the function

Therefore you must actually return something from the body of the function. However I question whether or not this function needs to return anything. Maybe you should change it to a void function.
Thanks for your help so far but now when i changed the program to:

void output()
{
float heightMe;
int heightFt;
float heightIn;

cout << "Your height in meters is ---> " << height(heightFt, heightIn, heightMe);

}

i am getting: "error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)" this error instead of the others.
Last edited on
hey guys i appreciated your help earlier but i cant figure out why i am getting this error message and i am wondering if anyone had any ides it is the same program with the same headers.

void output()
{
float heightMe;
int heightFt;
float heightIn;

cout << "Your height in meters is ---> " << height(heightFt, heightIn, heightMe);

}

the error message is:
"error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)" this error instead of the others.

if anyone had any input it would be great the assignment is due tomorrow and i could use the help.
Last edited on
the function "height" is type "void", so it doesn't return a value (as expect by the complier).

try to return a value (changing the function type).
You are misunderstanding return types.

The return type specifies what a function call resolves to in an expression. In English, this means that when you call a function, the function call effectively gets "replaced" with whatever the function returns.

(note it's not really "replaced", I'm just illustrating the concept here)

Here's a simple example:

1
2
3
4
5
6
7
8
9
int func()
{
  return 5;
}

int main()
{
  cout << func();
}


Since 'func' returns 5, this means that the function call is "replaced with" 5.

So cout << func(); becomes cout << 5;. Therefore the above code would print 5.

A slightly more complicated example:

1
2
3
4
5
6
7
8
9
10
11
int add(int a, int b)
{
  return a+b;
}

int main()
{
  int foo = add(4,9);

  cout << foo;
}


Our 'add' function adds the two numbers together, and returns the sum. Therefore int foo = add(4,9); becomes int foo = 13; because the function will return 13.


void functions have no return type, so they don't get replaced with anything. Here's an example:

1
2
3
4
5
6
7
8
9
10
void func()
{
  cout << "Example";
  // not returning anything because this is a void function
}

int main()
{
  int foo = func();  // ERROR, see why below
}


That line of code will give you an error because func() doesn't return anything (it's void!), so you can't use it in an expression like that. Think about it... what would you be setting 'foo' to in that code? You can't set foo to nothing. That's why it's an error.

That's basically what you're doing. You're calling a void function and using it as if it returned something. You're doing this:

1
2
3
4
5
6
7
8
9
void func()
{
  // stuff here
}

int main()
{
  cout << func(); // ERROR
}


What do you expect cout to print? 'func' doesn't return anything, so how can cout print anything?
Last edited on
Topic archived. No new replies allowed.