Hi guys, here i have a problem, that i have been trying to rectify for past 1 and half hour.
Here in the code, as you can see, When the user Press Y, He get the chance to input the exponent value. There i have added another nested if, so that the cout statement will be output with proper superscripts (2nd, 3rd, 4th etc etc).
But with the nested if in place the "code have no effect error" occur.

#include<iostream.h>
#include<conio.h>
#include<math.h>

int power(int m, int n=2)
{
	m=pow(m,n);
	return m;
}

int main()
{
	clrscr();
	char o;
	int m,n;
	cout<<"Enter the value for base?"<<endl;
	cin>>m;
	cout<<"Would you like to specify value for exponent?"<<endl;
	cout<<"If Yes Press Y else press N"<<endl;
	cout<<"NB: If you don't specify a exponent value, the base will be Squared By Default"<<endl;
	here:
	cin>>o;
	if(o=='y'||o=='Y')
      {	cout<<"Enter the exponent value"<<endl;
	cin>>n;
	int c=n%10;
	      {	if(c==0||c==4||c==5||c==6||c==7||c==8||c==9)
		cout<<n<<"th";
		else if(c==2)
		cout<<n<<"nd";
		else(c==3)
		cout<<n<<"rd"; }

	 cout<<" power of base= "<<power(m,n);
	 }

	else if(o=='n'||o=='N')
      {	cout<<"Square of base is "<<power(m); }
	else
      {	cout<<"Wrong Option"<<endl;
	cout<<"Enter a valid option (Y/N)"<<endl;
	goto here; }
	getch();
	return 0;
}

>> But with the nested if in place the "code have no effect error" occur.

First, I imagine it's a warning, not an error. Depending on compiler flags, warnings may be turned into errors.

Second, what line number and what's the exact error message?

The error occurred when i inserted lines 26 to 32.
And compiler say "Code have no effect" & "Statement missing" error.
But according to my knowledge, Nested ifs are possible in C++. We do it often....

Among a lot of other problems this curly bracket on line 27 is misplaced. You should probably place it after the statement, instead of in front of it.

Why the { on line 27?
What if n%10=1? i.e n=11 or 21?

EDIT
Beat me to that Nick:)
I have also been led to believe goto is BAD. A while or do while loop may be better.

Edited 5 Years Ago by frogboy77: n/a

Well guys,
Thanks to Vernon, I got the problem rectified.....
Thank You very much.....
Well
Nick, it a common convention in C++ to put statements as compound statements....Mostly to make it more compact or less complicated....So now the whole Nested if is a compound statement....
Anyway Thanks guys....

>>Nick, it a common convention in C++ to put statements as compound statements....Mostly to make it more compact or less complicated....So now the whole Nested if is a compound statement....
Yes, it is, but there is no point in making Lines 27-32 a compound statement; it doesn't accomplish anything for you. In fact, in this instance it actually "muddies up the waters", making your code more difficult to read instead of easier. You're usually better off using "whitespace" (indenting and empty Lines) to improve the readability, that way you're not adding extra (visible) characters to your code. Observe:
Yours:

int main()
{

//...

if(o=='y'||o=='Y')
      {	cout<<"Enter the exponent value"<<endl;
	cin>>n;
	int c=n%10;
	      {	if(c==0||c==4||c==5||c==6||c==7||c==8||c==9)
		cout<<n<<"th";
		else if(c==2)
		cout<<n<<"nd";
		else(c==3)
		cout<<n<<"rd"; }
 
	 cout<<" power of base= "<<power(m,n);
	 }

Mine:

int main()
{

  //...

  if(o=='y'||o=='Y')
  {
    cout<<"Enter the exponent value"<<endl;
    cin>>n;

    int c=n%10;

    if(c==0||c==4||c==5||c==6||c==7||c==8||c==9)
      cout<<n<<"th";
    else if(c==2)
      cout<<n<<"nd";
    else(c==3)
      cout<<n<<"rd";
 
    cout<<" power of base= "<<power(m,n);
  }

Compound statements are used in control structures to indicate what statements/commands get executed as part of the control structure. i.e.

if (someCondition == true)
{ //begin compound statement 1
  statement 1-1;
  statement 1-2;
  statement 1-3;
} //end compound statement 1
else
{ //begin compound statement 2
  statement 2-1;
  statement 2-2;
  statement 2-3;
  //...
} //end compound statement 2

Or, you can use them to control the scope of a variable:

int main() { //begin main() scope
  int someInt = 10;

  std::cout << "The value of someInt is: " << someInt << std::endl;

  { //begin sub-scope 1
    int someOtherInt = 5;
    std::cout << "The value of someOtherInt is: " << someOtherInt << std::endl;
  } //end sub-scope 1

  std::cout << "The value of someInt is still: " << someInt << std::endl;
  std::cout << "The value of someOtherInt is: " << someOtherInt << std::endl; //ERROR: someOtherInt is no longer in scope!

  return 0;
}

There is nothing in Lines 27-32 that warrants making those lines a compound statement. If you wanted 'c' to have a limited life/scope, you could use a compound statement that contains Lines 26 - 32. But as currently written, it is a useless construct.

Edited 5 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.