473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Problem with Exponent program

I'm getting a compilation error with this program, something about the
instation of invArg; here's the code:

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

template<class T>
class invArg
{
public:
invArg(T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
base*=exp;
return ret;
}
}
};

int main()
{
for(;;)
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
}
catch(invArg<long double>& inv)
{
inv.Write();
}
system("PAUSE");
return 0;
}
}

I think the type checking is too strong; any suggestions? Thanks!!!

Dec 18 '05 #1
11 3528
Protoman wrote:
I'm getting a compilation error with this program, something about the
instation of invArg; here's the code:
"Something about..." is not a very good description of an error message.
template<class T>
class invArg
{
public:
invArg(T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};


The constructor should take `const T &`. It doesn't now and that is why

throw invArg<T> (base);

fails to compile; `base` is `const T &`.

I would also consider making the constructor `explicit`.

Martin.
Dec 18 '05 #2
Here's the new code; now after I type the numbers, the program closes.
What do I do?

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

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
}
}

Any suggestions? Thanks!!!

Dec 19 '05 #3
Protoman wrote:
Here's the new code; now after I type the numbers, the program closes.
What do I do?
First, please indent your program properly. You make it VERY hard to
follow your logic when everything is on the left margin.

Second, how can we know if you don't tell us what the numbers are that
resulted in the behavior?
#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
What on earth is the above supposed to do? Are you missing a return
statement? In any case, if exp is negative, you get an infinite loop
until you overrun your stack. Please explain what you intended with
the above.
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
??? You initialize ret to 1, then you never change ret. Therefore, the
above will always return 1. Meanwhile, exp gets bigger and bigger -
how will it ever get to zero?
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
Probably you should insert another system("PAUSE") here, in case your
program throws (which I assume it did).
}
}


You know, I think it's fairly likely you're just a troll, but I suppose
you are mildly entertaining.

Best regards,

Tom

Dec 19 '05 #4
OK, I'm trying to make it work like exponents in algebra work; i.e.
negative exponents are reciprocals of their positive counterparts,
fractional exponents are roots, like that. And I'm trying to make it
throw if one of these four things: the base is 0, both the base and exp
is 0, or its 0 rasised to zero or a negative. Does that make it
clearer? Thanks!!!

Dec 19 '05 #5

"Protoman" <Pr**********@gmail.com> wrote in message
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
}
}


Pass a negative number as the exponent to that function. What happens? It
calls operator() again, passing it the SAME values. Which means it passes a
negative value for exp again. So then what happens...?

Also, look at the loop. What's it doing? Look carefully, especially at how
ret gets its value...

-Howard

Dec 19 '05 #6

Howard wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
}
}

Pass a negative number as the exponent to that function. What happens? It
calls operator() again, passing it the SAME values. Which means it passes a
negative value for exp again. So then what happens...?


OK, this should work, but how do I get it to stop closing after I
input?

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

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
goto neg;
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
ret*=base;
return ret;
}
neg:
{
T ret=1;
for(;exp--;)
ret*=base;
return 1/ret;
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
}
}

Any suggestions? Thanks!!! Also, look at the loop. What's it doing? Look carefully, especially at how
ret gets its value...

-Howard


Dec 19 '05 #7

Protoman wrote:
OK, this should work, but how do I get it to stop closing after I
input?
Is this a joke?
What do you mean by "work"?

See comments below.

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

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
It's not very helpful to just print the argument without any message
now, is it?
private:
T inv;
};

template<class T>
class Exp
Why make this a class and not a freestanding function?

