Reputation: 133
Just wondering why this area calculation code only returns whole numbers, even though I used double throughout.
#include <iostream>
#define pi 3.14
piCalc(double radi) {
double result;
result = (radi * radi) * pi;
return result;
}
int main() {
double radius;
std::cout << "Welcome to the circle area calculator. Please enter your radius"
<< std::endl;
std::cin >> radius;
std::cout << "your answer is " << piCalc(radius) << std::endl;
std::cout << "thankyou for using the area calculator" << std::endl;
}
Even when i enter a float, for example 5.3, i still only return 88, when it should be 88.25. Thanks.
Upvotes: 0
Views: 151
Reputation: 122133
Here:
piCalc(double radi){
You are missing the return type. This isn't valid C++. In (ancient?) C you could omit the return type and int
was assumed. Some compilers still allow this as non-standard extension. Make the return type double
:
double piCalc(double radi){
In general you need to take care with compiler extensions. For example gcc is rather lax with its default settings and compiles lots of code that shouldn't according to standard C++. -pedantic
option can help to spot such undesired use of compiler specific extensions.
Upvotes: 5