CSIS 610: Programming and Problem Solving
Programming assignment 6: Strings and Files

Due May 4

This assignment will introduce you to strings and two-dimensional arrays, and give you an introduction to using data files.

For this assignment, you are to create a program which simulates a voting booth. It will repeatedly prompt the user for the name of the candidate they wish to vote for, and increment the number of votes for that candidate. At the end of the program, it will display the current number of votes for each candidate, and tell which has the greatest number of votes.

The names of these candidates will be read from a data file at the beginning of the program. The final vote totals will be written to another file at the end of the program.

For example, a run of the program might look like:


Enter the name of the file containing the candidate names: candidates.txt
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: Curley
Thank you for your vote!
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: Wilma
Thank you for your vote!
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: Curley
Thank you for your vote!
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: Cartman
Thank you for your vote!
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: Lucy
Thank you for your vote!
 
CANDIDATES:
Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl
Please enter the name of the candidate that you wish to vote for: QUIT
 
Total votes for each candidate:
Fred: 0
Wilma: 1
Barney: 0
Betty: 0
Larry: 0
Curley: 2
Moe: 0
Cartman: 1
Kenny: 0
Lucy: 1
Ethyl: 0
The winner is Curley!

String Representation

You will need to create an array of strings (that is, a two-dimensional array of char) in order to store the names of the candidates. You may assume:

  1. There are 11 candidates, the names given in the example above.
  2. No name will be longer than 15 characters.
  3. Each candidate has a single name (that is, no spaces).

Note, however, that these maximums may be changed in the future, so you should make it as easy as possible to do so.

You are to print out this list sequentially as part of the prompt for a vote, and input the candidate that the user wishes to vote for as a string. Keep in mind that in order to print out a name, you will have to print an entire row at a time of the two-dimensional array of char.

Your program should keep prompting for and accepting votes until QUIT is entered for the name of the candidate.

Parallel Arrays and Search

As described in class, you are to represent the name and current number of votes for each candidate as parallel arrays. When the user enters the name of a candidate, your program should search the array of names for that name, and then use the index where the name was found in order to increment the corresponding element of the votes array.

Note that this search will probably involve the strcmp function from the <string.h> library. Recall that to determine whether two strings string1 and string2 are equivalent, you use a condition of the form:

if (strcmp(string1, string2) == 0) ...

Printing the Vote Totals and Winner

This idea of parallel arrays also applies to printing the final vote totals and the winner. You should:

Hint: you may want to keep track of who the current leader is, either during the voting or as you are printing the final totals. Each time another vote is cast, compare the number of votes for that person to the number of votes for the leader. If the number of votes for that person is more, then that person is now the leader.

Input Verification

As before, your program should verify that the candidate name entered by the user is a legal one -- that is, one of the strings in the array. If they do not enter an existing candidate name (or the word QUIT), your program should print an error message informing the user that they have chosen a nonexistant candidate:

Please enter the name of the candidate that you wish to vote for: Shemp
No such candidate!

This should also be done as part of the search describer above -- if the search does not find the name, then this message should be printed.

File Access

As mentioned above, the names of the candidates will be read from a file. You may assume that each name is on a separate line -- that is, the file looks something like this:

Fred
Wilma
Barney
Betty
Larry
Curley
Moe
Cartman
Kenny
Lucy
Ethyl

Your program is to prompt the user for the name of the file to read these names from, and print an error message and shut down if the file could not be opened.

You are also to store the results in a file called results.dat. It should list the names of the candidates and their vote totals on each line:

Fred 0
Wilma 1
Barney 0
Betty 0
Larry 0
Curley 2
Moe 0
Cartman 1
Kenny 0
Lucy 1
Ethyl 0

 

Remember that you will need to include the <fstream.h> library for this.

 

In order to test this, you will need to create your own data file of candidates (which can be done using pico).

Implementing the Program

As always, you are encouraged to implement your program incrementally. In this case, you may want to implement the data files after everything else has been completed. In the meantime, you can initialize your array of candidate names with the following declaration:

char names[11][16] = {"Fred", "Wilma", "Barney", "Betty", "Larry", "Curley", "Moe", "Cartman", "Kenny", "Lucy", "Ethyl"};

Note that some or all of your functions will take the array of names as a parameter, as well as the array of votes. Keep in mind that functions which take two-dimensional arrays as parameters must specify the number of elements in the second dimension.

What to Turn In