C++ program using LOOPS and Files

Pirate Election

Fifteen men on a dead man's chest.

Yo ho ho and a bottle of rum.

Drink and the devil have done for the rest.

Yo ho ho and a bottle of rum.1

Shiver me timbers, the ship’s captain is swimmin’ in Davey Jone’s Locker! He was infamous for being a lush, gambling too much, and cussing like—well—a sailor. During a recent raid, he got caught by locals for hornswaggling2 and was strung up by a hempen halter3.

The Jolly Roger is flying at half-mast until a new captain is picked. That means no raids which has the crew in the grips of seafartherin’ chaos. No one knows what to do so you’ve suggested an election. It’s been generally agreed to with some ground rules, pirates not being the most upright of citizens. While the fuss and muss of an election is inevitable, the crew has also decided to vote on amendments on two long standing pirate laws on the ballot4. That way, they can resolve these issues and elect a new captain and first mate with one ballot.

Law 1: A pirate does not ask for directions. He relies only on his gut feeling, a compass, or a treasure map.

Amendment 1: GPS be added to the list with the restriction that iPhones, iPads or other iTechnology that comes up must use Google maps5. By Rob’s Rules of Order, this amendment requires 75% of the pirates to vote yes.

Law 2: Hooks are the only acceptable hand substitute.

Amendment 2: Secondary attachments such as screwdrivers, bottle openers, or corkscrews are permitted. This amendment only requires a majority (> 50%).

All of the pirate’s names have been tossed in a hat and after many a mate brawled, it comes down to three burly candidates. Their names are stored in a file called candidates.txt. You need to open this file and read in the names, stored as strings on three separate lines.

1 Treasure Island.

2 To cheat or defraud, often of money or belongings.

3 The hangman's noose.

4 Pirates never, ever obey laws. Period. Ironic, I realize.

5 Relying on the iPhone map app's directions to get to Blackbeard’s island is downright dangerous.

Example [url removed, login to view] file:

Jack Black Bartelmy

Steve the Pi-rat

Pearly Pete

The voting process proceeded as follows:

 Pirates stand in line to fill out a paper ballot.

 Each pirate has been assigned a unique number that they enter on the ballot.

 They write down the name of the candidate they are voting for.

 They enter Y or N for issue 1 and Y or N for issue 2.

 The pirate with the most votes becomes the new ship’s captain.

 The pirate with the second most votes becomes the new first mate.

 The pirate with the least votes walks the plank6.


Pirates are neither honest nor very bright. They tend to get back in line and vote again with the “vote early and vote often” philosophy. They are not, however, smart enough to use a different number. Your file will contain a sorted list of votes so that you can discount duplicates. You only need to check the current pirate number with the last one to see if that pirate has already cast a ballot. Only tally the results of the first ballot for each pirate. Ignore duplicates even if they voted differently the second or third or fourth time. Note that not all numbers may be used, i.e., pirate registration numbers may be 5, 13, 21, and 34. Therefore you’ll need to keep track of how many pirates actually voted. You cannot rely on the last pirate registration number.

Ballots are stored in a file called ballots.txt. You need to open this file and read each ballot (one per line) until the end of the file.

Example [url removed, login to view] file:

1YYSteve the Pi-rat

2YNJack Black Bartelmy

2YNJack Black Bartelmy

2NNPearly Pete

3NYSteve the Pi-rat

3YYJack Black Bartelmy

7YNSteve the Pi-rat


6 This encourages only serious candidates to apply. Unfortunately it also encourages a lot of cheating. And fighting.

Results would be:

4 pirates voted in this election using 8 ballots.

Captain (most votes): Steve the Pi-rat (with 3 votes)

First mate: Jack Black Bartelmy (with 1 vote)

Pearly Pete did not receive any votes because pirate #2’s first ballot for Jack Black Bartelmy is the one you count. He walks the plank. (Don’t worry, like most pirates, he can swim.)

Amendment #1 passes (3 Y, 1 N) – 75% met

Amendment #2 fails (2 Y 2 N) – 50% is not a majority

Your task is two-fold. First, determine the results of the election and print them out on the screen. Second, as you read in the ballots, create a new ballot file called [url removed, login to view] that contains only those ballots that were used in the count. Add a space between the ballot entries. For example, with the sample file given, the new file would look like the following.

1 Y Y Steve the Pi-rat

2 Y N Jack Black Bartelmy

3 N Y Steve the Pi-rat

7 Y N Steve the Pi-rat

Voting ties:

It’s possible that there could be a tie for first or second place. This has been anticipated. The three candidates participated in a rat throwing contest7. Their names are placed in the candidate file according to their hurling skills. Therefore, if there is a tie, the pirate whose name appears first in the file breaks the tie. For example, using the example file, had there been a tie between Jack Black Bartelmy and Pearly Pete, the winner would be Jack Black Bartelmy.


1. Candidate name list and how many votes each received.

2. How many votes were cast. (This includes the cheat ballots.)

3. How many actual pirates voted.

4. If each issue passed or failed and the number of yes votes it received.

5. Who the new captain and first mate are.

6. Give an “Aye! Aye! Captain name of winner!” for each vote the winner received. ex. if Steve the Pi-rat won with 3 votes, end your program with

Aye! Aye! Captain Steve the Pi-rat!

Aye! Aye! Captain Steve the Pi-rat!

Aye! Aye! Captain Steve the Pi-rat!

7 Only the dead ones from the last heat wave.


1. You are opening two files for input and one file for output.

2. You need a while or do-while loop to read in the ballots.

3. You need to keep track of the most recent pirate number and compare it to the current ballot to determine if you are going to do anything with the ballot or not.

4. You need to tally the votes for each pirate candidate and for each issue.

5. For the captain and first mate election, you only need to know who got the most votes and who came in second.

6. For the issues, you need to know how many pirates actually voted to determine 75% or majority.

7. You will use the actual file names ([url removed, login to view], [url removed, login to view] and [url removed, login to view]) in your code. Do not prompt the user for file names.

8. If a pirate receives one vote, say “… received 1 vote” not “… received 1 votes”.

9. Use a for-loop for your final “aye aye”s.


 Use a header comment with your name and the purpose of the program.

 Use appropriate code comments.

 Use appropriate variable names.

 Use good coding practices (i.e., spacing, indentation, etc.)

 Do not use functions, classes, arrays, or other techniques not covered through chapter 5 of Gaddis.


Habilidades: Programação C++

Veja mais: while loop c programming, which numbers have the most arrays, what to program, what is a variable in programming, what is a loop in programming, what does purpose means, what are classes in programming, strings in c programming, steve black, smart input, smart and final, programming tips, programming techniques, programming program, programming loops, programming from the ground up, print all attachments, need of tally, map cplusplus, loops programming, loops in programming, in the swim, in line program, how to get money with programming, how to get good at programming

Acerca do Empregador:
( 2 comentários ) Stow, United States

ID do Projeto: #5085082

Concedido a:


Hello , I am C++ expert and can surely help you here with this project. Please let me know if you are interested. Thank You

$98 USD em 2 dias
(109 Comentários)

2 freelancers estão ofertando em média $137 para esse trabalho


A proposal has not yet been provided

$175 USD in 15 dias
(1 Comentário)