Weird result from function

P

Protoman

I keep getting -1#INF from this code; I can't figure out what's wrong.

#include <iostream>
#include <cstdlib>
#include <memory>
using namespace std;


class invalidArg
{
public:
virtual void Write(long long& arg)const{cout << arg << " is invalid. "
<< endl;}
virtual void Write(long long& arg,long long& arg2)const{cout << arg <<
"and "
<< arg2 << " are invalid. " << endl;}
};

class numbers
{
public:
numbers():pi(3.1415926535),e(2.71828182846){}
const long double pi;
const long double e;
};

class math:protected numbers
{
public:
long long* exp(const long long& base,long long expn)throw(invalidArg)
{
if(base<=0)
throw invalidArg();
else if(base<=0&&expn<=0)
throw invalidArg();
long long* result=new long long(1);
for(;expn>0;expn--)
*result*=base;
return result;
}
long double* area(const long long& radius)throw(invalidArg)
{
if(radius<=0)
throw invalidArg();
long long temp=*exp(radius,2);
long double temp2=pi*temp;
long double* result=new long double(temp2);
return result;
}
};

math Math;
int main()
{
auto_ptr<long long> radius(new long long);
try
{
cout << "Enter a radius: ";
cin >> *radius;
auto_ptr<long double> temp(Math.area(*radius));
cout << "Here's the area of a circle with a " << *radius << " radius: "
<< *temp << endl;
}
catch(invalidArg& inv)
{
inv.Write(*radius);
}
system("PAUSE");
return 0;
}

Can you help me?
 
V

Victor Bazarov

Protoman said:
I keep getting -1#INF from this code; I can't figure out what's wrong.

#include <iostream>
#include <cstdlib>
#include <memory>
using namespace std;


class invalidArg
{
public:
virtual void Write(long long& arg)const{cout << arg << " is invalid. "
[...]

First of all 'long long' is non-standard. Second of all, this code
asks for input. Since you didn't say what input you're giving it to
get "-1#INF", we can't really figure out it either. I tried your code
with and without 'long long', and got expected behaviour.

V
 
P

Protoman

If i input anything, it won't work (except 0 and below, which simply
throws the exception).
 
J

John Harrison

Protoman said:
If i input anything, it won't work (except 0 and below, which simply
throws the exception).

Work's for me too.

Have to say its some of the worst concieved code I've seen in a long
while. Any good reason for using classes, inheritance and auto_ptr to
solve a simple math problem? And you have a memory leak, when area calls
exp the memory allocated by exp never gets freed.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top