1
\$\begingroup\$

I'm trying to make a simulation of Travelling sales man problem. So I got random circles spawning and each circle.

I want to make it so I can switch between the circles. Currently the player spawns to the first circle created which is in position (cir[0].x, cir[0].y). However, I'm trying to make an input so you can type in for example 12 and the position of the queen would now be (cir[12].x, cir[12].y). But, this seems to not work.

int number;
cin>>number;

if(number<NUMCIRC)
{
    switch(number)
    {
    case number: 
        queenX = cir[number].x;
        queenY= cir[number].y;
        break;

    default: break;

    }
}   

I was wondering as surely if I input the number and then press enter it should jump to the number. Assuming it's smaller than NUMCIRC, which is the number of circles that I have spawned.

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Your switch statement is completely superfluous because you're not switching between anything. The point of an array is you can select a value by its numerical index. I'm not entirely sure how the switch statement will execute with a variable case. Regardless, you can achieve the desired result with the following code.

int number; 
cin>>number; 

if(number<NUMCIRC) 
{ 
    queenX = cir[number].x; 
    queenY = cir[number].y; 
}    
\$\endgroup\$
1
  • 1
    \$\begingroup\$ case labels must be Integral Constant Expressions. That's "more constant" than a regular const; the compiler has to know the values. But this is indeed the correct solution; switch is in no way related to the problem. \$\endgroup\$
    – MSalters
    Commented Jul 31, 2012 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .