Lab Chapter 4
Here we will be taking data in as a file. Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same.
File input is very similar to how data comes in from the user through cin (using the same operators) but we will need to setup the stream ourselves.
First we need to include
We will need to create an input file stream to work from now. We will do this with ifstream. ifstream is a datatype just like int or float. Create a variable with the datatype being ifstream. We will define the variable by using the member accessor operator to call the open function and passing in “Judges.txt”, which is where our data will be.
Example: [whatever the variable name is].open(“Judges.txt”);
Your program must be setup to open “Judges.txt” as the file. For testing you can add your own Judges.txt file into visual studios or xcode as a resource. The judges file will be formatted like:
David 4 R Y G B
Max 2 R G
Elora 1 P
Felicity 3 G P Y
Sam 5 Y G P B R
Ken 2 P G
There are 6 judges (each on his only line). Each judge has a been given a different number of ballots (number of votes), this is the number given after their name. The judges are voting on the candidate that they like most, and the order of their votes matter. The first vote a judge gives is worth 5 pointes, the second vote 4 points, the third 3, and so on, with the fifth vote being worth 1 point.
Each judge has a maximum of 5 votes and there are 5 candidates in the voting race. The candidates are Red, Green, Blue, Yellow, and Purple (each represented in the file by their first letter). Your job is to add up the points for each candidate and announce the winner. In order to complete this assignment you will need to use selection statements for reading the file and for comparing the candidates at the end.
When comparing candidates, instead of comparing each one to one another pick one as the highest (or best) and compare it to the others one at a time. If you find a higher (better) candidate, then set this as the one you are comparing and continue until you go through all of them. If you have problems with this then ask during class or office/tutoring hours.
Lab Chapter 4