Input File and sorting alphabetically

I have a lab with the following instructions:
Manually create an input text file called Lab22BInput.txt with the following data:
Dove
Doe
Alston
Zebra
Egg
Write a program that will read the names from the input file. Print out each of the names read to the screen. The program should go through the list of names and determine which comes 1st in the alphabet. The program should determine how many letters are in this name. Print out the name that comes first in the alphabet and the length of the name.

I have created the input file, and the data correctly outputs to the screen when I run the program. However, I do not know what kind of logic to use to sort them alphabetically and determine the amounts of characters.
So far I have the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    string str1, str2, str3, str4, str5;
    fstream namefile;
    namefile.open("Lab22BInput.txt", ios::in);
    if(!namefile)
    {
        cout << "Error opening file" << endl;
        return 0;
    }
    else
    {
        namefile >> str1 >> str2 >> str3 >> str4 >> str5;
        cout << str1 << " " << str2 << " "  << str3 << " " << str4 << " " << str5 << endl;
    }
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::string> input;
    std::ifstream namefile("Lab22BInput.txt");
    if(!namefile)  {
        std::cout << "Error opening file" << std::endl;
        return -1;
    }
    std::string temp;
    while(std::getline(namefile, temp)) {
        input.push_back(temp);
    }
    std::sort(input.begin(), input.end());
    for(const auto& s: input)
        std::cout << "String: " << s << "\nLenght: " << s.size() << '\n';
}
String: Alston
Lenght: 6
String: Doe
Lenght: 3
String: Dove
Lenght: 4
String: Egg
Lenght: 3
String: Zebra
Lenght: 5
My teacher hasn't taught us vectors and algorithms yet, so I am sure she wouldn't accept the lab that way. Thank you for the reply.
I completed the code the following way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
string str1, str2, str3, str4, str5, maxi;
fstream namefile;   
namefile.open("Lab22BInput.txt", ios::in);                                                  
 if(!namefile)
    {
        cout << "Error opening file" << endl;                                                   
        return 0;
    }
    else
    {
        namefile >> str1 >> str2 >> str3 >> str4 >> str5;                             
        cout << str1 << " " << str2 << " "  << str3 << " " << str4 << " " << str5 << endl;      
        if ( str1 < str2 )
			{
			maxi = str1;                                                                        
			}                                                                                 
			else if( maxi < str3 )                                                              
			{
			maxi = str3;                                                                      
			}
			else if ( maxi < str4 )                                                             
			{
			maxi = str4;                                                                        
			}
			else if ( maxi < str5 )                                                             
			{
			maxi = str5;                                                                        
			}
        cout << maxi << " comes first in the alphabet" << endl;               
        cout << maxi << " has " << maxi.length() << " characters" ;               
    }
    return 0;
}




Again, thanks a lot for your help.
Topic archived. No new replies allowed.