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

Exercise 1 (to be solved during the lab session): Write a menu-driven C++ progra

ID: 3590647 • Letter: E

Question

Exercise 1 (to be solved during the lab session): Write a menu-driven C++ program that offers the following menu:
1. Read a list of students
2. Display the list of students
3. Find a student
4. Sort the list of students by id
5. Exit
The menu options are described as follows:
1. Read list of students: Read from an input file “students.txt” a list of student ids and names, and stores them in two parallel arrays. The input file contains an unknown number of input lines (maximum 50 lines). Each line contains a student id and a student name (first name and last name separated by a space). The student ids must be stored in an array called IDS. The student names must be stored in an array called NAMES. The following is an example of what the input file may contain:
67890/13 Faisal Al-Hinai
12345/12 Laila Al-Zadjali
98765/12 Alia Al-Rahbi
43210/14 Salim Al-Amri
2. Display the list of students: Display the list of student ids and names.
3. Find a student: Search for a given student id (entered by the user) and display the student id and name if found. Otherwise display the message “Student id not found”.
4. Sort the list of students by id: Sort the arrays IDS and NAMES by increasing student id.
Your program must define and use one function for each menu option (other than Exit). You may use additional functions if needed.

Explanation / Answer

#include <iostream>

#include <fstream>

#include <string>

#include <stdlib.h>

#include<stdio.h>

using namespace std;

void showChoices();

void display(int ids[],string names[], int length);

void search(int ids[],string names[], int id,int length);

void BSort(int A[],string B[], int n);

int main()

{

ifstream inFile;

string name;

int x=0,i=0,j=0,length=0,k=0;

int id=0;

int ids[50];

string names[50];

int choice;

do

{

showChoices();

cin >> choice;

switch (choice)

{

case 1:

inFile.open("students.txt");

while (! inFile.eof() )

{

getline(inFile,name,' ');

string str = name.substr(0,5) +name.substr(6,2);

//sscanf(str, "%d", &x);

x=stoi(str);// to convert string to integer

ids[i++]=x;

str=name.substr(9);

names[j++]=str;

}

length=i;

break;

case 2:

cout<<"list of students";

display(ids,names, length);

break;

case 3:

  

cout<<" enter the student id to be searched : ";

  

cin>>id;

search(ids,names,id,length);

break;

case 4:

BSort(ids,names, length);

break;

case 5:

break;

default:

cout << "Invalid input" << endl;

}

}while (choice != 5);

return 0;

}

void showChoices()

{

cout << "MENU" << endl;

cout << "1: Read a list of students " << endl;

cout << "2: Display the list of students" << endl;

cout << "3: Find a student " << endl;

cout << "4: Sort the list of students by id " << endl;

cout << "5: Exit " << endl;

cout << "Enter your choice :";

}

void display(int ids[],string names[], int length){

int k=0;

for(k=0;k<length;k++){

cout<<ids[k]<<" "<<names[k]<<endl;

}

}

void search(int ids[],string names[], int id,int length){

int flag=0, k=0;

  

for(k=0;k<length;k++){

if(ids[k]==id){

flag=1;

break;

}

}

if(flag==1){

cout<<ids[k]<<" "<<names[k]<<endl;

}else{

cout<<"Student id not found";

}

}

void BSort(int A[],string B[], int n)

{

int I,J,Temp;

string name;

for(I=0;I<n-1;I++) //sorting

{

for(J=0;J<(n-1-I);J++)

if(A[J]>A[J+1])

{

Temp=A[J]; //swapping

name=B[J];

A[J]=A[J+1];

B[J]=B[J+1];

A[J+1]=Temp;

B[J+1]=name;

}

}

}