C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 6th, 2009, 02:37 PM
spazamatic2 spazamatic2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 17 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 23 sec
Reputation Power: 0
Functions and powers

I have written a program to have the user enter 2 values and put the first one as the base and the second as the exponent. It comes out the right answer but it still says error. I'm not sure why when the answer is right it outputs error instead of verified. I also want to know why it only verifies certain numbers

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

using namespace std;

float add (float a, float b)
{
float c = a + b;
return c;
}

float subtract (float a, float b)
{
float c = a - b;
return c;
}
float multiply (float a, float b)
{
float c = a * b;
return c;
}

float divide (float a, float b)
{
float c = (a/b);
return c;
}

float pow ( int a, int b)
{
float c = a^b;
return c;
}

int main ()
{

float x;
float y;

cout << "Please enter a number " << endl;
cin >> x;
cout << " Please enter an integer " << endl;
cin >> y;

float power = pow (x,y);
cout << "The answer is " << power << endl;
if (power == pow (x,y))
{
cout << "Verified" << endl;
}
else if ( power != pow (x,y))
{
cout << "Error" << endl;
}



return 0;
}

Reply With Quote
  #2  
Old November 6th, 2009, 03:17 PM
openoutcome openoutcome is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 2 openoutcome User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 55 sec
Reputation Power: 0
Are you using a C compiler or C++ compiler? The header file used by C++ for the function pow() is cmath: Header File: math.h (C) or cmath (C++). Hope this helps.

Reply With Quote
  #3  
Old November 6th, 2009, 06:06 PM
spazamatic2 spazamatic2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 17 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 23 sec
Reputation Power: 0
Thank you!!

Reply With Quote
  #4  
Old November 6th, 2009, 07:07 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 990 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 15 h 32 m 39 sec
Reputation Power: 3
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
Originally Posted by spazamatic2
I have written a program to have the user enter 2 values and put the first one as the base and the second as the exponent. It comes out the right answer but it still says error. I'm not sure why when the answer is right it outputs error instead of verified. I also want to know why it only verifies certain numbers


You -may- have heard of why == is usually a bad idea on floats:
http://www.parashift.com/c++-faq-li....html#faq-29.17

Make sure to also read the one just below it; it's even more relevant for your situation.


Have you considered the possibility of both your ifs (verified/error) being false yet?


Code:
float pow ( int a, int b)
{
float c = a^b;  // oops... ^ is the xor operator, not the power operator
return c;
}


That is returns the correct answer means it's not calling your function. (See overload resolution; you have several pow() taking two arguments in scope, and it picks the best one based on the arguments you pass to it)

The correct thing to do in any case is to call the standard library function pow(), instead of defining your own, (usually, like here) incorrect one.

Having included <math.h> it's already there for your use. <cmath> usually contains something along the lines of namespace std {
#include <math.h>
}

so with the using namespace std; in place there's no real difference.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place.

Last edited by MaHuJa : November 6th, 2009 at 07:11 PM.

Reply With Quote
  #5  
Old November 7th, 2009, 12:38 PM
spazamatic2 spazamatic2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 17 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 23 sec
Reputation Power: 0
Oh great thank you! I am VERY new to this so I'm just picking up things as I go.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Functions and powers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek