How do you write the code to allow the robot to switch back and forth between autonomous and teleoperated mode. Please help!

The provided framework code already switches back and forth between Teleoperated and Autonomous for you. For instance, when the robot is in Autonomous, the Basic Robot Framework executes the Autonomous automatically. During the TeleOp portion of the FRC competition, the TeleOp Execute case of the Case structure executes.

Look in the FRC Programming Guide.pdf for more details. See pg 4-4 (pdf p.38).