Hi I\'m having trouble with getting the names to sort I don\'t know what to do a
ID: 3593358 • Letter: H
Question
Hi I'm having trouble with getting the names to sort I don't know what to do
and adding additional students into it
thank you
#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");
myfile.open("grades2.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 students mark"<<endl;
cout<<"5. -Find maximum"<<endl;
cout<<"6. -Add new student to the record"<<endl;
cout<<"7. -Quit program"<<endl;
cout<<"Enter Any 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:
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<<"Good bye:"<<endl;
cout<<"-----------------"<<endl;
exit(0);
default:
cout<<"Wrong option! please select correct option"<<endl;
}
}
return 0;
}
grades Notepad File Edit Format View Help Mary 101 Rocky 144 95.5 Alex 123 60.75 Peter 122 Maria 111 75.25 65.00 54.50Explanation / Answer
You have 2 Questions:
1. How to sort records, based on a field, stored in a file?
2. How to append new records?
To clarify this, refer to the following code written in a simple cpp file without considering any class (to make the understanding clear). The explaination is written in Bold:
#include <bits/stdc++.h>
using namespace std;
struct Student
{
string name;
int id;
float mark;
};
// We should specify how to sort 2 records. Here we need to sorts based on the field name.
bool compareTwoStudents(Student a, Student b)
{
return (a.name < b.name);
}
void sortStudents(Student a[], int size)
{
sort(a, a+size, compareTwoStudents);
}
// Answer to the 2nd Part. Open the file in append mode and just add a new record.
void addRecord(Student a)
{
ofstream outfile;
outfile.open("grades.txt",ios::app);
outfile<<endl<<a.name<<" "<<a.id<<" "<<a.mark;
}
void displayStudents(Student a[], int size)
{
for (int i = 0; i < size; i++)
cout << a[i].name << "Id= " << a[i].id << "Mark= " << a[i].mark << endl;
}
int readFile(Student a[])
{
ifstream file;
int size=0;
file.open("grades.txt");
for (int i = 0; i < 100; i++)
{
if (file.eof())
break;
file >> a[i].name;
file >> a[i].id;
file >> a[i].mark;
size++;
}
return size;
}
int main()
{
int n = 100, size=0;
// array of structure objects
Student a[n];
size = readFile(a);
sortStudents(a, size);
// Display details of Students
displayStudents(a,size);
Student newRecord;
newRecord.name="CCC";
newRecord.id=7;
newRecord.mark=90;
addRecord(newS);
return 0;
}
Hope it helps to solve your problem and to apply it in your original code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.