Whats wrong with this C++ code?

Status
Not open for further replies.

zegulas

Traceur
#include<iostream.h>

int main()
{
int n;
double m;
char a;

void power(double,int);

cout<<"Enter the number"<<endl;
cin>>m;

cout<<"You want to raise this number to power 2. Right?";
cout<<endl<<"if YES, enter Y else enter N"<<endl;
cin>>a;

if(a=='Y')
{
n=1;
}
else
{
cout<<"Enter the power to which you want to raise"<<m<<endl;
cin>>n;
}
power(m,n);

}

void power(double m,int n)
{
double x=1;

if(n==1)
{
x=m*m;
}
else
{
for(i=0;i<n;i++)
{
x*=m;
}
}

if(n==1)
{
cout<<"the given number: "<<m;
cout<<"raised to power 2 gives the value: "<<x<<endl;
}
else
{
cout<<"The given number: "<<m;
cout<<"raised to power: "<<n;
cout<<"gives the value: "<<x<<endl;
}

}






I attended my 1st C++ lecture yesterday and learnt this program, after trying to compile & run it in Dev C++, it just runs n goes away in fraction of a second, I don't even know if it shows correct answer or not!
 

QwertyManiac

Commander in Chief
There is nothing wrong with the program except for a few small defects here and there.

Here is the corrected one. Run it via Command Prompt to see the output. Else ask for a garbage input at the end.

Code:
Edit: Snipped for reducing post length and thread redundancy. ;)

See below posts for the corrected version of your program.

Now for an efficient and better implementation of the same program:
Code:
#include<iostream>
#include<math.h>

using namespace std;

int main(void)
{
    int Number(0), Power(0);
    cout<<"Enter a number: ";
    cin>>Number;
    cout<<"Enter a power to raise it to: ";
    cin>>Power;
    cout<<endl<<"Number "<<Number<<" raised to a power of "<<Power<<" is: "<<pow(Number,Power)<<endl;
    return 0;
}

Do indent your code next time onwards so that its easy to read. :)
 
Last edited:

aditya.shevade

Console Junkie
First, stop using iostream.h. Use iostream.

Try this
Code:
#include<iostream>

using namespace std;

void power(double,int);

int main()
{
int n;
double m;
char a;

cout<<"Enter the number"<<endl;
cin>>m;

cout<<"You want to raise this number to power 2. Right?";
cout<<endl<<"if YES, enter Y else enter N"<<endl;
cin>>a;

if(a=='Y'||a=='y')
{
n=1;
}
else
{
cout<<"Enter the power to which you want to raise"<<m<<endl;
cin>>n;
}
power(m,n);

}

void power(double m,int n)
{
double x=1;

if(n==1)
{
x=m*m;
}
else
{
for(i=0;i<n;i++)
{
x*=m;
}
}

if(n==1)
{
cout<<"the given number: "<<m;
cout<<"raised to power 2 gives the value: "<<x<<endl;
}
else
{
cout<<"The given number: "<<m;
cout<<"raised to power: "<<n;
cout<<"gives the value: "<<x<<endl;
}

}
 

eggman

I have Yolks not Brains!
A little edit here and there
Code:
#include<iostream>
using namespace std;
int main()
{[b]int i;[/b]
int n;
double m;
char a;

void power(double,int);

cout<<"Enter the number"<<endl;
cin>>m;

cout<<"You want to raise this number to power 2. Right?";
cout<<endl<<"if YES, enter Y else enter N"<<endl;
cin>>a;

if(a=='Y')
{
n=1;
}
else
{
cout<<"Enter the power to which you want to raise"<<m<<endl;
cin>>n;
}
power(m,n);

}

void power(double m,int n)
{[b]int i;[/b]
double x=1;

if(n==1)
{
x=m*m;
}
else
{
for(i=0;i<n;i++)
{
x*=m;
}
}

if(n==1)
{
cout<<"the given number: "<<m;
cout<<"raised to power 2 gives the value: "<<x<<endl;
}
else
{
cout<<"The given number: "<<m;
cout<<"raised to power: "<<n;
cout<<"gives the value: "<<x<<endl;
}
[b]int temp;
cin>>temp;[/b]
}
 
Status
Not open for further replies.
Top Bottom