When should titles like "captain" or "admiral" be capitalized?

Please excuse the contrived example below, but I am wondering if this example is properly capitalised? I'm assuming that capitals are used when addressing someone directly, but lower case is used elsewhere. Can someone give some rules on this?

The captain walked into the room.
"Hi, Captain," said the admiral.
"Hi, Sir," replied the captain.
"Call me Admiral."
"Hi Admiral."
The captain and admiral stepped forward.
"Prime Minister, this is Captain Jones," said Admiral Brown.
"Pleased to meet you," said the prime minister.


It looks right to me. The rules of capitalisation (based on the Chicago Manual of Style) are explained quite well on this page. One of the rules relevant to this question is as follows:

Rule 3

Capitalize a person's title when it precedes the name. Do not capitalize when the title is acting as a description following the name.

Examples:

Chairperson Petrov

Ms. Petrov, the chairperson of the company, will address us at noon.


At least according to CMOS, there are a few rules that apply. (Your mileage may vary with AP or Oxford rules, I just happen to have CMOS within grabbing distance.)

7.16 says that titles used in place of names in direct address are capitalized. (e.g., "'Hi, Captain'")

7.17 says that titles used alone in place of a name are generally lowercased (e.g., "replied the captain").

(Edit: Cited the wrong rule.)


That is correct. Titles are capitalized only when they appear with a name or when they are used to address. Otherwise, when the word stand alone, it is not capitalized.

Usually, when the titles is local only to a group or an organization, it is NOT capitalized, eg:"the disaster management head, Mr. X"....


Like Coleopterist said, the capitalization is good, the formality is a bit off though. If you were trying to have a conversation with a Captain or an Admiral it would be a bit more formal like, Good evening or Good Afternoon, unless they are good friends. I understand this is really off topic and that you said that you tried to make it look artificial but formality is also important.