Does a police officer have to tell you he's a cop if you ask?
No. This is perfectly legal in most jurisdictions. Your question repeats one that is often asked, as people for some reason assume that dishonesty by a police officer should always lead to a dismissal. It is only police tactics that could deprive you of some constitutional right that will lead to suppression of evidence and a subsequent dismissal.
You didn’t use the word, but your question is centered around the legal doctrine of “entrapment,” but this term does NOT mean simply that you were tricked into getting caught. Entrapment means that the police persuaded you to commit a crime you had no intention of committing at the outset. In your case, you intended to buy the drugs all along. You just didn’t intend to get caught. There is no Constitutional right not to get caught committing a crime. What the police did was only to provide you with the “opportunity” to buy drugs, not to force you or convince you to do so.
If you had been approached by an undercover cop or civilian agent of the police and offered drugs, and you refused, but he came back repeatedly and urged you to take some, or used some sort of pressure or threats, then this might invalidate the arrest and result in the charges being dismissed. The reason behind this is that the law does not want police creating crime where there was none, in order to make an arrest.
Who initiated the criminal transaction - the cop or you?
That an officer lies and denies being a cop does not put the initial idea in your mind to buy drugs. Nor does this strategy “urge” you or “pressure” you to buy. He just makes you think that you can get away with it. This strategy is permitted all over the country.
Look at it this way: if federal agents from A.T.F. (Alcohol, Tobacco and Firearms) were conducting a sting operation to sell machine guns to terrorists, do you think the terrorists should go free because the feds did not disclose that they were cops? I didn’t think so.