Improving C++ code
Posted: Tue Sep 09, 2003 8:24 am
I've got this in my code for a program
I know it's long, but it was my last resort. I've tried everything I know, checked my tutorials at least five times, tried fiddling with commands I didn't know up till then, fiddled with old commands, tried making different calculations, some simple, some overly complicated, but it never did what it had to do.
Now I've come to the conclusion that the easiest way out is checking if a given value, inputted by the user (here stored in home.longi) is positioned between two set values. If it isn't, it should go on to the next two values, check if it's positioned there, etc. I tried some more tricks for it (thought I came to a solution using an array and a for-loop, but that seems to be completely the wrong way as well), so this's the only working code I managed to make. I know there must be an easier way to get this done, without all those ifs, but can't find it.
So, my question is: how can I improve this code so that it won't be so long and still do the same?
Thanks.
Edit: Forgot to say this: it should do it with target.longi on target.zone too, if that might help.
Code: Select all
if(home.longi >= 0 && home.longi < 7.5) {
home.zone = 0;
}
if(home.longi >= 7.5 && home.longi < 22.5) {
home.zone = 1;
}
if(home.longi >= 22.5 && home.longi < 37.5) {
home.zone = 2;
}
if(home.longi >= 37.5 && home.longi < 52.5) {
home.zone = 3;
}
if(home.longi >= 52.5 && home.longi < 67.5) {
home.zone = 4;
}
if(home.longi >= 67.5 && home.longi < 82.5) {
home.zone = 5;
}
if(home.longi >= 82.5 && home.longi < 97.5) {
home.zone = 6;
}
if(home.longi >= 97.5 && home.longi < 112.5) {
home.zone = 7;
}
if(home.longi >= 112.5 && home.longi < 127.5) {
home.zone = 8;
}
if(home.longi >= 127.5 && home.longi < 142.5) {
home.zone = 9;
}
if(home.longi >= 142.5 && home.longi < 157.5) {
home.zone = 10;
}
if(home.longi >= 157.5 && home.longi < 172.5) {
home.zone = 11;
}
if(home.longi >= 172.5) {
home.zone = 12;
}
Now I've come to the conclusion that the easiest way out is checking if a given value, inputted by the user (here stored in home.longi) is positioned between two set values. If it isn't, it should go on to the next two values, check if it's positioned there, etc. I tried some more tricks for it (thought I came to a solution using an array and a for-loop, but that seems to be completely the wrong way as well), so this's the only working code I managed to make. I know there must be an easier way to get this done, without all those ifs, but can't find it.
So, my question is: how can I improve this code so that it won't be so long and still do the same?
Thanks.
Edit: Forgot to say this: it should do it with target.longi on target.zone too, if that might help.