Please you the code format tag on the right to format your code. It's almost impossible to understand the code as posted. You can simple edit the post for format it, you don't need to re-post it.
You can help yourself by using the C++ iostream libarary (the cin/cout stuff) for handle your input/output. scanf is error prone.