c++ programme

guys i dont know what wrong with this prgramme , its a error free code
in output window its not displaying anything except that its making several code.cpp files in my folder

please help me





#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
struct student //defining the structure student
{
string name;
int id;
float mark;
};
int readFile(student s[]) //function for reading file in the array
{
ifstream input("grades.txt"); //opening the file in read mode
int i=0; //for counting no of records in file
while(!input.eof())
{
input>>s[i].name>>s[i].id>>s[i].mark; //storing the information in array
i++; //incrementing i by 1
}
input.close(); //closing the file
return i; //returning the number of records stored in array
}
int menu() //for displaying the menu
{
cout<<"(1) Display students' details\n";
cout<<"(2) Calculate average of all students' marks\n";
cout<<"(3) Sort the students' details\n";
cout<<"(4) Search for a particular student's mark\n";
cout<<"(5) Find maximum\n";
cout<<"(6) Add new student to the record\n";
cout<<"(7) Quit program\n";
cout<<"Your choice: ";
int ch;
cin>>ch;
return ch; //returning the choice
}
void displayStudents(student s[],int n) //displaying the details of students
{
cout<<"\tName\t\tId\t\tMarks\n";
for(int i=0;i<n;i++) //looping through the array
{
cout<<"\t"<<s[i].name<<"\t\t"<<s[i].id; //displaying one by one
cout<<"\t\t"<<s[i].mark<<endl;
}
cout<<endl<<endl;
}
float calculateAverage(student s[],int n) //for calculating average marks
{
float avg=0; //for storing average
for(int i=0;i<n;i++)
avg+=s[i].mark; //adding all the marks to avg;
return avg/n; //returning average
}
bool compare1(student s1,student s2) //comparator for sorting according to name
{
return s1.name<s2.name;
}
bool compare2(student s1,student s2) //comparator for storing according to marks
{
return s1.mark<s2.mark;
}
void sortName(student s[],int n) //sorting according to names
{
sort(s,s+n,compare1); //calling sort function
}
void sortMarks(student s[],int n) //sorting according to marks
{
sort(s,s+n,compare2); //calling sort function
}
int searchMarks(student s[],int n,string m) //searching for a particular student's marks
{
int index=-1; //initialising index to 1
for(int i=0;i<n;i++)
{
if(s[i].name==m) //if this student's name is same as m
{
index=i; //then index becomes i
break;
}
}
return index; //returning the index
}
float findMaximum(student s[],int n) //finding maximum marks
{
float m=s[0].mark;
for(int i=1;i<n;i++) //looping through the array to compare the marks with m
m=max(m,s[i].mark);
return m; //returning m
}
int updateFile(student arr[]) //for updating file
{
ofstream output("grades.txt",ios::app); //opening file in append mode
student s;
//Taking student's details from user
cout<<"Student's name: ";
cin>>s.name;
cout<<"Student's Id: ";
cin>>s.id;
cout<<"student's marks: ";
cin>>s.mark;
output<<endl<<s.name<<" "<<s.id<<" "<<s.mark; //storing details in file
output.close();
int n=readFile(arr); //calling readFile() function
cout<<endl<<endl;
return n; //returning the updated value of size of array
}
int main()
{
student studentArray[100]; //declaring array of type student
int n=readFile(studentArray); //calling readInput function
int ch=0; //for storing choice
while(ch!=7) //looping till choice becomes 7
{
ch=menu(); //calling menu function
switch(ch)
{
case 1:displayStudents(studentArray,n); //if ch=1, then displaying students' records
break;
case 2: //if ch=2, then displaying the average marks
cout<<"Average marks are "<<calculateAverage(studentArray,n)<<endl<<endl<<endl;
break;
case 3: //if ch=3, then sorting the array
{
cout<<"According to (n)ame or (m)arks: ";
//if user enters n,then sorting according to name
//if user enters m, then sorting according to marks
char s;
cin>>s;
if(s=='n')
sortName(studentArray,n);
else
sortMarks(studentArray,n);
cout<<"done\n\n";
}
break;
case 4: //if ch=4, then finding the marks of particular student
{
cout<<"Name of the student : "; //asking the user for the student's name
string k;
cin>>k;
int m=searchMarks(studentArray,n,k); //searching for the marks of that student
if(m==-1) //if index is -1 then record not found
cout<<"Record not found\n\n\n";
else //otherwise displaying the marks
cout<<"Marks of this student are "<<studentArray[m].mark<<endl<<endl<<endl;
}
break;
case 5: //if ch=5, then displaying the maximum marks
cout<<"Maximum marks are "<<findMaximum(studentArray,n)<<endl<<endl<<endl;
break;
case 6: //if ch=6, then updating the file
n=updateFile(studentArray);
break;
case 7: //if ch=7, then quitting the program
cout<<"Goodbye\n";
break;
}
}
return 0;
}



these are the grades
grades.txt
Alex 123 60.75
Peter 122 54.50
Maria 111 75.25
Mary 101 65.00
Rocky 144 95.5
In your readFile funtion you need to check if the file is open before you use it.
1
2
3
4
5
6
7
8
9
10
int readFile(student s[]) //function for reading file in the array
{
  ifstream input("grades.txt"); //opening the file in read mode
  if (!input)
  {
    perror(nullptr); // #include <cstdio>
    return 1;
  }
  // ...
}
Last edited on
Topic archived. No new replies allowed.