Question

Please solve using c++ plus There are two text files with the following information stored in...

Please solve using c++ plus

There are two text files with the following information stored in them:
The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma
The department.txt file where each line stores the name, location and budget of the department separated by a comma

You need to write a C++ program that reads these text files and provides user with the following menu:
1. Enter the instructor ID and I will provide you with the name of the instructor, affiliated department and the location of that department.
2. Enter the department name and I will provide you with the location, budget and names of all instructors that work for the department.
3. Insert a record about a new instructor.
4. Exit

The above menu should continue to be displayed in a loop until the user selects option 4.

When the user selects option 1 above, the following should be displayed:
Enter the instructor ID:
If the user enters an instructor id that is not present in the text file, display "The ID doesnot appear in the database.", otherwise, display the name of the instructor, affiliated department and the location of that department.

When the user selects option 2 above, the following should be displayed:
Enter the department name:
If the user enters a name that is not present in the text file, display "The department name doesnot appear in the database.", otherwise, display the location, budget and names of all instructors that work for the department.

If the user selects option 3 above, display the following:
Enter the instructor id:
Enter the instructor name:
Enter the affiliated department name:
Once the user enters the above information, store the information in the instructor file

Use vectors instead of arrays whereever needed.

The program should work for any number of rows in the two text files.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

#include<iostream>

#include<fstream>

#include<string>

#include<vector>

#include <sstream>

using namespace std;

//structure that holds the info of instructor and department

struct instructor

{

int id;

string name,dept;

};

struct department

{

string name,location;

double budget;

};

bool isId(vector<instructor>,int);//bool function that returns true instructor id is in database otherwise false

bool isDept(vector<department>,string);//bool function that returns true department name is in database otherwise false

int menu();//menu function that displays the menu and return user choice

int main()

{

ifstream inInstructor,inDepartment;

string iname,idept,line;

int iId;

vector<instructor>Vins;

vector<department>Vdept;

instructor in;

department dep;

ofstream outInstructor;

bool cont=true;

inInstructor.open("instructor.txt");//opnes the instructor.txt file for reading

if(!inInstructor.is_open())//checks file is opend or not

{

cout<<"department.txt file not found! Exiting....."<<endl;

return 0;

}

inDepartment.open("department.txt");//opnes the department.txt file for reading

if(!inDepartment.is_open())

{

cout<<"instructor.txt file not found! Exiting....."<<endl;

return 0;

}//if both the files are opened for reading then read the files in structure and add the structure in vector

while(getline(inInstructor,line))

{

istringstream ss(line);

int i=0;

while (ss)

{

string s;

if (!getline( ss, s, ',' )) break;

if(i==0)

{

in.id=stoi(s);

}

if(i==1)

{

in.name=s;

}

if(i==2)

{

in.dept=s;

}

i++;

}

Vins.push_back(in);

}

while(getline(inDepartment,line))

{

istringstream ss(line);

int i=0;

while (ss)

{

string s;

if (!getline( ss, s, ',' )) break;

if(i==0)

{

dep.name=s;

}

if(i==1)

{

dep.location=s;

}

if(i==2)

{

dep.budget=stod(s);

}

i++;

}

Vdept.push_back(dep);

}

while(cont)

{

int choice=menu();//calling the menu function

switch (choice)//switch case for user input

{

case 1:

cout<<"\nEnter the instructor id:";

cin>>iId;

if(!isId(Vins,iId))

{

cout<<"\n\nThe ID doesnot appear in the database."<<endl<<endl;

break;

}

for(int i=0;i<Vins.size();i++)

{

if(Vins[i].id==iId)

{

idept=Vins[i].dept;

cout<<"\n\nName : "<<Vins[i].name<<endl<<"Department : "<<Vins[i].dept<<endl;

break;

}

}

for(int i=0;i<Vdept.size();i++)

{

if(Vdept[i].name==idept)

{

cout<<"Location : "<<Vdept[i].location<<endl;

}

}

break;

case 2:

cout<<"\nEnter the Department Name :";

cin>>idept;

if(!isDept(Vdept,idept))

{

cout<<"\n\nThe department name doesnot appear in the database"<<endl<<endl;

break;

}

for(int i=0;i<Vdept.size();i++)

{

if(Vdept[i].name==idept)

{

cout<<"\n\Location : "<<Vdept[i].location<<endl<<"Budget : "<<Vdept[i].budget<<endl;

break;

}

}

cout<<"Instructor for this depar are : "<<endl;

for(int i=0;i<Vins.size();i++)

{

if(Vins[i].dept==idept)

{

cout<<Vins[i].name<<endl;

}

}

break;

case 3:

cout<<"Enter the instructor id : ";

cin>>iId;

cout<<"Enter the instructor name : ";

cin>>iname;

cout<<"Enter the affiliated department name : ";

cin>>idept;

outInstructor.open ("instructor.txt", std::fstream::in | std::fstream::out | std::fstream::app);

outInstructor<<endl<<iId<<","<<iname<<","<<idept;

outInstructor.close();

break;

case 4:

cont=false;

break;

}

}

return 1;

}

