Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

C++: Dance off level

Hello!

I’m stuck at this level of the game. I really don’t understand what’s wrong in my code?`

int main()
{

auto friend = hero.findNearest(hero.findFriends());
auto diffx = friend.pos.x - hero.pos.x;
auto diffy = friend.pos.y - hero.pos.y;
while (true)
{
auto target = {“x”: friend.pos.x - diffx , “y”: friend.pos.y - diffy} ;
hero.move(target);
}
return 0;
}

I am getting this error for the line 10

“Expected a ; to finish the local variable on line 10 but end of input found”

Thanks in advance for your replies.
`

Hi @DaRkFoX, welcome to the forum. I’m not sure exactly why this is happening. Maybe C++ isn’t supposed to use object literals like that…
An easy solution is just to change this:

To: Vector(friend.pos.x - diffx, friend.pos.y - diffy)
It does exactly the same thing. You’ll use Vectors() more later, but I think this might be a good temporary fix for this.
I’m sorry I couldn’t give a more satisfactory answer, but I don’t actually know that much about C++. The above change did make it work for me though.

1 Like

Thank you for your quick reply,

I have just tried the code which you suggested to me, but i didn’t work for me. I really don’t understand what is the real problem is.

For example,this code needs to work but it doesn’t work

hero.move({“y”:20, “y”:10}) ;

Will this code work for you?

int main() {
auto friend = hero.findNearest(hero.findFriends()),
  diffy = friend.pos.y - hero.pos.y;
  while (true){
    if (friend){
        if (friend.pos.y > hero.pos.y)
            hero.move(Vector(friend.pos.x, friend.pos.y - diffy));
        else
            hero.move(Vector(friend.pos.x, friend.pos.y + diffy));}}  
    return 0;
}

@Deadpool198 is C++ another name for coffee script?

No, they are different languages.

Then how can you do it in coco?

You need to be a sub.

Interesting… I didn’t see that before. Thanks!

Hey there! As Xython and Deadpool mentioned above, please use the Vector(x,y) for this level. We haven’t fully implemented object literals for CPP, so some levels might need a different approach. (Usually you could use {x: xPos, y: yPos} for CPP, but something must’ve been broken). Sorry for the inconvenience!

2 Likes

No coffee script is a cleaner version of Javascript

1 Like

@Xython, @Deadpool and @Chaboi_3000

Thank you all for your replies, code is worked for me like this.

auto target = Vector(friend.pos.x - diffx, friend.pos.y - diffy);

In case if all of you forgot you don’t get Vectors until Summit Gate.

@milton.jinich In case if you don’t know something for sure don’t post…
You do not need to pass Summit Gate or own any book to use Vectors and all the possibilities of the language. They are available from the first to the last level.

@Chaboi_3000 I’m really perplexed if {x: xPos, y: yPos} is available for coco cpp . This isn’t c++!
I think creating a position can be something like this:
image
but same code triggers an error in CoCo:


despite having struct as reserved word.

@DaRkFoX Are you the same user from 7 years ago?
image
I’m really happy to salute you! :slight_smile:

It was originally meant for {x: xPos, y: yPos} to work for CodeCombat’s cpp. It seems to me that it’s still incomplete.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.