Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Native Development

Reply
Contributor
developerUK
Posts: 35
Registered: ‎11-21-2012
My Carrier: o2
Accepted Solution

Weird int problem

I am trying to work out the percentage of file loaded and instead of getting a value between 0 and 100 im getting values like -33000 - can somebody help to find the reason why please?

 

bytesRead = (int)infile.gcount();
		
		if(bytesRead > 0){
		
			completeBytesRead += bytesRead;
			per = (((double)completeBytesRead)/((double)fileSize))*100;
			percent = per;
			printf("file percent %d \n", percent);
			fflush( stdout );
			dialog_set_progress_toast_level(dialog, percent);

 thank you for any help you can provide.

Please use plain text.
Developer
Zmey
Posts: 915
Registered: ‎12-18-2012

Re: Weird int problem

[ Edited ]

Just a guess - is completeBytesRead initially initialized to zero?

 

How do you initialize fileSize?

 

 

Btw, for better readability this line

 

per = (((double)completeBytesRead)/((double)fileSize))*100;

can be rewritten as:

per = (double)completeBytesRead / fileSize * 100;

 

'/' and '*' have the same priority, so this line will be processed left to right.

fileSize will be promoted to double automatically.

 

Please use plain text.
Contributor
developerUK
Posts: 35
Registered: ‎11-21-2012
My Carrier: o2

Re: Weird int problem

I init completeBytesRead like...

 

int completeBytesRead, bytesRead, percent = 0;

 

and I init fileSize like..

 

fileSize = filestatus.st_size;

 

is this wrong? cant load to a device for an hour or so to try any different.

 

Thanks for the tips on brackets.

 

 

Please use plain text.
Developer
Zmey
Posts: 915
Registered: ‎12-18-2012

Re: Weird int problem

int completeBytesRead, bytesRead, percent = 0;

this is equivalent to:

int completeBytesRead; // uninitialized
int bytesRead; // uninitialized
int percent = 0;

This should fix it:
int completeBytesRead = 0, bytesRead = 0, percent = 0;
Please use plain text.
Contributor
developerUK
Posts: 35
Registered: ‎11-21-2012
My Carrier: o2

Re: Weird int problem

it certainly did fix it - thank you.

 

regards.

Please use plain text.