Okay, its been a while since my last visit here..and yes I need your help with C++ Programming...
My grades were fine so that's all thanks to you guys....so here's my next question....
What is wrong with my code? My exponent displays a wrong number...
I used a for loop to calculate the power of a certain number cause using pow() is prohibited....I experimented on my loop and finally was able to arrive at this answer...So, whats wrong with it?

#include<iostream.h>
#include<conio.h>

int b, e, result=1;

void bande()
{
 cout<< "Enter Base number:";
 cin>>b;
 cout<< "Enter Exponent:";
 cin>>e;
 clrscr();
}
void compt()
{
 for(;e--;result*=b);
}
void display()
{
 cout<< "Base "<<b<<"\n";
 cout<< "Exponent "<<e<<"\n";
 cout<< "Power: "<<result<<"\n";
}
void main()
{
 bande();
 compt();
 display();

}

Recommended Answers

All 8 Replies

First off, while it isn't relevant to your particular issue, I would recommend against using void main(). According to the C++ standard, the only valid return type for main() is int, and while some compilers do accept other return types, it is strictly non-portable.

Speaking of non-portable, the <conio.h> library is specific to older MS-DOS compilers, and while some newer compilers have versions of it, they are not by any means consistent with each other. In any case, there's no real need to clear the screen, unless you were specifically instructed to by your instructor. If you eliminate the conio.h> header and the clrscr() function, you'd have portable code.

Sort of, that is... except for one more minor issue. The newer C++ standard (as of 1998, that is) changed the headers, so that <iostream.h> should now be just <iostream>. It also added a system of namespaces, which means you'll need a scope qualifier. The easiest way to have that is to add the line

using namespace std;

just after the header includes. A better way would be to explicitly scope each reference to a class or object in the std namespace, like so:

void bande()
{
    std::cout<< "Enter Base number:";
    std::cin>>b;
    std::cout<< "Enter Exponent:";
    std::cin>>e;
}

While it is more work, it gives you better control over the namespaces.

Which compiler are you using, BTW? If you are using the Turbo C++ compiler, I would strongly recommend getting rid of it; it is more than 20 years out of date, and is specific to MS-DOS. The newest versions of Windows won't even run it properly, so it has definitely gone passed it's normal lifespan. I would recommend either Visual C++ or Code::Blocks instead, if you have any choice in the matter.

OK, let's finally address the specific problem you posted for (about time!). What's happening is that in your compt() function, you are working on, and modifying, the global values. When you write e--, you change the same e that you then are looking to print out later. Thus, e will always come out as zero, because that is the end mark for the for() loop you've written.

It is generally better to avoid global variables whenever possible, and you've just seen one of the reasons why. Another is what is called modularity, which means that the different parts of a program can be used independent of each other. So what would a mor modular approach for the compt() function be? We could pass the arguments to the function, and then have it give a result. You would need to re-write main() as well, but you'll get better results there, too. In fact, let's get rid of the global variables entirely, and use function parameters instead:

#include <iostream>

void bande(int& b, int& e);
int compt(int b, int e);
void display(int b, int e, int result);

void bande(int& b, int& e)
{
    std::cout<< "Enter Base number:";
    std::cin>>b;
    std::cout<< "Enter Exponent:";
    std::cin>>e;
}

int compt(int b, int e)
{
    int result = 1;

    for(; e--; result *= b);

    return result;
}

void display(int b, int e, int result)
{
    std::cout<< "Base "<<b<<"\n";
    std::cout<< "Exponent "<<e<<"\n";
    std::cout<< "Power: "<<result<<"\n";
}

int main()
{
    int b, e, result;

    bande(b, e);
    result = compt(b, e);
    display(b, e, result);
}

Note the use of reference parameters in bande() to allow you to change the actual variables in main().

If you have any questions about this, feel free to ask.

hehe..im using Turbo C++..damn my school is so out of date....Well thanks alot sir!!! My instructor told us that we will use visual c++ though but he wants us to practice in turbo c++..i dont know why....maybe theres just little changes made in visual c++? ......whooo! thank you sir...thank you!

Not little changes, no; enormous ones. The entire C++ language was overhauled in the 1998 ISO standard, and again last year in the 2011 ISO revised standard. The big change in the 1998 version was the introduction of namespaces, which was what prompted the changes in the standard headers (the old headers were, for a while, allowed for the sake of backwards compatibility in older code that didn't use the namespace system, but have been phased out since then).

This is just in the standard C++ language itself. Visual C++ has a whole raftload of proprietary additions to C++, as well, and if your professor teaches those, well, they are worh knowing about, but you ought to understand that they aren't really part of the language as it is used by the rest of the world.

this should work for large amount of data:

long double pow(long double nr, long double exp){
    long double temp=1;
    for (;exp--;) temp*=nr;
    return (temp);
}

for like 23^53=1.48448e+072.

void bande(int& b, int& e);
i forgot to ask what does & mean???

It's called that that function passes parameters by reference:
Read more about this here.

In this context, the ampersand is the reference type indicator; it means that instead of passing the value of the arguments, it should pass a reference to them. A reference is similar to a pointer, in that it let's you act on the original variable rather than a copy.

In practical terms, this means that when you change the values of b and e in the function bande(), it also changes the values of the b and e in the main() function.

woosh..i learned something new again...thanks guys!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.