-4

Write the definition of a function that takes as input the three numbers. The function returns true if the first number to the power of the second number equals the third number; otherwise, it returns false. (Assume that the three numbers are of type double.)

The program runs and executes but after everything is done its only returning 0 and not true or false. Where could i be wrong please. thank you in advance. this is my code so far:

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

using namespace std;

class powers
{
    private:
        double num1;
        double num2;
        double num3;

    public:
        bool takeInput(double, double, double);
};

int main()
{
    powers power;

    double a;
    double b;
    double c;

    cout << "please input first number: ";
    cin >> a;

    cout << "please input second number: ";
    cin >> b;

    cout << "please input third number: ";
    cin >> c;

    power.takeInput(a, b, c);
}

bool powers::takeInput (double num1, double num2, double num3)
{
    double a;
    double b;
    double c;

    num1 = a;
    num2 = b;
    num3 = c;

    if (pow(a, b) == (c))
        return true;
    else
        return false;
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • What do you mean by "the program is only returning 0"? Do you mean the status code (which is an indication of the successful execution, not of any result)? Do you want to print "true" or "false"? – Pierre Jul 12 '17 at 07:23
  • Read [ask]. Stackoverflow is neither a help desk nor a forum. – dandan78 Jul 12 '17 at 07:24
  • a) `main` cannot return `true` or `false`, it returns an `int` b) you dont return anything from `main`, so the compiler will add a `return 0;` at the end of your `main` – 463035818_is_not_an_ai Jul 12 '17 at 07:24
  • 1
    Stack Overflow isn't a place to get your homework done. Extract the essential from the problem. – Top Sekret Jul 12 '17 at 07:26
  • @Pierre yes i meant to say that, its only showing the status code, i would like it to print true or false – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:34
  • @tobi303 thanks for that note, let me try and edit my code. – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:37
  • pointing you to relevant sites that explain how to write a better question is helping. Telling people not to comment on the other hand is counterproductive – 463035818_is_not_an_ai Jul 12 '17 at 07:38
  • @JakubKaszycki this is not homework, am just try to program on my own, if u cant help please reserve your comments, i need to build my skill not to be demoralized – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:39
  • 1
    you wont get far by only trial and error and asking on SO. I suggest you to get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Jul 12 '17 at 07:41
  • @tobi303 yes i know, but am new to all this, i just recently found this site and then he goes like "stackoverflow is neither a help desk nor a forum" like am here to look only for an answer? that wasnt very welcoming – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:43
  • You should just fix your question title and content a bit (especially title), so they reflect issue better and not sound like "plz halp!". – user7860670 Jul 12 '17 at 07:45
  • i am using C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN, FIFTH EDITION, D.S. MALIK – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:45
  • @PatrickPat-WizyMulenga you are not the first one to get a negative first impression, but thats not because you arent welcome here. To my experience SO is very open to new users. The problem is just that, well, SO isnt a forum or a help desk ;) but question are expected to be about a very specific issue and not just "THis is my code, please help to fix it". And this is not just pure arrogance, but such question wont be useful for anybody but the OP and thats not what SO is about. – 463035818_is_not_an_ai Jul 12 '17 at 07:47
  • @vtt thank you, i will improve on that, this is my first activity on this site – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:47
  • @tobi thanks i will work on that, i guess i just didnt know how you guys roll here – Patrick Pat-Wizy Mulenga Jul 12 '17 at 07:49

2 Answers2

0

Your program will not magically print anything by itself. To print "true" or "false" to the standard output, you would have to call your function power.takeInput like this at the end of main.

cout << power.takeInput(a, b, c) ? "true\n" : "false\n";

As additional notes:

  • The code of powers::takeInput seems to be unnecessary complicated. What is the purpose of your variables a, b and c if you end up assigning to them the values of num1, num2 and num3? Why not use these values directly?
  • if (a) then return true else return false is equivalent to return (a). – Indeed, the return code of the main function is always an integer, which is used exclusively to indicate the successful execution of a program. 0 means successful execution.
Pierre
  • 6,084
  • 5
  • 32
  • 52
-1

You never initialize variables a, b, c inside of takeInput. I guess num1 = a; should be a = num1; and so on. Also notice how power class fields num1, num2, num3 are not initialized as well and have the same names as takeInput parameters which may lead to more confusion. You should either name member fields with m_ prefix or access them using explicitly, like this->num1. Also if you have variables with names like x1, x2 ... xn it would be better to use array instead.

user7860670
  • 35,849
  • 4
  • 58
  • 84