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;
 }

Dani AI

Generated

A few focused fixes will make the program reliable and easy to maintain.

Switch from C-style name arrays to C++ strings and the STL. Arrays like char lastName[80] cannot be assigned or compared with > in the way you tried; that’s why the compiler complains. Replace those fields with std::string and store students in a std::vector<Info>. Sorting, swapping and searching become trivial with the standard algorithms and you won’t need manual character-array copying.

Use a lexical comparator (sort by last name, then first name) and library functions for stats. Example (keeps it short and modern):

struct Info { std::string lastName, firstName, id; int score; };
std::vector<Info> students = /* filled earlier */;

std::sort(students.begin(), students.end(),
    [](const Info& a, const Info& b){
        if (a.lastName != b.lastName) return a.lastName < b.lastName;
        return a.firstName < b.firstName;
    });

Compute min, max and average with library helpers to avoid loop bugs and integer-division mistakes:

auto maxIt = std::max_element(students.begin(), students.end(),
    [](const Info& a, const Info& b){ return a.score < b.score; });

auto minIt = std::min_element(students.begin(), students.end(),
    [](const Info& a, const Info& b){ return a.score < b.score; });

double average = std::accumulate(students.begin(), students.end(), 0.0,
    [](double s, const Info& p){ return s + p.score; }) / students.size();

Practical tips: read lines with std::getline (or consume the leftover newline after operator>>) to avoid skipped inputs; validate numeric input before storing; use switch for the menu (cleaner than many if blocks); and follow ’s good point — don’t try to swap only lastName fields, swap whole Info objects (the STL does that for you). ’s nudge toward an OOP design is useful too: wrapping the menu and operations in a GradeBook class makes the code clearer and easier to test.

Recommended Answers

All 6 Replies

if(student.lastName>student[i+1].lastName) { string temp; temp=student.lastName; student.lastName=student[i+1].lastName; student[i+1].lastName=temp; }

lastName is a char array. It wont help you sort this way.
use strcmp() to compare or using string class for names.

second, there is no use swaping just the last name, the purpose of your structure gets defeated by that.
You must thank c++ complier, cause it adds default copy constructor and assignment operator function to you class. just swap the entire structure

qsort() is a good friend, as far as you know how to talk with him
I hope you like the code below

#include <iostream.h>
#include <string.h>
#include<stdlib.h>
struct values{
	char c[20], d[20] ;
} v[3] = { "a", "z", "b", "y", "c", "x" };

int compare (const void * a, const void * b)
{
  return ( strcmp ( (char *)a+20 , (char *)b+20 ));
}

int main ()
{
  int n ;
  qsort (v, 3, sizeof(values), compare);
  for (n=0; n<3; n++)
     cout << v[n].c << " "<< v[n].d;
  return 0;
}

qsort() is a good friend, as far as you know how to talk with him
I hope you like the code below

#include <iostream.h>
#include <string.h>
#include<stdlib.h>
struct values{
	char c[20], d[20] ;
} v[3] = { "a", "z", "b", "y", "c", "x" };

int compare (const void * a, const void * b)
{
  return ( strcmp ( (char *)a+20 , (char *)b+20 ));
}

int main ()
{
  int n ;
  qsort (v, 3, sizeof(values), compare);
  for (n=0; n<3; n++)
     cout << v[n].c << " "<< v[n].d;
  return 0;
}

One correction. In line 10
return ( strcmp ( ((values *)a)->d, ((values *)b)->d ) ; will be better.

Ok, i think i managed to get highest,lowest, average score., and i change the data to 5 students instead of 2.. As for sorting the lastNames... I tried what you guys suggested... maybe i'm writing it wrong somewhere... but it kept saying:
error C2228: left of '.lastName' must have class/struct/union
type is 'char'... And I would have no clue as how to fit the firstNames with the lastNames sorting...

#include<iostream>
#include<cstring>
#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){
        int large;
        for(int i=0; i<5;i++){
{
    int large, k;
    for(int k=5;k>0;k--){
        string large=student[i].lastName;
        for(int i=0;i<k;i++){
            if(strcmp(student[i].lastName,large)>0)
                large=student[i].lastName;

        }
    }

    }
        cout<<setw(10)student[i].lastName
            <<setw(10)student[i].firstName<<endl;
        }
    }

    if(ans==4){
        {int large;
        for(int i=0; i<5;i++){
//largest
int k;
for( k=5;k>0;k--){
large=student[i].score;
for(int i=0;i<k;i++){
if(student[i].score>large)
large=student[i].score;
}
}
        }
        cout<<"Highest score: "<<large<<endl;}
        {int smallest;
        for(int i=0; i<5;i++){
//smallest
int k;
for( k=5;k>0;k--){
smallest=student[i].score;
for(int i=0;i<k;i++){
if(student[i].score<smallest)
smallest=student[i].score;
}
}
        }
        cout<<"Lowest score: "<<smallest<<endl;}  
    //average
        {int average=0;
    for(int i=0; i<5;i++){
    average+=student[i].score/5;
    }
    cout<<"Average class score: "<<average<<endl;
    }
        cout<<endl;
        cout<<endl;
    }

    if(ans==5){
        cout<<"Have a good day."<<endl;
        cout<<endl;
        cout<<endl;
    }

    t++;
    }

    return 0;
 }

Try, this one

// fn compare
int compare (const void * a, const void * b)
{ 
   return(strcmp(((Info*)a)->lastName,((Info*)b)->lastName ));
}

// in main function
case 3:
{
    qsort ( student, 5, sizeof( Info ), compare ) ;
}

also it would be better if you use switch case than if else structure
Dont, use so much spaces in your output iomanip offers a better way of doing it.

Edit:
And finally,
case 4 is a mess. sort it out.
Use code tags & intend it. Its difficult to read.

I think u should first thing of oop before starting writing your code
coz as everyone knows , OOP help to implement your work more easilly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.