Here's the problem (i know it's really easy "it's for beginners")

You will be given two numbers X and Y and you will have to define the relation between them. It can be one of the following relations :

  • X greater than Y.

  • X smaller than Y.

  • X equals Y.

and that's my solution

include <iostream>

using namespace std;

int main()
{
    int X;
    int Y;

        cin >> X;
        cin >> Y;
if ( X> -10^12 && Y<10^12) {

       if (X>Y)
        cout << ">" <<endl;
       else if (X<Y)
        cout << "<" << endl;
        else if (X=Y)
            cout << "=" << endl;
}
   return 0;
}

now what's wrong? it can't be accepted on Codeforces

#include <iostream>

int main()
{
    int x ;
    std::cin >> x ; // if input fails, x would be set to zero

    int y = 0 ;
    std::cin >> y ; // do nothing if std::cin is in a failed state

    // there is no need to check that x and y are valid integers

    char relation = '=' ;
    if( x < y ) relation = '<' ;
    else if( y < x ) relation = '>' ;

    std::cout << x << ' ' << relation << ' ' << y << '\n' ;
}

vijayan121

sorry i forgot to post the input & output info

Input
The input consists of two numbers X and Y ( - 10^12 <x, y< 10^12).

Output
Print one line containing the right relational operator ">", "<", "=" without quotes.

Member Avatar for ArashVenus

Hello Aida,
first thing you must to when you write the code is to use #include <iostream> , you forgot the # sharp sign here ,
second thing that is wrong with this code is initialization (Persian : تعریف اولیه)

int X=0 , Y=0;

or

int X{0} , Y{0};

if you don't initialize your variables, you might end up having false values at output .

C++ doesn't recognize operator ^ , if you like to do power calculation , you can use c++11 <math.h> library , then use function pow (10 , 12) to calculate 10^12

third thing thats wrong with this codes are the indents , get a Visual Studio Express 2013 for Win Desktop and It'll fix It for you .

now the code
here's what I suggest :

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    long int x{ 0 }, y{ 0 };
    double num1{ 0 };
    num1 = pow (10.0 , 12.0);
    cout << "Please enter first number then press enter : ";
    cin >> x;
    cout << "Please enter second number then press enter : ";
    cin >> y;
    if (x > -num1, y < num1)
    {
        if (x>y) cout << "First number is bigger than second number";
        else if (x<y) cout << "First number is smaller than second number";
        else if (x = y) cout << "Numbers are equal";
    }
    return 0;
}

this code initializes x & y values , defines variable num1(type double) and puts the result of 10^12 into It , prints some stuff on console and requests for inputs , compares the entered values with num1 and -num1 (10^12 and -10^12) then prints the results .

else if (X=Y)

In C++, = is the assignment operator. X=Y means "make X have the same value as Y".

The operator you want here is ==.

commented: yep , you're right , I made a mistake +1

The input consists of two numbers X and Y ( - 10^12 <x, y< 10^12).

Well, then the essence of this exercise is to ask the student to determine and use a suitable signed integer type that can hold integers having 13 decimal digits.
(The types int or long int may be inadequate.)

#include <iostream>

int main()
{
    constexpr auto upper_bound = 1'000'000'000'000 ; // C++14 literal
    // constexpr auto upper_bound = 1000000000000 ; // C++11

    constexpr auto lower_bound = -1'000'000'000'000 ;

    // integer_type is an alias for a signed integer type
    // which can hold integers with 13 decimal digits
    // http://www.stroustrup.com/C++11FAQ.html#decltype
    using integer_type = decltype(9'999'999'999'999) ;

    integer_type x = 0 ;
    std::cin >> x ; // if input fails, x would be set to zero
    integer_type y = 0 ;
    std::cin >> y ; // do nothing if std::cin is in a failed state

    if( x > lower_bound && y < upper_bound )
    {
        char relation = '=' ;
        if( x < y ) relation = '<' ;
        else if( y < x ) relation = '>' ;
        std::cout << x << ' ' << relation << ' ' << y << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/38a38ef80b3c857c

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.