Milestone 1 #include <iostream> using namespace std; int main() { int x=0; //Run
ID: 3596527 • Letter: M
Question
Milestone 1
#include <iostream>
using namespace std;
int main() {
int x=0;
//Runnigng infinite loop till the user select the exit option
while(true){
// Displaying the menu
cout<<"Main Menu: 1. Department Menu 2. Staff Menu 3. Patient Menu 4. Exit the program Enter a choice (1-4): ";
//Taking user input
cin >> x;
//If conditions for different menu
if(x==1){
cout<<"Department Menu: 1.Go back to main menu ";
cin>>x;
if(x==1){
continue;
}
else{
cout<<"Enter valid input ";
}
}
else if(x==2){
cout<<"Staff Menu: 1.Go back to main menu ";
cin>>x;
if(x==1){
continue;
}
else{
cout<<"Enter valid input ";
}
}
else if(x==3){
cout<<"Patient Menu: 1.Go back to main menu ";
cin>>x;
if(x==1){
continue;
}
else{
cout<<"Enter valid input ";
}
}
//Exit condition
else if(x==4){
cout<<"Thank you for using my program ";
exit(0);
}
}
}
IN C++
your job is to add more code to on top of you Milestone 1 so that your code can now get (read) the data from the three input text files. In particular, the program will need to get input from three text files (one for each Department, Staff, and Patient).
When your main program first starts, it will read data from each input file , print the data to the screen, and then close the files. for this Milestone 2, you will read data from the text files and simply print the data to the screen.
I also provide you a code piece that reads data from the department file to help you start your milestone 2. You can accordingly write the code to read data from the patient and staff files. You will need to learn to use the getline function with a delimiter to achieve this milestone.
d.txt file
Pediatrics,11
Human Resources,23
Oncology,5
p.txt
Tom,Smith,333,9010,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
Tom,Green,444,9030,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
Alex,Brown,222,9030,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
s.txt
D,Alex,Red,9030,23,1/10/1970,M,631-999-777,alex.green@somehospital.com
A,Alex,Yellow,9000,5,1/10/1970,M,631-999-777,alex.green@somehospital.com
D,Alex,Green,9010,5,1/10/1970,M,631-999-777,alex.green@somehospital.com
N,Tom,Brown,9020,11,1/10/1970,M,631-999-777,alex.green@somehospital.com
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile;
int x = 0;
//Runnigng infinite loop till the user select the exit option
while (true) {
// Displaying the menu
cout
<< "Main Menu: 1. Department Menu 2. Staff Menu 3. Patient Menu 4. Exit the program Enter a choice (1-4): ";
//Taking user input
cin >> x;
//If conditions for different menu
if (x == 1) {
inFile.open("d.txt");
if (inFile.fail()) {
cout << "Error in opening the department file. ";
return 0;
} else {
string dName;
int dID;
// read the entire department file: each time while loop executes a single line of the
// a department file is read (that is name and ID of the department will be read)
// Example of a line of a department file:
//
// Human Resources,23
//
//
while (getline(inFile, dName, ',')) { // read a string from inFile until a comma is seen,
// ignore the comma and store the characters that come before comma into dName
inFile >> dID; // read an int from the file, until a non-digit character is seen.
// so as soon as the new line character is seen at the end of the current line,
// the reading will stop and the
// digits that are read before character will be save into int dID
inFile.ignore(); // ignore is a function that reads a single character and throws it away.
// so the new line character at the end of the current line will be
// ignored and the file cursor will move to the beginning of the new line
// hence the next time the while loop executes, the name and Id of the next department
// will be read
cout << dName << " " << dID << endl; // this line is to DEBUGGING purposes
// to make sure that name and id of the department is
//read correctly. In your final project, at this point, instead, you
//need to record the name and id of the
// department to the appropriate location in the department array.
}
inFile.close();
}
cout << "Department Menu: 1.Go back to main menu ";
cin >> x;
if (x == 1) {
continue;
}
else {
cout << "Enter valid input ";
}
}
else if (x == 2) {
inFile.open("s.txt");
if (inFile.fail()) {
cout << "Error in opening the staff file. ";
return 0;
} else {
string line;
cout << "Staff File Content" << endl;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
cout << "Staff Menu: 1.Go back to main menu ";
cin >> x;
if (x == 1) {
continue;
}
else {
cout << "Enter valid input ";
}
}
else if (x == 3) {
inFile.open("p.txt");
if (inFile.fail()) {
cout << "Error in opening the Patient file. ";
return 0;
} else {
string line;
cout << "Patient File Content" << endl;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
cout << "Patient Menu: 1.Go back to main menu ";
cin >> x;
if (x == 1) {
continue;
}
else {
cout << "Enter valid input ";
}
}
//Exit condition
else if (x == 4) {
cout << "Thank you for using my program ";
return 0;
}
}
}
============
OUTPUT:-
================
Main Menu:
1. Department Menu
2. Staff Menu
3. Patient Menu
4. Exit the program Enter a choice (1-4):
1
Pediatrics 11
Human Resources 23
Oncology 5
Department Menu:
1.Go back to main menu
1
Main Menu:
1. Department Menu
2. Staff Menu
3. Patient Menu
4. Exit the program Enter a choice (1-4):
2
Staff File Content
D,Alex,Red,9030,23,1/10/1970,M,631-999-777,alex.green@somehospital.com
A,Alex,Yellow,9000,5,1/10/1970,M,631-999-777,alex.green@somehospital.com
D,Alex,Green,9010,5,1/10/1970,M,631-999-777,alex.green@somehospital.com
N,Tom,Brown,9020,11,1/10/1970,M,631-999-777,alex.green@somehospital.com
Staff Menu:
1.Go back to main menu
1
Main Menu:
1. Department Menu
2. Staff Menu
3. Patient Menu
4. Exit the program Enter a choice (1-4):
3
Patient File Content
Tom,Smith,333,9010,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
Tom,Green,444,9030,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
Alex,Brown,222,9030,2/2/1990,M,33,Market St.,New York City,NY,17345,631-999-777,tom.smith@someplace.com
Patient Menu:
1.Go back to main menu
4
Enter valid input
Main Menu:
1. Department Menu
2. Staff Menu
3. Patient Menu
4. Exit the program Enter a choice (1-4):
4
Thank you for using my program