"My cat sleeps under the sofa."
Translation:Meine Katze schläft unter dem Sofa.
when is dem used
Here, check this link out for a really good explanation as to the various German cases: http://www.jabbalab.com/blog/795/how-the-german-cases-work-nominative-accusative-dative-and-genitive
Yeah that's grammatical. It's like saying "Under the sofa my cat sleeps".