ok, so i've read most of the stuff here to help with sorting string arrays in alphabetical order, but i got more confused... i need some1 to take a look at my program and see where i went wrong. also, i need help in getting the largest,smallest,and average... yes, i'm a newbie at c++, and i think i got myself even more confuzzled... help? this is what i'm supposed to do:
1. Print the class roster.
2. Print Grade Book.
3. Sort the class roster in alphabetical order by students’ last name
4. Print out the class stats.
5. Exit.
and here's my program:
#include<iostream>
#include<iomanip>
using namespace std;
struct Info
{
char lastName[80];
char firstName[80];
char id[20];
int score;
};
int main ()
{
Info student[5];
char dummy;
int i;
for(i=0; i<5; i++){
cout<<"Enter last name: ";
cin>>student[i].lastName;
dummy=getchar();
cout<<"Enter first name: ";
cin.getline(student[i].firstName, 80);
cout<<"Enter id: ";
cin>>student[i].id;
cout<<"Enter score: ";
cin>>student[i].score;
}
cout<<endl;
cout<<endl;
cout<<endl;
int ans=0;
int t=0;
while(ans!=5){
cout<<"What would you like to do?"<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<"1. Print the class roster. "<<endl;
cout<<"2. Print Grade Book. "<<endl;
cout<<"3. Sort the class roster in alphabetical order by student's last name. "<<endl;
cout<<"4. Print out the class stats. "<<endl;
cout<<"5. Exit. "<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<endl;
cin>>ans;
if(ans==1){
for(int i=0; i<5; i++)
cout<<setw(10)<<student[i].lastName
<<setw(10)<<student[i].firstName
<<setw(10)<<student[i].id
<<endl;
cout<<endl;
}
if(ans==2){
for(int i=0; i<5; i++)
cout<<setw(10)<<student[i].id
<<setw(10)<<student[i].score
<<endl;
cout<<endl;
}
if(ans==3){
//lastNames in alphabetical order also print out the firstNames
int i=0;
if(student[i].lastName>student[i+1].lastName)
{
string temp;
temp=student[i].lastName;
student[i].lastName=student[i+1].lastName;
student[i+1].lastName=temp;
}
//display sorted arrays
for(i=0; i<5; i++)
cout<<student[i].lastName<<" "<<student[i].firstName<<" "<<endl;
}
if(ans==4){
int student[5].score;
//largest
{
int large, index;
int k;
for( k=5;k>0;k--){
large=student[5].score;
for(int i=0;i<k;i++){
if(student[i].score>large)
large=student[i].score;
}
cout<<large<<endl;
}
//smallest
{
int smallest, index;
int k;
for( k=5;k<0;k++){
large=student[5].score;
for(int i=0;i<k;i++){
if(student[i].score<smallest)
smallest=student[i].score;
}
cout<<smallest<<endl;
}
//average
average+=student[5].score/5;
cout<<average<<endl;
}
if(ans==5){
cout<<"Have a good day."<<endl;
cout<<endl;
}
t++;
}
return 0;
}