“Baby names and birth weights”
Introduction
Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low birth weight indicates increased risk for complications and such babies are given specialized care until they gain weight. Public health agencies, such as the Centers for Disease Control and Prevention (CDC) in the United States, keep records of births and birth weights. In this project you will write principled object-oriented C++ code to read a data file recording birth information and answer questions based on this data.
Objective
You are given data in text files - each line in a text file is a baby girl’s first name followed by her birth weight in grams (an integer). The text file could contain a large number of such lines. Write code to answer the following questions:
- How many births are recorded in the data file?
- How many babies have a given first name?
- How many babies are born with a low birth weight (less than 2,500 grams)?
- Which baby name is the most popular?
The code
You are given “skeleton” code with many blank spaces. Your assignment is to fill in the missing parts so that the code is complete and works properly. The code is in three files:
- main.cpp contains the main function and a set of unit tests, implemented using assert statements - each assert statement compares the output of your code with the expected correct answer. Since the provided code is only a template, all the tests will fail immediately with runtime errors. As you add functionality and fix any bugs that may arise, the tests will be able to get further and further. Do not edit this main.cpp file.
- Baby.h contains code for a “Baby” data class. This class is incomplete and you should complete it.
- MedicalRecord.h contains a class whose methods can return answers to the questions asked in this project. You can use (dynamic) arrays but not any of the STL containers (such as std::vector). This class is incomplete and you should complete it.
Hints
MedicalRecord.h will require the use of an array-based data structure of Baby objects. Note that private data members are also incomplete. Feel free to add private member variables to both MedicalRecord.h and Baby.h as you need. There is more than one way to solve this problem!
In summary, do the following:
- Fill in the missing blanks in the skeleton code. Test your progress with the test programs or critic tool. Push your changes to GitHub at regular intervals.
- Check that your code works one last time.
main.cpp contains
#include <iostream> #include <fstream> #include <string> #include <cassert> #include "MedicalRecord.h" #include "Baby.h" using namespace std; int main() { try { { // test only the Baby class Baby babyTest("Testname", 1000); assert(babyTest.getName() == "Testname"); assert(babyTest.getWeight() == 1000); } { // test full code with a small data file MedicalRecord MR; MR.buildMedicalRecordfromDatafile("baby_data_small.txt"); // build a medical record from the file of baby names and weights\ int nBirths = MR.numberOfBirths(); cout << "Number of births: " << nBirths << endl; assert(nBirths == 10); int nEmma = MR.numberOfBabiesWithName("Emma"); cout << "Number of babies with name Emma: " << nEmma << endl; assert(nEmma == 2); int nLow = MR.numberOfBabiesWithLowBirthWeight(); cout << "Number of babies with low birth weight: " << nLow << endl; assert(nLow == 2); string mostPopularName = MR.mostPopularName(); cout << "Most popular baby name: " << mostPopularName << endl; assert (mostPopularName == "Sophia"); } { // test full code with a large data file MedicalRecord MR; MR.buildMedicalRecordfromDatafile("baby_data_large.txt"); // build a medical record from the file of baby names and weights\ int nBirths = MR.numberOfBirths(); cout << "Number of births: " << nBirths << endl; assert (nBirths == 199604); int nEva = MR.numberOfBabiesWithName("Eva"); cout << "Number of babies with name Eva: " << nEva << endl; assert (nEva == 566); int nLow = MR.numberOfBabiesWithLowBirthWeight(); cout << "Number of babies with low birth weight: " << nLow << endl; assert (nLow == 15980); string mostPopularName = MR.mostPopularName(); cout << "Most popular baby name: " << mostPopularName << endl; assert (mostPopularName == "Emma"); } } catch (exception &e) { cout << e.what() << endl; } // system("pause"); }
Baby.h contains
#pragma once #include <string> using namespace std; // class that contains information related to a single birth or baby name class Baby { public: Baby() { // default constructor }; Baby(string s, int w) { // constructor // TO BE COMPLETED } // a "getter" method int getWeight() { return -1; // TO BE COMPLETED } // a "getter" method string getName() { return "COMPLETE ME"; // TO BE COMPLETED } private: string name; int weight; };
MedicalRecord.h contains
#pragma once #include <string> #include <stdexcept> #include "Baby.h" using namespace std; class MedicalRecord { public: // default constructor MedicalRecord() { // TO BE COMPLETED } // destructor ~MedicalRecord() { // TO BE COMPLETED } // Load information from a text file with the given filename. void buildMedicalRecordfromDatafile(string filename) { ifstream myfile(filename); if (myfile.is_open()) { cout << "Successfully opened file " << filename << endl; string name; int weight; while (myfile >> name >> weight) { // cout << name << " " << weight << endl; Baby b(name, weight); addEntry(b); } myfile.close(); } else throw invalid_argument("Could not open file " + filename); } // return the most frequently appearing name in the text file string mostPopularName() { return "COMPLETE ME"; // TO BE COMPLETED } // return the number of baby records loaded from the text file int numberOfBirths() { return -1; // TO BE COMPLETED } // return the number of babies who had birth weight < 2500 grams int numberOfBabiesWithLowBirthWeight() { return -1; // TO BE COMPLETED } // return the number of babies who have the name contained in string s int numberOfBabiesWithName(string s) { return -1; // TO BE COMPLETED } private: // update the data structure with information contained in Baby object void addEntry(Baby b) { // TO BE COMPLETED } // Add private member variables for your data structure along with any // other variables required to implement the public member functions };
baby_data_small.txt contains
Sophia 4435 Emma 3561 Ava 2918 Sophia 3765 Mia 2204 Sophia 2749 Lourdes 3981 Sophia 2617 Olivia 4352 Emma 840
baby_data_large.txt contains
Grace 3385 Callie 2524 Presley 3396 Samantha 4455 Samantha 2662 Charleigh 4033 Luna 3122 Bria 3593 Lilah 2520 Madalyn 2838 Justice 3800 Grace 3284 Dakota 3296 Malaysia 3647 Gabriella 4020 Jasmin 3499 Allison 4193 Emma 2710 Maya 4216 Audrina 3368 Hazel 2780 Ava 4474 Lana 2717 Destiny 2952 Angelique 3972 Sophie 3396 Isabella 3011 Giavanna 4203 Lindsey 4420 Riley 4062 Francesca 3870 Harper 4130 Brynlee 2603 Chloe 3437 Catherine 3825 Ryann 3058 Evangeline 4232 Isla 3861 Audrey 2789 Penelope 4091 Lily 2630 Lyla 2086 Iris 3154 Morgan 3556 Harley 2974 Rosalie 2619 Harlow 2559 Faith 3976 Alessandra 2997 Gabriella 4060 Brooke 4351 Journey 3708 Emilia 4440 Meadow 3038 Athena 2538 Lily 3069 Brynlee 3161 Charlotte 2906 Hailey 2810 Rosie 3414 Allison 3182 Izabella 3302 Mia 3175 Nevaeh 3323 Nevaeh 1638 Juliette 4183 Isabella 3594 Claire 2236 Maryam 2509 Sage 3656 Sophia 2541 Lauryn 852 Zoey 3524 Kiara 4094 Paisley 4235 Adelyn 3205 Genesis 2919 Melody 2554 Serena 3399 Naomi 2812 Ava 2950 Emery 2802 Viviana 3198 Stella 2919 Jamie 4344 Addison 2886 Mia 4141 Jordan 3059 Lauren 2935 Lexie 4047 Chloe 2964 Emily 3003 Olivia 3761 Sofia 3388 Emilia 3588 Kaylee 3394 Kassidy 2505 Rachel 1828 Jazmine 4270 Baylee 3165 Lena 2395 Zelda 973 Isabella 3082 Sofia 4057 Nevaeh 3951 Alexis 4493 Sarah 3371 Delilah 3017 Arianna 4319 Amari 4375 Gianna 2629 Sarah 4095 Olivia 3163 Lauren 2546 Clara 3221 Daniella 3767 Carmen 2855 Brooklyn 3897 Nevaeh 2744 Aliyah 2529 Mila 3702 Kendall 4334 Jenna 3286 Melanie 3736 Aliyah 4007 Julia 3165 Allison 3983 Olivia 3999 Julia 2714 Jaelyn 2699 Whitney 2609 Kathleen 3694 Esmeralda 4457 Kara 2861 Sofia 4278 Roselyn 3993 Harley 3696 Bailey 2915 Abigail 1868 Destiny 2338 Scarlett 4396 Lily 2835 Abigail 2505 Juliet 4208 Grace 3935 Kennedy 4465 Clara 3181 Emily 3118 Abigail 3276 Ally 4339 Brooklyn 1570 Isla 4048 Kennedi 3741 Everly 1129 Elizabeth 3619 Audrina 3842 Serenity 3794 Ariana 3588 Ava 1551 Vanessa 2868 Sara 3359 Adriana 4350 Emily 4310 Cali 3816 Laila 1871 Harmony 1032 Jennifer 3547 Charlotte 2793 Genesis 3121 Fiona 1628 Reese 3656 Penelope 4003 Emily 3206 Reagan 2714 Harper 3047 Isla 4070 Paisley 2193 Ana 3238 Camila 3291 Rose 3817 Zoey 1517 Jazlyn 4067 Camryn 3307 Eva 2408 Angela 3695 Elena 2661 Ariel 2001 Makenna 4180 Jocelyn 973 Sabrina 3801 Olivia 2942 Ivy 3124 Megan 3060 Laila 3909 Luna 4366 Arianna 3662 Brooklyn 3380 Mckinley 2717 Helena 3334 Genesis 1516 Addyson 4074 Hazel 4287 Madelyn 3691 Mckenzie 4188 Daniela 4488 Finley 3765 Melody 2823 Virginia 3178 Milania 3088 Maci 3185 Emily 4231 Maria 3375 Alessandra 4066 Nyla 3391 Naomi 3563 Sophia 2826 Aaliyah 2403 Hayden 3355 Trinity 4052 Aubrianna 4481 Delilah 3003 Paisley 3393 Natalie 3415 Ellie 4183 Olivia 3929 Lydia 4177 Peyton 2848 Makenna 4452 Katherine 4496 Mia 2942 Haven 3035 Cali 1737 Sutton 3102
Executable code
//Baby.h
//Include the needed header file
#pragma once
#include <string>
using namespace std;
//Class
class Baby
{
//Access specifier
public:
//Default constructor
Baby(){}
//Constructor
Baby(string s, int w)
{
//Initialize name
name.assign(s);
//Initialize weight
weight =
w;
}
//Method getWeight()
int getWeight()
{
//Return
weight
return
weight;
}
//Method getName()
string getName()
{
//Return
name
return
name;
}
//Access specifier
private:
//Declare a variable for
name
string name;
//Declare a variable for
weight
int weight;
};
//MedicalRecord.h
//Include the needed header files
#pragma once
#include <string>
#include <stdexcept>
#include "Baby.h"
//Define default array size
#define ARR_SIZE 200000
//Namespace
using namespace std;
//Class
class MedicalRecord
{
//Access specifier
public:
//Default constructor
MedicalRecord()
{
//Initialize size
size =
0;
}
//Destructor
~MedicalRecord()
{
//Delete
delete[]bB;
}
//Method definition
buildMedicalRecordfromDatafile()
void
buildMedicalRecordfromDatafile(string filename)
{
//File
object
ifstream
myfile(filename.c_str());
//Open
file
if
(myfile.is_open())
{
//Display message
cout << "Successfully opened file "
<< filename << endl;
//Declare a varaible for baby name
string name;
//Declare a varaible for baby weight
int weight;
//Loop
while (myfile >> name >>
weight)
{
//Update
Baby b(name, weight);
//Function call
addEntry(b);
}
//Close file
myfile.close();
}
//Otherwise
else
//Throw exception
throw invalid_argument("Could not open file " +
filename);
}
//Method definition
mostPopularName()
string
mostPopularName()
{
//Declare
needed integer varaibles
int count
= 0, tmpcount = 0;
//Declare
needed string varaibles
string
mpName, tmpName;
//Loop
for (int i
= 0; i<size; i++)
{
//Update
tmpName = bB[i].getName();
//Update
tmpcount = 0;
//Loop
for (int j = 0; j<size; j++)
{
//Check condition
if
(tmpName.compare(bB[j].getName()) == 0)
//Increment
tmpcount++;
}
//Check condition
if (count == 0)
{
//Update
count = tmpcount;
//Function call
mpName.assign(tmpName);
}
//Check condition
else if (count<tmpcount)
{
//Update
count = tmpcount;
//Function call
mpName.assign(tmpName);
}
}
//Return
return
mpName;
}
//Method
numberOfBirths()
int numberOfBirths()
{
//Return
size
return
size;
}
//Method
numberOfBabiesWithLowBirthWeight()
int
numberOfBabiesWithLowBirthWeight()
{
//Needed
variable
int count
= 0;
//Loop
for (int i
= 0; i<size; i++)
//Check condition
if (bB[i].getWeight() <2500)
//Increment
count++;
//Return
count
return
count;
}
//Method
numberOfBabiesWithName()
int
numberOfBabiesWithName(string s)
{
//Needed
variable
int count
= 0;
//Loop
for (int i
= 0; i<size; i++)
//Check condition
if (bB[i].getName().compare(s) == 0)
//Increment
count++;
//Return
count
return
count;
}
//Access specifier
private:
//Methos addEntry()
void addEntry(Baby b)
{
//Check
condition
if (size
== 0)
{
//Update
bB = new Baby[ARR_SIZE];
//Update
bB[size++] = b;
}
//Check
condition
else if
(size<ARR_SIZE)
//Update
bB[size++] = b;
//Otherwise
else
//Display
cout << "No space in the array to add"
<< endl;
}
//Declare needed
variables
int size;
Baby *bB;
};
//Main.cpp
//Include the needed header files
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "MedicalRecord.h"
#include "Baby.h"
using namespace std;
//Driver
int main()
{
//Try block
try
{
//Test the Baby class
{
//Create
new instance
Baby
babyTest("Testname", 1000);
//Assert
assert(babyTest.getName() == "Testname");
//Assert
assert(babyTest.getWeight() == 1000);
}
//Test code with small
data file
{
//Create
new instance
MedicalRecord MR;
//Function
call
MR.buildMedicalRecordfromDatafile("baby_data_small.txt");
//Function
call
int
nBirths = MR.numberOfBirths();
//Display
cout
<< "Number of births: " << nBirths << endl;
//Assert
assert(nBirths == 10);
//Function
call
int nEmma
= MR.numberOfBabiesWithName("Emma");
//Display
cout
<< "Number of babies with name Emma: " << nEmma
<< endl;
//Assert
assert(nEmma == 2);
//Function call
int nLow =
MR.numberOfBabiesWithLowBirthWeight();
//Display
cout
<< "Number of babies with low birth weight: " << nLow
<< endl;
//Assert
assert(nLow == 2);
//Function call
string
mostPopularName = MR.mostPopularName();
//Display
cout
<< "Most popular baby name: " << mostPopularName
<< endl;
//Assert
assert(mostPopularName == "Sophia");
}
//Test code with large
data file
{
//Create a
new instance
MedicalRecord MR;
//Function
call
MR.buildMedicalRecordfromDatafile("baby_data_large.txt");
//Function
call
int
nBirths = MR.numberOfBirths();
//Display
cout
<< "Number of births: " << nBirths << endl;
//Assert
assert(nBirths == 199604);
//Function call
int nEva =
MR.numberOfBabiesWithName("Eva");
//Display
cout
<< "Number of babies with name Eva: " << nEva <<
endl;
//Assert
assert(nEva == 566);
//Function call
int nLow =
MR.numberOfBabiesWithLowBirthWeight();
//Display
cout
<< "Number of babies with low birth weight: " << nLow
<< endl;
//Assert
assert(nLow == 15980);
//Function call
string
mostPopularName = MR.mostPopularName();
//Display
cout
<< "Most popular baby name: " << mostPopularName
<< endl;
//Assert
assert(mostPopularName == "Emma");
}
}
//Catch block
catch (exception &e)
{
//Exception
cout << e.what()
<< endl;
}
}
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...
Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...
How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...
I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it.
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
int main()
{
char option;
vector<movie> movies;
while (true)
{
printMenu();
cin >>
option;
cin.ignore();
switch (option)
{
case 'A':
{
string nm;
int year;
string genre;
cout << "Movie Name: ";
getline(cin, nm);
cout << "Year: ";
cin >> year;
cout << "Genre: ";
cin >> genre;
//call you addMovie() here
addMovie(nm, year, genre, &movies);
cout << "Added " << nm << " to the catalog"
<< endl;
break;
}
case 'R':
{
string mn;
cout << "Movie Name:";
getline(cin, mn);
bool found;
//call you removeMovie() here
found = removeMovie(mn, &movies);
if (found == false)
cout << "Cannot find " << mn << endl;
else
cout << "Removed " << mn << " from catalog"
<< endl;
break;
}
case 'O':
{
string mn;
cout << "Movie Name: ";
getline(cin, mn);
cout << endl;
//call you movieInfo function here
movieInfo(mn, movies);
break;
}
case 'C':
{
cout << "There are " << movies.size() << " movies
in the catalog" << endl;
// Call the printCatalog function here
printCatalog(movies);
break;
}
case 'F':
{
string inputFile;
bool isOpen;
cin >> inputFile;
cout << "Reading catalog info from " << inputFile
<< endl;
//call you readFromFile() in here
isOpen = readFile(inputFile, &movies);
if (isOpen == false)
cout << "File not found" << endl;...
I am having trouble trying to output my file Lab13.txt. It will
say that everything is correct but won't output what is in the
file. Please Help
Write a program that will input data from the file
Lab13.txt(downloadable
file);
a name, telephone number, and email
address.
Store the data in a simple local array to the main
module, then sort the array by the names. You should have several
functions that pass data by reference.
Hint: make your array large...
I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...
I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...