void displayCost();
double computeArea();
double roundCost();
int main()
{
return 0;
}
void displayCost(string name, int size)
{
double area = computeArea(size);
cout << "Since a " << size << "-inch pizza covers" << area << "square inches, "
<< name << ", then a 12 cents per square inch, the cost will be" << 12*area
<< " - which rounds to" << roundCost();
}
double computeArea(int size)
{
double radius = size/2;
double area = pi*radius*radius;
return area;
}
double roundCost(double price)
{
double roundedCost = ceil(price*100)/100;
return roundedCost;
}
It happens on the line at double area = computeArea(size);. I don't understand why it says I'm not passing an argument in when I clearly am.
double computeArea();
double area = computeArea(size);
double computeArea(int size) {
One of these things is not like the others, ...
You need to fix your prototype (the first one) to match the actual function:
double computeArea(int);
And, of course, ditto for the other prototypes as well.
You are messing up your forward declarations, they also have to reflect the type of parameters of your function.
void displayCost(string, int);
double computeArea(int);
double roundCost(double);
C++ differs from C in that a forward declaration of a function must specify the number and types of arguments. Whereas in C
double computeArea();
means that there is some function called computeArea that returns double, in C++ it is the same as
double computeArea(void);
That is, it specifies that computeArea takes no arguments.
Presumably the difference is because C++ allows functions to be overloaded, whereas C doesn't.
You've declared computeArea to take exactly zero arguments in the prototype (above main). The fact that you've then defined it to take a double down below is beside the point. Inside main, you're calling it with an argument, which, according to the prototype, is wrong.
Related
I am learning C++. I am following along with a learning tutorial. After creating (forgive me if I am not labeling this correctly) a function that calculates exponentials with the for() loop. We are learning how to use a void function to call it. In all of the code there are few times the parameters (hopefully labeling correctly) do not include the return type.
It is creating an itch for me as I really am taking things slow trying to understand everything so far.
#include <iostream>
double power_function(double base, double exponent)
{double solution = 1;
for(double i = 0; i < exponent; i++)
{solution = solution * base;
}
return solution;
}
void print_power(double base, double exponent)
{ double void_operation = power_function(base,exponent);
/* Why do we call without the return type? Cause it is already defined? */
std::cout << "The base of " << base << " with an exponent \n.";
std::cout << " of " << exponent << " is equal to " << void_operation;
}
int main ()
{int base test, exponent test;
std::cout << "What is the base,\n.";
std::cin >> base_test;
std::cout << "What is the exponent?\n.";
std::cin >> exponent_test;
printpower(base_test, exponent_test);
}
When you call functions in C++ you don't need to specify the return type because it's already specified in the function definition above.
Here's a really simple example of this from cplusplus.com
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
addition() has return type int but when its called
z = addition (5,3); the return type is not specified.
First, a function should be declared:
double power_function(double base, double exponent)
This tells the compiler that a function exists named power_function, which takes two double parameters, and returns a double. (The definition might immediately follow, or the definition might be elsewhere, but that's not relevant int his case) From then, on, the compiler knows the parameter types, and the return type.
So when the compiler sees:
double void_operation = power_function(base,exponent);
The compiler will generate code to copy both those parameters as doubles to the stack (converting them to double if needed), and then call the function. When the function returns, the compiler already knows the return type is a double, so it will take the top item on the stack as a double and will assign it to void_operation (again, converting from double if needed).
I want to ask why this program does not generate a compile time error?
int Add(int x, int y){
return (x+y);
}
double Add(double x, double y, double z){
return (x+y);
}
int main()
{
cout<<Add(5,6);
cout<<Add(5.5,6.6);
return 0;
}
Add(5,6); calls int Add(int x, int y). That is clear enough.
Now Add(5.5,6.6) looks for a proper matching function. It finds one with two arguments and one with three. Now it checks if it can use the function with two arguments. And indeed it can convert double to int. So it again uses int Add(int x, int y).
If you would provide an double Add(double x, double y), it would find two functions with two arguments and checks which one "matches best". This would then be double Add(double x, double y).
Because C++ defines an implicit conversion from double to int, which gets applied in the second call.
See C++11[conv.fpint]ยง1:
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
When you call
Add(5.5,6.6);
it gets mapped to function signature
int Add(int x, int y);
so, 5.5 -> 5 and 6.8 -> 6 conversion takes place
and you get the answer as 11 (5 + 6).
The solution comes down to the number of arguments:
For doubles you have
double Add(double x, double y, double z){
return (x+y);
}
this function requires only/exactly 3 variables before it will be utilised, so when you specify only 2 arguments, there is only one choice available:
int Add(int x, int y){
return (x+y);
}
Should you want to make it multi-purpose you would have to change it to
double Add(double x, double y, double z = 0.0){
return (x+y+z);
}
This will allow you to add 2 or 3 doubles together as needed, as 2 double added is the same as 2 doubles added to 0.0.
The argument for z (double z = 0.0) is simply a default value declaration: most compilers work on the basis that, working right to left on the parameter list you can omit parameters which have defaults, therefore I could do the following:
double Add(double x= 0.0, double y=0.0, double z = 0.0)
and each of the following would be valid calls:
Add();
Add(1.0);
Add(1.1,2.2);
Add(1.1,2.2,3.3);
Add(1);
Add(1,2);
Add(1,2,3);
And the values which we do not specify will be in place by utilising the default values we defined earlier (0.0).
Example usage of modified function
#include <iostream>
using namespace std;
double Add(double x, double y, double z = 0.0){
return (x+y+z);
}
int main() {
cout << Add(1.1,2.1,3.1) << endl;
cout << Add(2,3) << endl;
cout << Add(1.0, 2.12345) << endl;
return 0;
}
Hope this helps, but let me know if you need more information or detail :)
Guys this will generate a compile error 'Add' : no overloaded function takes 2 arguments You need three arguments for the ADD functions- both for int and double- to be utilized.
While I appreciate your answers on this, you must understand that function overload in C++ done like this requires exact number of parameters to be specified in the calling.
Visual Studio won't allow u to compile the above source code. Pass three arguments to the calling of functions in main function.
.
.
.
cout << Add(5,6,7) << endl;
cout << Add(5.5,6.6,2) << endl;
.
.
.
Hi i have the below code
# include <iostream>
# include <limits>
# include <cmath>
using namespace std;
class fahrenheit
{
float f,c,x;
public:
void getdata();
void display();
}
void fahrenheit::getdata()
{
cout << "Enter the value of f : ";
cin >> f;
x=f-32;
c=5/9(x); //Here i am getting error as Expression must have (pointer-to-)function type //
}
void fahrenheit::display()
{
cout << "c=" << c;
std::cin.ignore();
std::cin.get();
}
int main()
{
fahrenheit f;
f.getdata();
f.display();
}
i have given the datatype as float for the input variables , but i am not sure what should be done to rectify the error .
5/9(x) doesn't remotely look like C++. You likely meant c = 5.0 / 9.0 * x;
first of all, you forgot about a semicolon right after the class definition.
Second, I presume you wanted to multiply x by 9. Write
c=5/9*(x)
otherwise the compiler tries to find a function called 9(int x) (which is an incorrect name for a function anyway) and realizes that 9 is in no sense any function pointer but just an int.. that's what the error means.
By the way.. if you write 5/9 compiler understands it as int values being divided.
It will divide int(5) by 9 using an int / operator, which after dividing will return
floor(5/9) = 0 . If you want to have a float or double division you have to inform the compiler that your values are floats(doubles).
For doubles: 5.0/9.0*x
For floats: 5.0f/9.0f * x
You should use the multiplication operator *.
c=5/9*(x);
I'm just learning C++ and I have a little code here:
using namespace std;
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
double moon_g();
}
double moon_g (double a, double b)
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
It compiles, but when I run it it only prints out:
This program will calculate the weight of any mass on the moon
but doesn't execute the moon_g function.
This line:
double moon_g();
doesn't actually do anything, it just states that a function double moon_g() exists. What you want is something like this:
double weight = moon_g();
cout << "Weight is " << weight << endl;
This won't work yet, because you don't have a function double moon_g(), what you have is a function double moon_g(double a, double b). But those arguments aren't really used for anything (well, they are, but there's no reason to have them passed in as arguments). So eliminate them from your function like so:
double moon_g()
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
double a;
cin>>a;
double b=(17*9.8)/100;
double mg=a*b;
return mg;
}
(And declare the function before you call it.) More refinements are possible, but that'll be enough for now.
This is a function declaration:
double moon_g();
this won't call a function, and if you did have it correct, which means adding two parameters since that is how you define it below:
moon_g( a, b ) ;
it would not work because you either need to move the definition of moon_g before main or add a forward declaration before main like this:
double moon_g (double a, double b) ;
Although it seems like a and b are not inputs but values you want to return back to main then you would need to use references and it would need to be declared and defined like this:
double moon_g (double &a, double &b) ;
^ ^
A useful thread to read especially if you are starting out would be What is the difference between a definition and a declaration?.
Which compiler you use makes a difference here clang provides the following warning:
warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
double moon_g();
^~
while I can not get gcc nor Visual Studio to warn me about this. It is useful in the long run to try code in different C++ compilers when you can, it can be a very educational experience and you don't have to install them either since there are plenty of online C++ compilers available online.
There is huge difference between calling a function and declaring it just as there is difference between local variables and function arguments.
I suggest reading basic tutorials first.
Anyway, thats how code should look like:
#include <iostream>
using namespace std;
double moon_g ()
{
double a,b;
cout<<"Enter the mass in kilograms. Use decimal point for any number entered\n";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
cout<<"Result is: "<<moon_g();
}
There are two problems in your code.
Firstly, if you want to call your function
double moon_g (double a, double b) // this means if you want to call moon_g() you must provide arguments a and b, otherwise, the you will encounter an compile error.
{
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
you should provide the two parameters a and b.
But a and b are calculated in the body of function definition, it is unnecessary to declare the two parameters. You can write like this.
double moon_g () //this means function moon_g() does not accept any arguments
{
double a, b; // declare a and b in the definition body instead of in the arguments list
cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
cin>>a;
b=(17*9.8)/100;
double mg=a*b;
return mg;
}
Then, in the main function, your calling function statement is wrong. You may want to receive the return value. So, you should write the code like this.
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
double ret = moon_g();
}
Finally, it is mostly recommended that the function which will be called by another function should be declared or defined previously.
Consider following code:
#include <iostream>
using namespace std;
int aaa(int a) {
cout << a * 0.3 << endl;
return a * 0.3;
}
int main()
{
cout << aaa(35000);
}
It prints out:
10500
10499
Why output differs?
I have a workaround to use "return a * 3 / 10;" but I don't like it.
Edit:
Found that doing "return float(a * 0.3);" gives expected value;
The result of 0.3*35000 is a floating point number, just slightly less than 10500. When printed it is rounded to 10500, but when coerced into an int the fractional digits are discarded, resulting in 10499.
int * double expression yields double, that's what the first thing prints.
Then you convert to int chopping the remaining part (even if it's almost there, sitting at 10500-DBL_EPSILON), and pass that back. The second prints that value.
float-int conversions should be made with care, better not at all.
a * 0.3 has type double. The call inside aaa calls
ostream& operator<< (double val);
whereas the one outside calls
ostream& operator<< (int val);
You'd get a warning (if you turn them on - I suggest you do) that the implicit cast from double to int isn't recommended.