Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 06-03-2008, 06:54 PM   #1 (permalink)
Traceur
 
zegulas's Avatar
 
Join Date: Oct 2003
Location: Mumbai
Posts: 276
Default Whats wrong with this C++ code?


Quote:
#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!
__________________
Parkour: A journey started by two & followed by many...
zegulas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 06-03-2008, 07:14 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Whats wrong with this C++ code?

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.
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 06-03-2008 at 07:24 PM.
QwertyManiac is offline  
Old 06-03-2008, 07:14 PM   #3 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: Whats wrong with this C++ code?

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;
}

}
__________________
--- Console Junkie
aditya.shevade is offline  
Old 06-03-2008, 07:17 PM   #4 (permalink)
I have Yolks not Brains!
 
eggman's Avatar
 
Join Date: Aug 2006
Location: Inside the shell
Posts: 743
Default Re: Whats wrong with this C++ code?

A little edit here and there
Code:
#include<iostream>
using namespace std;
int main()
{int i;
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)
{int i;
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;
}
int temp;
cin>>temp;
}
__________________
Y U NO ALLOW PICTURES IN SIGNATURES?
eggman is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats Wrong WIth My Monitor? pannaguma Hardware Q&A 16 03-10-2007 07:45 AM
Whats wrong? JSP in netbeans? nithinks QnA (read only) 6 08-03-2007 05:08 AM
Whats wrong with my laptop? kumars Software Q&A 2 29-12-2006 08:24 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:53 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2