Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that prompts for and reads in a student’s ID number. The pro

ID: 3763858 • Letter: W

Question

Write a C++ program that prompts for and reads in a student’s ID number. The program should read/search the file studentrecords.txt for that student ID number. The data file consists of a list of student records with each line consisting of a student id, first name, last name, and gpa of a student. The number of student records in the file is unknown. If found, the program should print the student ID and print the first name, last name, and gpa of the student that corresponds to that ID. If the student ID was not found in the file studentrecords.txt, the program should print the student ID and a message, which states: The student is not an active student of the university.

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int ID, again;
string firstName;
string lastName;
double gpa;
ifstream file;
int keyID;
while(true)
{
cout<<"Enter the ID of the student to search for: ";
cin>>keyID;
file.open("studentrecords.txt");
while(!file.eof())
{
file>>ID;
file>>firstName;
file>>lastName;
file>>gpa;
if(ID == keyID)
{
cout<<ID<<" ";
cout<<firstName<<" ";
cout<<lastName<<" ";
cout<<gpa<<endl;
break;
}
}
cout<<"ID: "<<keyID<<" is not an active student of the university."<<endl;
cout<<"Do you want to search for other student details? 1. Yes. 2. No.: ";
cin>>again;
if(again == 2)
return 0;
else if(again != 1)
{
cout<<"Invalid Choice. Quitting..."<<endl;
return 0;
}
file.close();
}
}



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote