There are a couple of problems with this code. first the line
cout << avg;
Actually prints out the function pointer to avg(). Second in the function avg() the identifier avg is actually the pointer to the function avg() which is the only identifer with that name in the scope since it is not redefined inside of the function. avg()
double avg(double a, double c, double c){
return (a + b + c)/3;
}
This would also work.
double avg(double a, double c, double c){
double avg = (a + b + c)/3;
return avg;
}
2006-10-23 14:11:52
·
answer #1
·
answered by run4ever79 3
·
0⤊
0⤋
I managed to get it running...what I did was I added a new variable (called avg1) and gave your function a return value (if a function is not void it needs to return something), and renamed all references to avg to avg1.
The reason why you were getting that error was because (the computer thinks) you were tiring to preform an aggregate (an operation that addresses the function as one item) operation, which is not allowed in C++ unless you do some type of overloading, regardless please take a look at my solution and learn from it. Best of luck in the programming world, its really fun!!!
#include
#include
using namespace std;
double avg (double a, double b, double c,double avg1);
int main ()
{
double a, b, c,avg1;
avg(a,b,c,avg1);
cout<<"Enter a Number that is greater than zero:";
cin>>a;
cout<<"Enter another Number greater than zero:";
cin>>b;
cout<<"Enter the Final number:";
cin>>c;
cout<<"The average of the three numbers is:";
cout<
char z;
cin>> z;
return 0;
}
double avg (double a, double b, double c,double avg1)
{
avg1=(a+b+c)/3;
return 0;
}
2006-10-23 13:27:31
·
answer #2
·
answered by D 4
·
0⤊
0⤋
You use function name as a left operand in assignment:
double avg (double a, double b, double c)
{
avg=(a+b+c)/3;
}
It is valid in some programming languages but not in C/C++. Simply do the following:
double avg (double a, double b, double c)
{
return (a+b+c)/3;
}
2006-10-23 14:27:01
·
answer #3
·
answered by alakit013 5
·
0⤊
0⤋
Above answers are right - you cannot say "avg = ***" on the last line in C++ (or C). You need "return (a + b + c)/3;".
There are 2 types of things that look like variables in C. They are called "lvalues" and "rvalues", but "variables" and "constants" might be better terms. Both hold a value, but an rvalue's value is assigned at compile time and cannot be changed. ("rvalue" is short for "right value" because it can only be on the right of an assignment. "lvalue" is "left value" because it can be on the left of an assignment)
In C a function name is actually a pointer to the function - this can be valuable if you want to include a function in a table. As an example, if you write a calculater program you might want to set up a table mapping from function character to function:
{
{'+', plus};
{'-', minus};
{'*', times};
{'/', divide};
};
could be looped through to call the proper function for each command. The compiler assigns the value when it compiles the preogram. (actually, the loader assigns the value when the program is run, but it follows the compiler's instructions)
An lvalue can have its assignment made at runtime. Most variables are lvalues. For them you can make an assignment.
In your example, avg is an rvalue because it is a function address, and so cannot be assigned to.
2006-10-23 13:39:23
·
answer #4
·
answered by sofarsogood 5
·
0⤊
0⤋
when u declare a int, double, char etc you have to return something
double avg(double a, double b, double c)
{
double t;
t = (a+b+c)/3
return t;
}
then change
cout<<"The average of the three numbers is:";
cout<
2006-10-23 14:08:32
·
answer #5
·
answered by Hawk 2
·
0⤊
0⤋
What's going on in your two subroutines, number and suit?? You can't set number=num or suit='H' without overloading something. That's what your error message is complaining about. And why aren't you returning anything from these functions?? (Or from main for that matter?) I think you actually want: // gives each card a number between 1-13 inclusive. int number(int num) { if (num<=13) return(num); else if (num>13 && num<=26) return(num-13); else if (num>26 && num<=39) return(num-26); else return(num-39); } // assigns each card a suit. H, S, D, or C. char suit(int num) { if (num<=13) return('H'); else if (num>13 && num<=26) return('S'); else if (num>26 && num<=39) return('D)'; else return('C'); } And put in a return(0) at the end of main. When you declare a function like int number(int) you mean that the function number takes an int as input and returns an int to the calling program.
2016-03-28 05:31:17
·
answer #6
·
answered by ? 4
·
0⤊
0⤋
maybe i'm reading this wrong (my C is rusty) but...
double avg (double a, double b, double c)
{
avg=(a+b+c)/3;
}
you've declared a method as avg, and a variable as avg.
double avg (double a, double b, double c)
{
double average;
average=(a+b+c)/3;
}
2006-10-23 13:16:53
·
answer #7
·
answered by javaHungerForce 3
·
0⤊
0⤋
the easiest way to fix it is to put your disk to your computer and let it run to do a repair it will ask you what you want to do, or do a restore it back a week or two when you didn't have this problem
2006-10-23 13:09:41
·
answer #8
·
answered by rich_below 4
·
0⤊
2⤋
i gotta learn more about c++ srry.
2006-10-23 13:42:36
·
answer #9
·
answered by animefan2k5 1
·
0⤊
0⤋