Your result (temp) keeps getting overwritten with each loop, so what will happen is temp will really only be set by the last comparison (3 versus 7 in this case). You might want to initialize temp...