Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program is suppose to read one name at a time from the txt file and then alp

ID: 3543938 • Letter: T

Question

The program is suppose to read one name at a time from the txt file and then alphabetize the list after each name is called


I am stuck on how iam suppose to alphabetize the students names and how to read one name at a time in the loop.














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


using namespace std;

int main()

{
      

    

    ifstream infile;

    string filename;
    string studentName;
    string firstStudent ="A";
    string lastStudent ="Z";

    

    //Ask user for name of file, ipen and test file
    cout <<"Enter the name of the file with your students names. ";
    cin >> filename;
    cout << endl;
    infile.open(filename.c_str());

    if (infile.fail())
        {            
            cout <<"Sorry, file "<< filename <<" was not found. ";
            cout <<" Ending program... ";
            return 1;

        }

    //file is open for reading.
    cout <<"Reading File " << filename << endl;
    cout << endl;

     if (infile >> studentName)
        {
                cout << studentName << endl;
                 studentName = firstStudent;
         }

    
     else
         { //display an error message
             cout <<"Error Opening the First Students Name" << endl;
         }

    
     while ( infile >> studentName )
        {
                
      
            
             if ( studentName < firstStudent)
             { firstStudent = studentName;}

             else if ( studentName >  lastStudent)
                 {lastStudent = studentName;}



     }
    


    infile.close(); // Close the File

    return 0;

}

Explanation / Answer

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


using namespace std;

int main()

{




ifstream infile;

string filename;
string studentName;
string firstStudent ="A";
string lastStudent ="Z";
int flag=1;

do{                                                                            //// to continue input and end at filename is 5
//Ask user for name of file, ipen and test file
cout <<"Enter the name of the file with your students names. ";
cin >> filename;
cout << endl;

if(filename.equal("5"))

flag = 0;

else{


infile.open(filename.c_str());

if (infile.fail())
{
cout <<"Sorry, file "<< filename <<" was not found. ";
cout <<" Ending program... ";


}
else{
//file is open for reading.
cout <<"Reading File " << filename << endl;
cout << endl;

if (infile >> studentName)
{
cout << studentName << endl;
studentName = firstStudent;
}


else
{ //display an error message
cout <<"Error Opening the First Students Name" << endl;
}


while ( infile >> studentName )
{



if ( studentName < firstStudent)
{ firstStudent = studentName;}

else if ( studentName > lastStudent)
{lastStudent = studentName;}



}



infile.close(); // Close the File

}
}
}while(flag!=0);

return 0;

}