Prog 2: Rally Songs
9/13 Added the comment on what to do if random line begins with '#', space, or has length 0. Shown below in blue.
Write a program to display random lyrics from the songs used at 2020 presidential campaign rallies. Create a quiz to display random lyrics and ask the user to guess if the lyrics are from Donald Trump or Elizabeth Warren.
Running the program in Codio should look like the following, where user input is shown in bold:
---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 2 Singing Sweet Home Alabama all summer long And the last known survivor stalks his prey in the night Sipping whiskey out the bottle, not thinking 'bout tomorrow ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 3 Get up now, get up, get up now You're the only one I see And so on and so on and scooby dooby doo ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 1 Enter the number of lines you want to display -> 5 ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 2 Sweet home Alabama From Detroit down to Houston You had a busy day today Cause I'm as free as a bird now And this bird you can not change ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 4 Need a little help y'all And you cannot slip You're the one that always calls Yeah, you turn me on (They say love hurts) Enter 1 for Trump, 2 for Warren: 2 Correct! You got 1 out of 1, which is 100% ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 4 It was 1989, my thoughts were short my hair was long It was 1989, my thoughts were short my hair was long Carry on, nothing equals the splendor Turning back she just laughs The eye of the tiger Enter 1 for Trump, 2 for Warren: 2 Wrong! You got 1 out of 2, which is 50% ---- Program 2: Rally Songs ---- Select from the following: 1. Set number of lines to display 2. Get random lines from Trump.txt 3. Get random lines from Warren.txt 4. Rally song quiz 5. Exit the program Your choice --> 5 You got 1 out of 2, which is 50%
What You Need to Know
Variables, Expressions, Decision (if) statements, srand() and rand(), Loops, Functions and Parameters, Reading from a file, strings
Details
This time around we expect you to use functions and parameters in your program in situations where it makes sense to reuse code.
File sample.cpp is included with the project and illustrates how to declare an input file, open it, and read from it. It also illustrates handling random numbers using srand( 1) and rand(). Note that srand( 1) should only be done a single time in your program, before calling rand(). "Seeding" the random number generator in this way with a constant value means your program will always give the same output, in the same order, which helps with debugging your program, and allows your output to match the expected output in the assessments. Call the random number generator the minimum number of times necessary. Calling it more times than necessary will cause your output not to match the expected output.
Use C++ strings to store the two input file names, "Trump.txt" and "Warren.txt". To convert a C++ string to a C string when opening a file use the following:
string fileName = "Trump.txt"; // Name of input file
ifstream inputFileStream; // Declare an input file stream
inputFileStream.open( fileName.c_str() ); // Open the file
The input file format is:
348
# God Bless the USA: Lee Greenwood
If tomorrow all the things were gone
I worked for all my life
. . . # Sweet Home Alabama: Lynyrd Skynyrd
Turn it up
Big wheels keep on turning
. . .
where the value on the first line indicates how many subsequent lines there are in the file. Lines that begin with '#' have the song title followed by the song author. Songs are separated by a blank line which may have spaces on it or may be a zero-length line. If the random line chosen begins with '#', begins with space ' ', or has length 0, then read the next line. Don't call the random number generator again in this situation!
Each time you want to display a random song lyric line you will need to open the file being used, find the random line, and display it. The lyrics should not be stored anywhere besides the input files.
You will need to carefully format your quiz percent correct output. I recommend you use something like:
cout << fixed << setprecision( 0) << valueToDisplay << "%" << endl;
Do not use arrays for this program. Failure to follow this constraint will result in a 10 point deduction.
Food For Thought (but not assigned)
How many song lyric lines on average do you need to look at to score well on the quiz? What is the optimal value?
How are you able to make correct guesses, given your knowledge of the candidates? How might this sort of expertise (in some future course) be captured?