writing cout giving the right answer

below in this code i am just incrementing the value of i and checking if %age value of i w.r.t x is an absolute number i.e its decimal part is zero.


int x=(time[0]-48)*10*60*60+(time[1]-48)*60*60+(time[3]-48)*10*60+(time[4]-48)*60+(time[6]-48)*10+(time[7]-48);
cout<<x;
float temp;
int count=0;
for(float i=1;i<x;i++){
temp=(i/x)*100;
cout<<temp<<" ";
if(temp-(int)(temp)==0) count++;
}
return count;

but when i remove the command(cout<<temp<<" ";) this programme fails in system test.please help as i m totally confused as cout is not changing ne variables's value.
foe eg when x=1800 the correct answer is 99 but when i remove that line this prog. gives 92 as answer.

PS:i didn't find any better title sry. :)
Declare temp like this and get back to me: volatile float temp;

I have an explanation for this here: http://www.cplusplus.com/forum/general/8077/
Last edited on
yeah its working that way.now can you tell me whats going on.do this type of declaration of temp force it to reside on memory rather than on FPU?
do i need to declare float every time in this way?
volatile forces the compiler to update the memory location as soon as possible, in this case by pulling it from the FPU (supposedly).
The problem is that your logic is depending on temp having a very specific precision. I would look to change that if to something like temp-int(temp)<epsilon. 'epsilon' being a very small value. Another option would be to use fixed point arithmetic.
that means i need to use volatile whenever i need to keep check on even the minute change in the variable.
Topic archived. No new replies allowed.