# Tiresome GCD CPP - default code is wrong

Hey guys. so on tiresome GCD for c++.
The beginning portion of the code keeps giving me an error… I am either blind and missing a simple mistake or the level is bugged. I haven’t changed anything on it but the bottom half of the code that is suppose to be changed. The error marker shows up at the first “IF” statement. Yet, I don’t see anything wrong

``````// Calculate the secret number and get into the Treasury.
// Those two guys know keys for the password.
var friends = hero.findFriends();
auto number1 = friends[0].secretNumber;
auto number2 = friends[1].secretNumber;
// Just to be sure that the first number is greater.
if (number2 > number1) {
auto swap = number1;
number1 = number2;
number2 = swap;
}

// It's simple but slow function to find gcd.
auto bruteforceGCD (auto a, auto b) {
hero.say("The naive algorithm.");
auto cycles = 0;
// We enumerate all possible divisors.
auto counter = b;
while (true) {
cycles++;
if (cycles > 100) {
hero.say("Calculating is hard. I'm tired.");
break;
}
// If both number have "counter" divisor.
if (a % counter == 0 && b % counter == 0) {
break;
}
counter--;
}
hero.say("I used " + cycles + " cycles");
return counter;
}
auto euclidianGCD (auto a, auto b) {
auto cycles = 0;
while (b) {
cycles++;
auto swap = b;
b = a % b;
a = swap;
}
hero.say("I used " + cycles + " cycles");
return a;
}
int main() {

// Maybe you need to use another function?
auto secretNumber = euclidianGCD(number1, number2); // ∆
hero.moveXY(48, 34);
hero.say(secretNumber);
// The treasury is open (I hope so)! Go there!
hero.moveXY(68, 34);

return 0;
}

``````

I still need help with this. Resetting code/ reloading level does not fix it. I won’t even touch anything and I instantly get this error.

As i remember code cannot be written outside of a function, you can only declare variables/functions etc…
so put this code inside the main() function, the rest is the same:

``````int main() {
auto friends = hero.findFriends();
int number1 = friends[0].secretNumber;
int number2 = friends[1].secretNumber;
if (number2 > number1) {
int swap = number1;
number1 = number2;
number2 = swap;
}
// code
}
``````

2 Likes

This one made me feel extra dumb because I came back to it multiple times. I can’t believe I missed something that simple. thanks again.