Have a look at my code, and compare it to yours. Instead of using cin >> i I am using a more robust method. So if someone enters some invalid input you don't end up in an infinite loop.
If so, You are not understanding the differences between the code. In which case I'd suggest you re-read through tutorials on Functions and If Statements. Then have a read through C++ IO (IOStreams).
Your trying to do something that is currently our of your knowledge level, and if you do accomplish it I feel it'll be by trial and error (and luck) more than learning to solve the problem.