I am having many problems in this program.
In this program we actually have to create a file in which we have to write the data of a Student i.e his Roll number , his name and his GPA . Now we have to display the following menu on the console for user to select any one of it.
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
void main ()
{
int menu;
ifstream outstudentinfo("student.txt",ios::in);
if (!outstudentinfo)
{
cout << "File could not be opened" << endl;
exit(0);
}
do
{
cout<<"Press (1) to Enter Student information"<<endl
<<"Press (2) to Delete Student information"<<endl
<<"Press (3) to Sort Student information"<<endl
<<"Press (4) to Search Student information"<<endl
<<"Press (5) to Calcuate Average GPA for Students"<<endl
<<"Press (6) to Save Student information to file"<<endl
<<"Press (7) to Exit the program"<<endl
<<"Select any from above menu"<<endl;
cin>>menu;
switch (menu)
{
case 1:
{
enterInformation();
break;
}
case 2:
{
deleteInformation();
break;
}
case 3:
{
sortstudentinfo();
break;
}
case 4:
{
searchstudentinfo();
break;
}
case 5:
{
averageGPA();
break;
}
case 6:
{
saveinfo();
break;
}
case 7:
{
fileexit();
break;
}
}
}
while (1);
}
void enterInformation()
{
int i=count;
cout<<"Please enter the data of the student"<<endl;
if(count<10)
{
cout<<"Roll.no :";
getline(cin,StudentRollno[i]);
cout<<"Student Name :";
getline(cin,StudentName[i]);
cout<<"Student GPA :";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin>>StudentGPA[i];
cout<<endl;
count++;
}
else
cout<<"Sorry, memory is already full!!!"<<endl;
}
void deleteInformation()
{
int i,del=0;
for (i=0;i<count;i++)
{
cout<<"ST.NO:"<<i+1<<' '<<StudentRollno[i]<<' '<<StudentName[i]<<' '<<StudentGPA[i]<<endl;
}
cout<<"Please enter the ST.NO of the student whoes data you want to delete "<<endl;
cin>>del;
for (i=del;i<count-1;i++)
{
StudentRollno[i]=StudentRollno[i+1];
StudentName[i]=StudentName[i+1];
StudentGPA[i]=StudentGPA[i+1];
}
StudentRollno[count]="";
StudentName[count]="";
StudentGPA[count]=0.00;
for (i=0;i<count;i++)
{
cout<<"ST.NO:"<<i<<' '<<StudentRollno[i]<<' '<<StudentName[i]<<' '<<StudentGPA[i]<<endl;
}
count--;
}
void sortstudentinfo()
{
int i,j;
float temp3;
string temp1,temp2;
for (i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(strcmp(StudentName[i].c_str(),StudentName[j].c_str())==1)
{
temp1=StudentName[i];
StudentName[i]=StudentName[j];
StudentName[j]=temp1;
temp3=StudentGPA[i];
StudentGPA[i]=StudentGPA[j];
StudentGPA[j]=temp3;
}
}
}
for (i=0;i<count;i++)
{
cout<<StudentRollno[i]<<' ';
cout<<StudentName[i]<<' ';
cout<<StudentGPA[i]<<endl;
}
}
void searchstudentinfo()
{
int i,j=0;
string search;
cout<<"Please enter the name of the student whoes record you want to search"<<endl;
getline(cin,search);
for (i=0;i<count;i++)
{
if(strcmp(StudentName[i].c_str() ,search.c_str())==0)
{
cout<<"StudentRollno:"<<StudentRollno[i];
cout<<"StudentName:"<<StudentName[i];
cout<<"StudentGPA:"<<StudentGPA[i]<<endl;
j++;
break;
}
}
if(j==0)
cout<<"The entered name was not found"<<endl;
}
void averageGPA()
{
int i;
float average,sum=0.00;
for (i=0;i<count;i++)
{
sum=sum+StudentGPA[i];
}
average=sum/count;
cout<<"The average GPA of the students is :"<<average<<endl;
}
void saveinfo()
{
int i=0;
ofstream outstudentinfo("student.txt",ios::out);
for(i=0;i<count;i++)
{
outstudentinfo<<"StudentRoll.no:"<<StudentRollno[i];
outstudentinfo<<"Student name :"<<StudentName[i];
outstudentinfo<<"Student GPA :"<<StudentGPA[i]<<endl;
}
outstudentinfo.close();
}
void fileexit()
{
char option;
cout<<"Do you want to save the current data? Press (y) for yes and (n) for no"<<endl;
cin>>option;
if (option=='y')
saveinfo();
else if (option=='n')
exit(0);
else
cout<<"Invalid Input"<<endl;
}
Now following are the problems I am facing :
1.I have used cin and getline togather . When we input data of the student on console , and select the sorting code , I have used two loops there , one in which swapping is done and the other is used to print the sorted information on the basis of student name . When I input 3 for sorting it sorts the student information on the basis of student name and displays the sorted data on console . Now the problem is that this also has to print student name on console along with the Roll Number and GPA of the student but instead it only print Roll number and GPA. It seems to me that there is some problem with cin and getline use . But I am really confused how to solve it .
2.When I enter data of the student and when the menu is displayed again I press 4 for searching the name of the student . When I input the name of the student whose data I already have inserted , and press enter , instead of showing me his information the program starts running infinite .
Please help me about this problem , I will be grateful.
Regards ,
Ahmad Shoaib
I could get the code you posted to compile, as is. I changed it to compile but:
1. When are you going to read in the information in the student.txt and load it? Not added yet?
2. When I tried to enter student information, it hangs. That is a function of the mixing of cin >> and getline.
Sir , When I enter the data in my compiler this program runs fine and does not get hanged. Secondly yes I have not add the reading function yet . The problems I am facing are written above . Sir , Please help me about how to overcome these problems . I will be thankful .
Regards ,
Ahmad Shoaib
//-------------------------------------------
cout<<"Press (1) to Enter Student information"<<endl
<<"Press (2) to Delete Student information"<<endl
<<"Press (3) to Sort Student information"<<endl
<<"Press (4) to Search Student information"<<endl
<<"Press (5) to Calcuate Average GPA for Students"<<endl
<<"Press (6) to Save Student information to file"<<endl
<<"Press (7) to Exit the program"<<endl
<<"Select any from above menu"<<endl;
cin>>menu;
// I changed this <---------------------------------- LOOK
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// In enterInformation
cout<<"Student GPA :";
cin>>StudentGPA[i];
// I changed this <---------------------------------- LOOK
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<endl;
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu
1
Please enter the data of the student
Roll.no :3
Student Name :Zack
Student GPA :88
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu
1
Please enter the data of the student
Roll.no :88
Student Name :Mark
Student GPA :33
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu
1
Please enter the data of the student
Roll.no :66
Student Name :Ab
Student GPA :89
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu
3
66 Ab 89
88 Mark 33
3 Zack 88
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu
4
Please enter the name of the student whoes record you want to search
Mark
StudentRollno:88, StudentName:Mark, StudentGPA:33
Press (1) to Enter Student information
Press (2) to Delete Student information
Press (3) to Sort Student information
Press (4) to Search Student information
Press (5) to Calcuate Average GPA for Students
Press (6) to Save Student information to file
Press (7) to Exit the program
Select any from above menu