0

I have to sort the last names in alphabetical order and I have been looking for hours on how to do this but my book which is garbage offers no examples other than integers so any help will be awesome. Here is my code:

#include <cstdlib>
#include <iostream>
#include <iomanip>


using namespace std;

int getAverage(int,int,int);
string letterGrade(int);
void getInfo();

 int main(int argc, char *argv[])
{
getInfo();
system("PAUSE");
return EXIT_SUCCESS;
}

void getInfo()
{
const int info = 5;
string last[info];
string first[info];
int ID[info];
int score1[info];
int score2[info];
int score3[info];

for (int count = 0; count < info; count++)
    {
         cout << "last name\n";
         cin >> last[count];
         cout << "first name\n";
         cin >> first[count];
         cout << "enter ID\n";
         cin >> ID[count];
         cout << "enter test 1\n";
         cin >> score1[count];
         cout << "enter test 2\n";
         cin >> score2[count];
         cout << "enter test 3\n";
         cin >> score3[count];

    }

    cout << endl;

    for (int count = 0; count < info; count++)
    {

     cout << last[count] 
     << setw(10) << first[count] 
     << setw(10) << ID[count] 
     << setw(10) <<score1[count]
     << setw(10) << score2[count] 
     << setw(10) << score3[count] 
     << setw(10) << getAverage(score1[count],score2[count],score3[count])
     << setw(10) <<  letterGrade(getAverage(score1[count],score2[count],score3[count]));


        cout << endl;
    }

}
string letterGrade(int average)
{
   string lGrade;

    if (average > 89)
        lGrade = "A";
    else if (average > 79 && average < 90)
        lGrade = "B";
    else if (average > 69 && average < 80)
        lGrade = "C";
    else if (average > 59 && average < 70)
        lGrade = "D";
    else if (average >= 0 && average < 60)
        lGrade = "F";
    return lGrade;
}
int getAverage(int score1,int score2,int score3)
{
 int average;
 average = ((score1 + score2 + score3)/ 3);
 return average;
}
6
  • What problems are you facing? Commented Mar 14, 2014 at 19:25
  • How would you sort with integers? Commented Mar 14, 2014 at 19:26
  • I don't know where to start honestly. I can't see how I would sort the last name array and get it to output in the format that I have. Commented Mar 14, 2014 at 19:29
  • 1
    This would be much easier if you put all your data into a single array of structures instead of spreading it out over a bunch of parallel arrays. Commented Mar 14, 2014 at 19:31
  • I have to display all of the information in the form of a table. Commented Mar 14, 2014 at 19:33

1 Answer 1

2

I would suggest using struct i/o a bunch of arrays and std::sort (assuming you are allowed to do that).

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>


using namespace std;

int getAverage(int,int,int);
string letterGrade(int);
void getInfo();

struct gradeInfo {
    string last;
    string first;
    int ID;
    int score1;
    int score2;
    int score3;
};

bool mySort (gradeInfo g1,gradeInfo g2) { 
    return (g1.last <g2.last); 
} //lexicographical string comparison

 int main(int argc, char *argv[])
{
getInfo();
system("PAUSE");
return EXIT_SUCCESS;
}

void getInfo()
{
const int info = 5;
vector<gradeInfo> grades;

for (int count = 0; count < info; count++)
    {
         gradeInfo gradesEntry;
         cout << "last name\n";
         cin >> gradesEntry.last;
         cout << "first name\n";
         cin >> gradesEntry.first;
         cout << "enter ID\n";
         cin >> gradesEntry.ID;
         cout << "enter test 1\n";
         cin >> gradesEntry.score1;
         cout << "enter test 2\n";
         cin >> gradesEntry.score2;
         cout << "enter test 3\n";
         cin >> gradesEntry.score3;
         grades.push_back(gradesEntry);
    }

    cout << endl;

    sort(grades.begin(), grades.end(), mySort);

    for (int count = 0; count < info; count++)
    {
     int average = getAverage(grades[count].score1,grades[count].score2,grades[count].score3);
     cout << grades[count].last 
     << setw(10) << grades[count].first 
     << setw(10) << grades[count].ID 
     << setw(10) << grades[count].score1
     << setw(10) << grades[count].score2 
     << setw(10) << grades[count].score3
     << setw(10) << average
     << setw(10) <<  letterGrade(average);


        cout << endl;
    }

}
string letterGrade(int average)
{
   string lGrade;

    if (average > 89)
        lGrade = "A";
    else if (average > 79 && average < 90)
        lGrade = "B";
    else if (average > 69 && average < 80)
        lGrade = "C";
    else if (average > 59 && average < 70)
        lGrade = "D";
    else if (average >= 0 && average < 60)
        lGrade = "F";
    return lGrade;
}
int getAverage(int score1,int score2,int score3)
{
 int average;
 average = ((score1 + score2 + score3)/ 3);
 return average;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.