I have pretty much writen the program however can seem to get the menu options (
ID: 3593895 • Letter: I
Question
I have pretty much writen the program however can seem to get the menu options (3) and (6) to work.. it wont sort the students and print it. and it wont add a new student. when I add a student in it whipes the file
Develop a C ++ program and submit an original implementation of a menu-driven program performing a number of tasks relating to students’ details: displaying all students’ details, adding new student to the record, displaying the average mark of all students, finding the maximum marks etc. You should complete the problem.
Problem
Use the following structure for this task.
struct student {
string name;
int id;
float mark;
};
Declare an array named studentArray of the structure type shown above, the size of the array is limited to maximum 100.
The main() function handles all interactions with the user and other functions:
It displays an appropriate welcoming message introducing the program.
Calls a function named readFile() which opens the text file grades.txt for reading and stores all of the students details from the file to an array named studentArray. The grades.txt has three columns, first column contains name, second column contains id and third column contains mark. The readFile()function has two parameters: one for receiving the file variable and one for the array, both receiving arguments passed by reference.
It then repeatedly calls the menu() function to display user options, get the user selection returned by the menu() function, use a switch statement to process user request by calling appropriate function(s)
It displays the result with an appropriate message after processing user request.
It displays a goodbye message when the user selects the Quit option from the menu and terminates the program. The menu() function has no parameters. When called, it displays a menu of 8 options allowing the user to select one and returns this option to the calling main()function.
The options displayed should be:
(1) Display students’ details
(2) Calculate average of all students’ marks
(3) Sort the students’ details
(4) Search for a particular student’s mark
(5) Find maximum
(6) Add new student to the record
(7) Quit program
Option (1) will use a function called displayStudents()called from the main()to display the contents of the studentArray array on the screen in an appropriate format. The displayStudents() function has two parameters: the array and the size of the array.
Option (2) will use a function called calculateAverage() which is designed to calculate the average value of all marks in studentArray and return the result to the main() function which will then display it with an appropriate message. This function also has two parameters: the array and the size of the array.
Option (3): Ask if the user wants to sort the students’ details with respect to either name or marks. Call appropriate functions to do the same.
Option (4) is to search for a particular student’s mark. The function should have three parameters, array, size and the name (a string variable) that the user searching for. This function returns the index of the array if it is found otherwise it returns -1. Display all the details of that student in main function. User should have the options to use either linear search or binary search .
Option (5) will use a function called findMaximum()which is designed to find the largest value of all marks in studentArray and return the result to the main() function which will then display it with an appropriate message. This function has two parameters: the array and the size of the array.
Option (6) will first use a function called updateFile() which will open the file in append mode, prompt the user for new student’s name, id and mark, and then write the new data at the end of the file using the same format as the original file. It will then the call the readFile()function used in the beginning of the program again to read the contents of the updated file and repopulate the studentArray.
Option (7) will terminate the program after displaying an appropriate goodbye message.
Program must be written in C++ please provide a sceen shot to show it works.
HERE IS WHAT I HAVE WRITEN
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include<algorithm>
using namespace std;
ifstream file;
ofstream myfile;
struct student {
string name;
int id;
float mark;
};
struct student studentArray[10];
int size = 0;
class Studentclass {
public:
void readFile()
{
file.open("grades.txt");
for (int i = 0; i < 10; i++) {
if (file.eof())
break;
file >> studentArray[i].name;
file >> studentArray[i].id;
file >> studentArray[i].mark;
size++;
}
}
public:
void displayStudents()
{
for (int i = 0; i < size; i++)
cout << studentArray[i].name << " " << studentArray[i].id << " " << studentArray[i].mark<< endl;
}
public:
int calculateAverage(int size,struct student studentArray[])
{
int sum=0,average=0;
for(int i=0;i<size;i++)
{
sum=sum+studentArray[i].mark;
}
average=sum/size;
return average;
}
public:
void Sort_Names()
{
//sort(studentArray.name,size+1);
}
public:
int searchStudent(int size,struct student studentArray[],string name)
{
for(int i=0;i<size;i++)
{
if(studentArray[i].name==name)
{
return i;
}
}
return -1;
}
public:
int FindMaximum()
{
int large=studentArray[0].mark;
for(int i=0;i<size;i++)
{
if(large<studentArray[i].mark)
{
large=studentArray[i].mark;
}
}
return large;
}
};
int main() {
Studentclass obj;
int option,position,maxmarks,average;
string name;
int id,marks;
cout<<"-----------------------------"<<endl;
cout<<"***Welcome to the Student Details******"<<endl;
cout<<"The Content From The File is:"<<endl;
obj.readFile(); //Reading The File
for (int i = 0; i < size; i++)
cout << studentArray[i].name << " " << studentArray[i].id << " " << studentArray[i].mark<< endl;
cout<<"-----------------------------"<<endl;
while(true) {
cout<<"***********MENU****************"<<endl;
cout<<"1. Display students’ details"<<endl;
cout<<"2. Calculate average of all students’ marks"<<endl;
cout<<"3 Sort the students’ details"<<endl;
cout<<"4. Search for a particular student’s mark"<<endl;
cout<<"5. Find maximum"<<endl;
cout<<"6. Add new student to the record"<<endl;
cout<<"7. Quit program"<<endl;
cout<<"Enter a menu option: "<<endl;
cin>>option;
switch(option)
{
case 1 :
obj.displayStudents();
break;
case 2:
average=obj.calculateAverage(size,studentArray);
cout<<"The Average Marks of All Students are:"<<average<<endl;
break;
case 3:
obj.Sort_Names();
cout<<"The Student Names Should Be Sorted:"<<endl;
break;
case 4:
cout<<"Enter the Student Name:"<<endl;
cin>>name;
position=obj.searchStudent(size,studentArray,name);
if(position!=-1) {
cout<<"The Student Details are Found at "<<position<<"Position"<<endl;
}
else{
cout<<"The Student Details are Not Found:"<<endl;
}
break;
case 5:
maxmarks=obj.FindMaximum();
cout<<"The Maximum Marks in All Students are:"<<maxmarks<<endl;
break;
case 6:
myfile.open("grades.txt");
cout<<"Enter New Record:"<<endl;
cout<<"Enter New Student Name:"<<endl;
cin>>name;
myfile<<name;
cout<<"Enter Id:"<<endl;
cin>>id;
myfile<<id;
cout<<"Enter Marks:"<<endl;
cin>>marks;
myfile<<marks;
cout<<"New Record Added Successfully:"<<endl;
break;
case 7:
cout<<"Thank You"<<endl;
exit(0);
default:
cout<<"No Option found! please select correct option"<<endl;
}
}
return 0;
}
Explanation / Answer
Given below is the fixed code for the question. Output is shown. Please don't forget to rate the answer if it helped. Thank you.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include<algorithm>
using namespace std;
struct student {
string name;
int id;
float mark;
};
class Studentclass {
private:
struct student studentArray[100];
int size = 0;
public:
void readFile()
{
ifstream file;
file.open("grades.txt");
size = 0;
for (int i = 0; i < 100; i++) {
file >> studentArray[i].name;
if (file.eof())
break;
file >> studentArray[i].id;
file >> studentArray[i].mark;
size++;
}
file.close();
}
void displayStudents()
{
for (int i = 0; i < size; i++)
cout << studentArray[i].name << " " << studentArray[i].id << " " << studentArray[i].mark<< endl;
}
int calculateAverage()
{
int sum=0,average=0;
for(int i=0;i<size;i++)
{
sum = sum + studentArray[i].mark;
}
average = sum / size;
return average;
}
int searchStudent(string name)
{
for(int i=0;i<size;i++)
{
if(studentArray[i].name==name)
{
return i;
}
}
return -1;
}
int FindMaximum()
{
int large=studentArray[0].mark;
for(int i=0;i<size;i++)
{
if(large<studentArray[i].mark)
{
large=studentArray[i].mark;
}
}
return large;
}
void addNewStudent(string name, int id, int marks)
{
ofstream myfile;
myfile.open("grades.txt", ios::app);
myfile<<name << " " << id << " " << marks << endl;
myfile.close();
cout<<"New Record Added Successfully:"<<endl;
readFile();
}
void Sort_Names()
{
for(int i = 0; i < size; i++)
{
int minIdx = i;
for(int j = i + 1; j < size; j++)
{
if(studentArray[j].name < studentArray[minIdx].name)
minIdx = j;
}
if(i != minIdx)
{
struct student temp = studentArray[i];
studentArray[i] = studentArray[minIdx];
studentArray[minIdx] = temp;
}
}
displayStudents();
}
void Sort_Marks()
{
for(int i = 0; i < size; i++)
{
int minIdx = i;
for(int j = i + 1; j < size; j++)
{
if(studentArray[j].mark < studentArray[minIdx].mark)
minIdx = j;
}
if(i != minIdx)
{
struct student temp = studentArray[i];
studentArray[i] = studentArray[minIdx];
studentArray[minIdx] = temp;
}
}
displayStudents();
}
};
int main() {
Studentclass clas;
int option,position,maxmarks,average;
string name;
int id,marks;
cout<<"-----------------------------"<<endl;
cout<<"***Welcome to the Student Details******"<<endl;
cout<<"The Content From The File is:"<<endl;
clas.readFile(); //Reading The File
clas.displayStudents();
cout<<"-----------------------------"<<endl;
while(true) {
cout<<"***********MENU****************"<<endl;
cout<<"1. Display students’ details"<<endl;
cout<<"2. Calculate average of all students’ marks"<<endl;
cout<<"3 Sort the students’ details"<<endl;
cout<<"4. Search for a particular student’s mark"<<endl;
cout<<"5. Find maximum"<<endl;
cout<<"6. Add new student to the record"<<endl;
cout<<"7. Quit program"<<endl;
cout<<"Enter a menu option: "<<endl;
cin>>option;
switch(option)
{
case 1 :
clas.displayStudents();
break;
case 2:
average=clas.calculateAverage();
cout<<"The Average Marks of All Students are:"<<average<<endl;
break;
case 3:
cout << "1. Sort by name" << endl;
cout << "2. Sort by marks" << endl;
cout << "Enter option: ";
cin >> option;
if(option == 1)
clas.Sort_Names();
else
clas.Sort_Marks();
break;
case 4:
cout<<"Enter the Student Name:"<<endl;
cin>>name;
position=clas.searchStudent(name);
if(position!=-1) {
cout<<"The Student Details are Found at "<<position<<"Position"<<endl;
}
else{
cout<<"The Student Details are Not Found:"<<endl;
}
break;
case 5:
maxmarks=clas.FindMaximum();
cout<<"The Maximum Marks in All Students are:"<<maxmarks<<endl;
break;
case 6:
cout<<"Enter New Record:"<<endl;
cout<<"Enter New Student Name:"<<endl;
cin>>name;
cout<<"Enter Id:"<<endl;
cin>>id;
cout<<"Enter Marks:"<<endl;
cin>>marks;
clas.addNewStudent(name, id, marks);
break;
case 7:
cout<<"Thank You"<<endl;
exit(0);
default:
cout<<"No Option found! please select correct option"<<endl;
}
}
return 0;
}
output
-----------------------------
***Welcome to the Student Details******
The Content From The File is:
John 111 89
Bob 222 95
Kate 333 70
Bill 444 60
-----------------------------
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
1
John 111 89
Bob 222 95
Kate 333 70
Bill 444 60
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
2
The Average Marks of All Students are:78
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
3
1. Sort by name
2. Sort by marks
Enter option: 1
Bill 444 60
Bob 222 95
John 111 89
Kate 333 70
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
3
1. Sort by name
2. Sort by marks
Enter option: 2
Bill 444 60
Kate 333 70
John 111 89
Bob 222 95
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
5
The Maximum Marks in All Students are:95
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
6
Enter New Record:
Enter New Student Name:
Michael
Enter Id:
555
Enter Marks:
80
New Record Added Successfully:
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
1
John 111 89
Bob 222 95
Kate 333 70
Bill 444 60
Michael 555 80
***********MENU****************
1. Display students’ details
2. Calculate average of all students’ marks
3 Sort the students’ details
4. Search for a particular student’s mark
5. Find maximum
6. Add new student to the record
7. Quit program
Enter a menu option:
7
Thank You