Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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; 
}    
share|improve this answer
1  
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. –  MSalters Jul 31 '12 at 13:27
    
@MSalters, thanks for the clarification! –  Hand-E-Food Jul 31 '12 at 23:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.