CMPSC121: Intro to Programming Techniques (Fall 2018)
Lab 07 (20 points)
Due Sunday, October 7 at 11:59pm
Objectives
After this lab assignment, students should be able to:
• Declare, assign values to, and access values in arrays
Background
Parallel arrays are multiple arrays, usually of same size, that hold related data in the same index number.
Consider the following parallel array contents for days of the week and their corresponding abbreviations:
Index number
string days[7]
string days3[7]
char days1[7]
0
“Monday”
“Mon”
‘M’
1
“Tuesday”
“Tue”
‘T’
2
“Wednesday”
“Wed”
‘W’
3
“Thursday”
“Thu”
‘R’
4
“Friday”
“Fri”
‘F’
5
“Saturday”
“Sat”
‘S’
6
“Sunday”
“Sun”
‘U’
Once properly populated, related data in each of the parallel arrays can be accessed using the same index number:
int current = 6;
cout << days1[current] << " is short for " << days[current];
//U is short for Sunday
When using cin to read user input for a string variable, whitespace characters (spaces, tabs, and newlines) are used to split up the user input. Consider the following example:
If the user enters "John Doe", then "Doe" will be buffered (unused but saved for future user input requests) and used in the cin >> city; in line 5, resulting in the undesirable behavior shown below:
In order to accept user input for strings that include spaces, the getline() function is used:
The getline() function accepts all user input until a newline is entered (Enter button is pressed). Now the user can type in “John Doe” and “New York” for name and city, respectively:
1 2 3 4 5 6
string name, city;
cout << "Please enter your name: ";
cin >> name;
cout << "Where are you from, " << name << "? ";
cin >> city;
cout << "I hear " << city << " is a great place!" << endl;
Please enter your name: John Doe
Where are you from, John? I hear Doe is a great place!
1 2 3 4 5 6
string name, city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Where are you from, " << name << "? ";
getline(cin, city);
cout << "I hear " << city << " is a great place!" << endl;
Please enter your name: John Doe
Where are you from, John Doe? New York
I hear New York is a great place!
Unfortunately, there is a complication when using both getline() and cin >> in the same program. Consider the following example:
1 2 3 4 5 6 7 8 9
int zip;
string city, state;
cout << "What is your ZIP code? ";
cin >> zip;
cout << "What city are you in? ";
getline(cin, city);
cout << "What state are you in? ";
getline(cin, state);
cout << "You are in " << city << ", " << state << ", " << zip;
Note the behavior shown below:
• cin >> in line 4 runs as expected
• The getline() function call in line 6 is seemingly skipped (does not wait for the user to type in a
value for city)
• The getline() function call in line 8 runs as expected
In order to avoid this complication, add cin.ignore() after a cin >> and before a getline().
What is your ZIP code? 16563
What city are you in? What state are you in? PA
You are in , PA, 16563
1 2 3 4 5 6 7 8 9
int zip;
string city, state;
cout << "What is your ZIP code? ";
cin >> zip;
cout << "What city are you in? ";
cin.ignore(); getline(cin, city);
cout << "What state are you in? ";
getline(cin, state);
cout << "You are in " << city << ", " << state << ", " << zip;
With the added cin.ignore() in line 6, the programs behaves as desired:
An explanation for the complication and fix will be provided during lab and upon request.
What is your ZIP code? 16563
What city are you in? Erie
What state are you in? PA
You are in Erie, PA, 16563
Instructions
Write a program named trivia.cpp that:
• Uses two parallel arrays of size 5:
o An array of string variables to hold true or false questions
o An array of bool variables to hold answers to the above questions
o These arrays should be populated by assigning values directly instead of getting user input
• Quizzes the user using the arrays in the following fashion: o Displays the question
o Prompts the user to enter an answer:
▪ Accept"True","T","true",and"t"fortrue
▪ Accept "False", "F", "false", and "f" for false
o Determines and display whether the user entered the correct answer
o Keeps track of and displays how many questions the user answered correctly.
The program must implement and use the following function:
• bool getAnswer(): prompts the user to enter an answer, validates it, and returns a Boolean value
that represents the user's choice of true or false Optional Bonus (+2 points)
Write a program named buildtrivia.cpp that extends the functionality of trivia.cpp by:
• Prompting the user and accepting user input to populate the questions and answers
• Allowing the user to create a desired number of questions, up to 10
Sample Output
Let's take the trivia of true or false questions!
Question 1
CPU stands for "Central Park University"
Answer 1
Enter a true or false answer: nope
Invalid entry, try again!
Enter a true or false answer: false
Correct!
Question 2
RAM stands for "Reserved Access Memory"
Answer 2
Enter a true or false answer: true
Incorrect!
Question 3
HDD stands for "Hard Disk Drive"
Answer 3
Enter a true or false answer: T
Correct!
Question 4
SSD stands for "Soft State Drive"
Answer 4
Enter a true or false answer: False
Correct!
Question 5
CPP stands for "C Plus Plus"
Answer 5
Enter a true or false answer: f
Incorrect!
You got 3 out of 5 questions correct.
Optional Bonus Sample Output
Let's build a trivia of true or false questions (enter "done" to finish)!
Question 1
School buses are yellow
Answer 1
Enter a true or false answer: t
Question 2
Whiteboards are yellow
Answer 2
Enter a true or false answer: f
Question 3
Bananas are yellow
Answer 3
Enter a true or false answer: t
Question 4
Yellow paint is yellow
Answer 4
Enter a true or false answer: t
Question 5
done
...
Let's take the trivia of true or false questions!
Question 1
School buses are yellow
Answer 1
Enter a true or false answer: t
Correct!
Question 2
Whiteboards are yellow
Answer 2
Enter a true or false answer: t
Incorrect!
Question 3
Bananas are yellow
Answer 3
Enter a true or false answer: t
Correct!
Question 4
Yellow paint is yellow
Answer 4
Enter a true or false answer: t
Correct!
You got 3 out of 4 questions correct.
Submission
Submit the following file(s) to Canvas before the deadline:
1. trivia.cpp
2. (optional bonus) buildtrivia.cpp