You are not using any object orientated features here that would
require this to be a class. Don't do it simply for the sake of saying
"I made a class".
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
Why is exp not a const reference? It should be.
{
if(exp<0)
goto neg;
GOTO??? There is absolutely no need to use goto to achive the desired
program flow here. When you code, don't think about the exceptions
first, think about the commonalities....
else
{
T ret=1;
for(;exp--;)
A while loop would be way more appropriate here.

But the bigger issue is that this can only work for integer values of
exp. I.e. if exp is a fraction (which it can be based on you declaring
it as long double), this code will endless-loop.
ret*=base;
return ret;
}
neg:
{
T ret=1;
for(;exp--;)
ret*=base;
return 1/ret;
You are performing the same actions here as in the "positive" case, but
only invert the result. This can be done way simpler without using
goto.
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
You are never checking if this succeeded (cin still valid)...
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
This is not portable.
If you really have to do this (I'm sure your teacher would be ok
without it), just to a getline( cin, temp); or so...
return 0;
}
catch(invArg<const long double>& inv)
Wrong....

catch( const invArg<long double>& inv)
{
inv.Write();
}
}


Dec 19 '05 #8

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Howard wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
> T operator()(const T& base, T exp)throw(invArg<T>)
> {
> if(exp<0)
> 1/operator()(base,exp);
> else if(base==0)
> throw invArg<T>(base);
> else
> {
> T ret=1;
> for(;exp--;)
> exp*=base;
> return ret;
> }
> }
>


Pass a negative number as the exponent to that function. What happens?
It
calls operator() again, passing it the SAME values. Which means it
passes a
negative value for exp again. So then what happens...?


OK, this should work, but how do I get it to stop closing after I
input?

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

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
goto neg;
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
ret*=base;
return ret;
}
neg:
{
T ret=1;
for(;exp--;)
ret*=base;
return 1/ret;
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
}
}

Any suggestions? Thanks!!!


Yes, I have suggestions:

1) Read the other posts. One of the responders tells you why your app is
closing.
2) Use your debugger. It will tell you two things: first, that your
solution above does not work properly, and second, what it's doing when it
closes.
3) Why have you used a goto call? That's rarely a good design choice. Why
not just a block of code right where you need it? Or better yet, why not do
it the way you were (sort of), but pass the correct values when exp is
negative?
4) Do something about the editor or newsreader you're using: without
indentation, this is very hard to read.
5) What data types is this supposed to operate on? Consider especially what
it means to call the operator with a negative exponent when your type is
integral. If you only need to handle double (and/or float) do you really
need a template for this class? Seems like just handling the double type
with a function would work, without templates at all.
6) There's already a function called pow which handles raising to a power,
so why rewrite it?

-Howard

Dec 20 '05 #9

in*****@gmail.com wrote:
If you really have to do this (I'm sure your teacher would be ok
without it), just to a getline( cin, temp); or so...


This is not homework, it's a hobby. And does pow handle negative
exponents?

Dec 20 '05 #10

Protoman wrote:
in*****@gmail.com wrote:
If you really have to do this (I'm sure your teacher would be ok
without it), just to a getline( cin, temp); or so...


This is not homework, it's a hobby. And does pow handle negative
exponents?


If it's a hobby I seriously suggest a good C++ book like "Accelerated
C++". And a few good books on general programming.

Yes, pow() handles negative exponents as well as fractions as exponent.

Cheers,
Andre

Dec 20 '05 #11
Good. I'll use that. No sense in reinventing the square wheel.

Dec 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Usman | last post by:
Huy everyone , Well I am not a big C++ programmer , I am just a little young kid on it tryint to learn . Actually I was given an assignment last week by my teacher which I solved ...
9
by: Joseph Aldred | last post by:
I am new to C and I am writing a program for a class I am taking. I need to use a exponent but I can not figure how to do it. I figure I need to use exp() or pow() but I can not figure out how to...
3
by: JackRazz | last post by:
I'm trying to call a Managed C++ library function from VB and cannot get it to return the exponent paramater in Frexp(double floatValue, int* exponent). But I'm getting the following compile error...
3
by: Daylor | last post by:
hi. i got a sample project written in vc6. when i compile and build the exe (debug mode ) , the exe file size is 221KB, and the program work ok. if i open this project in vc7 , build the exe,...
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
3
by: Soneji | last post by:
Hello again! Ok, I wrote the following program ( just messing around ), to find the exponent of a user's number. Here's the problem: If I place the less-than symbol ( < ) in the 'for' loop,...
0
by: Dipanwita | last post by:
I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation . These...
8
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.