#Need help on a simple problem

13 messages · Page 1 of 1 (latest)

vague oyster
#

I just started learning c++ 2 days ago and right now i'm writing a program which find an int(i) such that i+x = y or i + y = x ( i know that i can just use int i = x - y but i want to practise my code soooo)

true anchorBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vague oyster
#

here's my code

#include <iostream>
int main() {
    int x,y;
    std::cin >> x >> y;
    if (x > y) {
        for (int i = 0; i < y; i++) {
            if (i + x == y) {
                std::cout << x << i;
            }
        }
    }
    else {
        for (int i = 0;i < x; i++) {
            if (i + y == x) {
                std::cout << y << i;
            }
        }
    }
}
#

it doesn't show any error but it doesn't give output either

vocal jetty
#

the conditions will never be true ig

sharp adder
#

well, that's likely because your code isn't actually reaching a cout statement. Have you tried running it in your debugger?

vocal jetty
vague oyster
vocal jetty
#

consider x = 2, y = 3.
the first if will not work the else block will be executed
inside the condition of that loop (i + y == x) will never be true as
0 + 3 == 2 can never be true. try doing y - i == x

vague oyster
#

OH

#

ok tysm

vocal jetty
#

👍