0

I am a newbie at C++ and I got really stuck at this problem: When the user enters 2 numbers EX: 1 and 2 than the code has to figure out if the first number is grater or not from the first one, the problem is that the code doesn't bring the true or false as text it brings it as numbers :/ (0= false 1=true)

Code here:

#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

bool GraterFunct(int num1, int num2);

int main(int argc, char** argv)

{
    std::cout <<" \n Hello there! This is a test to test how good you are with math. \n ";
    std::cout <<" Now enter G or L (Grater, less) to know if a number is grater or less than the number you choose \n ";
    char answer [1];
    std::cin >> answer;

    if(answer == "G" || "g")
    {
        int number1;
        int number2;
        std::cout << "You selected: Grater than, \n";
        std::cout << "Now type 2 numbers and see which one is grater than the other one. \n" << std::endl;
        std::cin >> number1;
        std::cout << "Your first number: " << number1 << std::endl;
        std::cout << "Select your second number \n";
        std::cin >> number2;
        std::cout << "The answer is: " << GraterFunct(number1, number2);
    }

    return 0;
}

bool GraterFunct(int num1, int num2)
{
    if(num1 >= num2)
    {
        {
            return true;
        }
    }
    else
    {
        if(num2 >= num1)
        {
            return false;
        }
    }
}

Please help! Thanks in advance!

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Lolechi
  • 151
  • 1
  • 3
  • 20

1 Answers1

1

To format Boolean values as true and false you can set the std::ios_base::boolalpha flag using the std::boolalpha manipulator:

std::cout << std::boolalpha << "true=" << true << " false=" << false << '\n';

In case you are a non-native English speaker like me, you might want to change the formatting of these values. Assuming there are suitable locales installed you can just imbue() into a stream or you can create you own locale with whatever rendering of true and false you want, e.g.:

#include <iostream>
#include <locale>

class numpunct
    : public std::numpunct<char>
{
    std::string do_truename() const { return "wahr"; }
    std::string do_falsename() const { return "falsch"; }
};

int main()
{
    std::cout.imbue(std::locale(std::locale(), new numpunct));
    std::cout << std::boolalpha << "true=" << true << " false=" << false << '\n';
}

BTW, you always need to verify that you input was successful, e.g.:

if (std::cin >> number1) {
    // deal with the successful input here
}
else {
    // deal with the wrong input here
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • If the purpose of the program is educational, input verification would obscure the actual code though. – riv Oct 02 '13 at 20:12
  • @riv: If people don't learn how to properly read data, they will never check! Not checking in example code is a Bad Idea: I see too much production code not checking successful input. – Dietmar Kühl Oct 02 '13 at 20:13
  • Hey guys thanks for all the advice I can now make the function return text! – Lolechi Oct 03 '13 at 15:35