Checkpoint 15.11.1.
Construct the algorithm to produce a name struct from a string.
Students.txt
below that has information about the students in a course. For each student there are 3 lines:Data: Students.txt
Maxine Q. Craft Active 80,93,64,68,87,70,84,83,97,71 Stella N. McFadden Active 83,79,62,98,97,93,78,67,73,62 Oren C. Singleton Inactive 86,81,69,77,78,76,84,97,68,69 Palmer R. Price Active 84,98,98,87,61,96,89,77,73,73 Gray J. Witt Active 61,68,70,75,96,84,61,71,85,87 Hilary B. Gill Inactive 82,86,72,92,82,94,82,97,79,82 Ingrid A. Horne Active 72,66,65,91,65,93,99,93,92,63 Byron U. Stout Active 64,70,94,65,90,63,88,65,76,65
"Active"
or "Inactive"
, but an enumeration is more efficient to store and work with. And we wonβt have to worry about typos as much. The compiler canβt identify status == "Atcive"
as an error, but it can identify that status == Status.ATCIVE
doesnβt make sense.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Name {
string first;
string middle;
string last;
};
enum class Status {
INACTIVE,
ACTIVE
};
struct Student {
Name name;
Status currentStatus;
vector<double> scores;
};
int main() {
vector<Student> studentList;
}
Byron U. Stout
or 64,70,94,65,90,63,88,65,76,65
into the right data. So to make the job easier to tackle, letβs break it up.
Byron U. Stout
into a Name
struct. We could either find each space and use those to chop up the string. But since the tokens are separated by whitespaces, we could also use a stringstream
to "read" the data from the string into the parts of a name. Complete the code to do this job using a stringstream:
Status::ACTIVE
or Status::INACTIVE
based on the contents of the parameter line
. Throw a logic_error
if line
is not "Active"
or "Inactive"
:
while there is a comma in the string
get the text up to the comma
turn it into a number
add it to the vector
remove the text up to the comma from the string
turn the remaining text into a number
add it to the vector
line
by value so we can safely modify the copy that results. If we took it by const reference, we would not be able to modify line
by removing one part at a time.
Student createStudent(const string& nameLine,
const string& statusLine,
const string& scoresLine) {
Student student;
student.name = createName(nameLine);
student.currentStatus = createStatus(statusLine);
student.scores = createScores(scoresLine);
return student;
}