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, 03:37 PM
spazamatic2 spazamatic2 is offline
Registered User
Click here for more information.
 
Join Date: Oct 2009
Posts: 12 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 4 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, 04: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, 07:06 PM
spazamatic2 spazamatic2 is offline
Registered User
Click here for more information.
 
Join Date: Oct 2009
Posts: 12 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 4 sec
Reputation Power: 0
Thank you!!

Reply With Quote
  #4  
Old November 6th, 2009, 08:07 PM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 880 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: 6 Days 19 h 48 m 25 sec
Reputation Power: 2
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 08:11 PM.

Reply With Quote
  #5  
Old November 7th, 2009, 01:38 PM
spazamatic2 spazamatic2 is offline
Registered User
Click here for more information.
 
Join Date: Oct 2009
Posts: 12 spazamatic2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 4 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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




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