when you are using IR to dectect obstacles or anything in its way. how do i find out how far the robot is from the object.

Well, you can only find out the distance if you have something that can measure distance! This can be done with IR, using some of the Sharp sensors, or with ultrasonic.