A key health indicator for hospitalized patients is ratio of fluid intake (IVs, drinks, Popsicles, etc.) to fluid output (urine, blood loss, etc.). This metric is useful because it can identify dangerous issues such as dehydration and renal failure without requiring expensive, invasive tests. If interested, see this site for more details. For this assignment, you are going to write a program which keeps a running tally of intakes and output and flags when the differential is 1 liter or more, a commonly used value. This will give you experience with writing basic loops and doing simple computations in C++.
Unlike most other assignments, it is fine to work together with another student on this assignment. That is, we will not be examining your solutions for plagiarism. It would be good to document who you worked with in your code (as described in the integrity policy), but we will not check for this assignment.
The input to your program will be a simple log for a given patient. Each log entry is on its own line and will have the form
time item amount-in-milliliters
For example:
7:00 iv 200
7:30 orange_juice 100
8:00 iv 300
13:15 water 150
14:05 urine 100
15:05 bloodloss 50
15:30 soup 400
15:30 coffee 200
15:50 diarrhea 350
(You have to respect those who do these measurements!) For simplicity, you can make the following assumptions:
The program reads the data (with no prompt) and prints a message of the form
after consuming UUU at VVV, intake exceeds output by XXX ml
whenever the accumulated total is at least 1000 milliliters (one liter). In this output, UUU is the item name, VVV is the time, and XXX is the accumulated total. After all data is read, print a message of the form
the final fluid differential is YYY ml
where YYY is the final total. As an example, running your program on the above input will result in the output
after consuming soup at 15:30, intake exceeds output by 1000 ml
after consuming coffee at 15:30, intake exceeds output by 1200 ml
the final fluid differential is 850 ml
You should do some quick addition (by hand) on the above example to confirm these numbers are correct! Some further notes:
We will be consistent about providing multiple stages for assignments. You can receive credit for each stage even if nothing else works. You are advised to complete your solution in stages, but a working solution will pass all stages.
Stage 1 of this assignment is complete when you can read one line of input and print the fluid intake of that one line using the format of the expected final message. You can assume this line is an intake (rather than an output), so you do not need to use if statements or while loops to get this stage working. For example, if the text file contains just the line
10:30 coffee 175
then the output would be
the final fluid differential is 175 ml
You can implement this just by reading two strings and an integer and printing the integer in the above format. Test 1 will confirm this case. You can receive 40% credit on this assignment simply by passing test 1.
Note that you can read multiple pieces of information in an input statement. For example, if your input is
Cathy Xhu 19
and you have
string first, last;
int age;
then the code
cin >> first >> last >> age;
will set first
to "Cathy", last
to "Xhu", and age
to 19. This
style of reading will read text up to the next whitespace (newline, space,
etc.) and then store it in the destination. There is no need to read whole
lines and break them apart for an assignment like this.
Extend your solution to process the full input. That is, write an end-of-file controlled loop that reads the data. Remember that the general pattern for such a loop in C++ is
read
while ( cin ) {
process
read
}
It is important to read both before the loop header and at the end of the loop. Do not try to have just a single read - it may appear to work at first, but if you look closely at your output you will see errors.
If you have a program that reads until end-of-file and run that program
in CLion, you will need a way to indicate that you have entered all
data. Click on the console window and press Control-D (hold down the
Ctrl button and press D). This will send an end-of-file message to the
program. However, entering large amounts of text in the console window
is error-prone. Another way is to configure CLion to read input from a
file instead. If your project is open in CLion, click on the three
vertical dots next to the debug symbol, select Edit... (for the run
configuration), select the Redirect input from box, and browse to a
file like 2.in
. Then you can run and debug your code as usual with the
input coming from this file instead of the console (that is, instead of
the keyboard).
main
less
than 15 lines - you do not need to write additional functions (other
than main
) for this assignment.]{.mark}endl
after your last line
of output:
cout << "last line" << endl;
(Operating systems utilities typically assume all text lines are
terminated by the appropriate end-of-line characters. It is good
practice to always line-terminate your output. Recall endl
generates
the right line terminators for your system.)
while ( not end-of-file ) {
read
process
}
This does not work in C++ because end-of-file becomes true after attempting to read data. The pattern that does work is
read
while ( not end-of-file ) {
process
read
}
This means the code to read data is repeated. This is what works! If the read operation is complex, then put it into a function and call that function to do the work.
g++ -std=c++17 main.cpp
This will create the file a.out
(on Linux) or a.exe
(on Windows).
Linux users run it with ./a.out
and Windows with .\a.exe
.