int menu()

{

int ch;

cout<<"\n\n1) Enter the instructor ID and I will provide you with the name of the instructor, affiliated department and the location of that department:"<<endl;

cout<<"2) Enter the department name and I will provide you with the location, budget and names of all instructors that work for the department."<<endl;

cout<<"3) Insert a record about a new instructor."<<endl;

cout<<"4) Exit."<<endl;

while(true)

{

cout<<"Enter your choice : ";

cin>>ch;

if(ch<1 || ch>4)

cout<<"\n Invalid choice;";

else

return ch;

}

}

bool isId(vector<instructor>recivedIns,int recivedId)

{

for(int i=0;i<recivedIns.size();i++)

{

if(recivedIns[i].id==recivedId)

return true;

}

return false;

}

bool isDept(vector<department>recivedIns,string recivedName)

{

for(int i=0;i<recivedIns.size();i++)

{

if(recivedIns[i].name==recivedName)

return true;

}

return false;

}

both text file

File ConsoleApName Type Size Debug ConsoleAp department ConsoleApp %] Instructor and Department 14-09-2018 17:36 File folder 14-09-2018 15:18Tt Document 14-09-2018 15:27VC++ Project 1 KB 4 KB 3 KB 815:27VC++ Project Fi 14-09-2018 15:14 Te 14-09-2018 17:38 Source 4 KB converD Copier department Notepad country ile Edit Format View Help CrediteCa pocs, D Block,15890 instructor- Notepad File Edit Format View Help 1001,Jay, DOCS 1802,Viral,DOCS 1003,Hirani,Mangement 1004,Zeel,System Mangement,C Block,25000 System,Admin Block, 58080 dijkstra t Greyscale Ln 1, Col1 Ln 1, Col 1 Instructor a Debug ed 71 ENG 17:39 A IN 14-09-2018

outputafter adding the new instructor it will save in the file

Add a comment
Know the answer?
Add Answer to:
Please solve using c++ plus There are two text files with the following information stored in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program Write a C++ program to manage a hospital system, the system mainly uses file...

    C++ program Write a C++ program to manage a hospital system, the system mainly uses file handling to perform basic operations like how to add, edit, search, and delete record. Write the following functions: 1. hospital_menu: This function will display the following menu and prompt the user to enter her/his option. The system will keep showing the menu repeatedly until the user selects option 'e' from the menu. Arkansas Children Hospital a. Add new patient record b. Search record c....

  • C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...

    C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to Random Access Binary File. New program should be modified to display a menu for the user to do the following: 1. Replace Employee and Department classes with Employee and Department Structures. 2. Inside each structure, replace all string variables with array of characters. 3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department. 4....

  • C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...

    C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to Random Access Binary File. New program should be modified to display a menu for the user to do the following: 1. Replace Employee and Department classes with Employee and Department Structures. 2. Inside each structure, replace all string variables with array of characters. 3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department. 4....

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW...

    ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW TO SEND THE DATA SERIALIZED**** Write a c# program that stores student grades in a text file and read grades from a text file.  The program has the following GUI: There are four buttons: Create File, Save Case, Close File and Load File.  Initially, only the Create File and Load File buttons are enabled.  If the user clicks the Create File button, a Save File Dialog window...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • Hello. can anyone solve this. i am having hard time with this. please post a solution...

    Hello. can anyone solve this. i am having hard time with this. please post a solution ASAP a) Create a class named Student. Data fields for Student class include an integer Student ID, a String name, and a double CGPA. Methods include a constructor that requires values for all data fields, as well as get and set methods for each of the data fields. b) Create an application that allows a user to enter values for an array of 15...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT