Function result is not accurate

Hello Dear,

please check following program which is running fine but output result is not correct. please help me to sort out issue.

[code]

#include<iostream>
using namespace std;
double raiseTopow(double,int );//function declaration
main()
{
double x;
int power;
cout<<"please enter the number:";
cin>>x;
cout<<"please enter power of number:";
cin>>power;
cout<<"the value of "<<x<<" after raise to pow "<<power<<" is "<<raiseTopow(x,power);
}
double raiseTopow(double x ,int power)//function to calculate the raise to power
{
double result;
int i;
result=1.0;
for(i=1;i<=power;i++);
{

result=result*x;
}
return(result)[/b];
}[/b]
*************************************

in following program when rad1 and rad2 are not define in function so how can function understand rad1 and rad2 value and given correct result. please explain this to me. i would be very thankful to you.


//calculation of area of ring by function
#include<iostream>
using namespace std;
double circleArea(double);//function declaration
main()
{
double rad1,rad2,ringArea;
cout<<"please enter the radius of outer circle:";
cin>>rad1;
cout<<"please enter the radius of inner circle:";
cin>>rad2;
if(rad1<=rad2||rad1==rad2)
{
cout<<"outer circle radius should be greater than inner circle radius, please re enter value:";
}
else
{

ringArea=circleArea(rad1)-circleArea(rad2);
cout<<"Area of the ring having outer radius "<<rad1<<" inner radius "<<rad2<<" is = "<<ringArea;
}
}

double circleArea(double radius)//defining circle area function

{
//pi is equal to 3.14
return(3.14*radius*radius);
}
Last edited on
The issue is the semi-colon following the for loop, which causes the empty statement ( ; )to run instead of the compound statement (the braces).
for(i=1;i<=power;i++) ; // remove the semicolon following the loop header.

You will find C++ much easier to learn if you use a modern compiler, and compile with warnings.

Indeed, all three of the modern compilers I tried complained:
main.cpp: In function 'double raiseTopow(double, int)':
main.cpp:19:1: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
 for(i=1;i<=power;i++);
 ^~~
main.cpp:20:1: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
 {
mbozzi wrote:
main.cpp:19:1: warning: this 'for' clause does not guard... [-Wmisleading-indentation]

@mbozzi, thank you very much for that! I’ve never heard about -Wmisleading-indentation, but it looks incredibly useful. I look forward for trying it.
g++: One of the many warnings turned on by -Wall is -Wmisleading-indentation
https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options

Compile C++ code with:

g++: -std=c++14 -Wall -Wextra -pedantic-errors
(or -std=c++17, but beware: the library is nowhere near C++17 as advertised.)

clang++: -std=c++14 -Weverything -pedantic-errors
(On non-BSD implemetations also add -stdlib=libc++ if libc++ is available).

Microsoft: -std:c++latest -W4 -analyze
(Ideally, also enable C++CoreGuidelines checks as part of -analyze static analysis).
@mbozzi, thank you dear, i got it. please recommend me latest compiler currently i am using dev c++ also please respond on 2nd program issue as well " when rad1 and rad2 are not define in function so how can function understand rad1 and rad2 value and given correct result"

Topic archived. No new replies allowed.