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++);
{
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);
}
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, 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"