Alphabetical Order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Diablo01
    New Member
    • Jun 2014
    • 13

    Alphabetical Order

    once all the names have been read in display which student will be at the front of the line and which student will be at the back of the line. You can assume that no two students have the same name.
  • Diablo01
    New Member
    • Jun 2014
    • 13

    #2
    My problem is that I dont know how to get the names to come out in alphabetical order. I only need 2 names from the list to appear on the screen. That is the first and the last. I have alot of code already and im guessing I am close but its not done yet. it does read the file and display 2 names but in no way are they in alphabetical order.

    Comment

    • Diablo01
      New Member
      • Jun 2014
      • 13

      #3
      #include <iostream>
      #include <fstream>
      #include <string>

      using namespace std;

      int main()
      {
      ifstream inputfile;
      string filename;
      string name;
      string front, back, student;
      int students = 46;


      inputfile.open( "lineup.txt ");

      if (!inputfile)

      {
      cout << "The input file did not open properly";
      return 0;

      }

      inputfile >> name;

      if (inputfile)
      {
      while (inputfile >> student)
      {
      for (int count = 1; inputfile >> student; count++)
      {

      if (count == 1)
      front = back = student;
      else if (student < front)
      back = student;
      }
      }

      cout << endl
      << front << " This is the name of the student at the front of the line.\n" ;
      cout << back << " This is the name of the student at the end of the line." << endl;

      inputfile.close ();
      }


      return 0;
      }

      Comment

      • Diablo01
        New Member
        • Jun 2014
        • 13

        #4
        This is what I have. I cannot use vector or algorithm . I am stuck with probably the most basic and annoying way to alphabetize a list of names and dont know what that is at this point.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Probably the easiest thing to do is to define an array of strings:

          Code:
          string students[10]; //10 students
          Assume students[0] is the lowest name.

          Then using a loop, compare student[0] with student[1] through student[9]. Anytime is lower than student[0], swap student[0] with student[x]. AT the end of the loop, the lowest alphabetic string will be in student[0].

          Then repeat the above only this time assume student[1] is the lowest student.

          Then repeat starting with student[2], etc... When you are starting with student[9], you are done and the array is in alphabetical order.

          This not the world's slickest sort but it should be good enough for your assignment.

          Comment

          Working...