Comparing sizes of strings and sort them

So I'm trying to write a program that compares the size of the last name first and then the first name if the last names are equal. But I don't get the output right in the end, what am I doing wrong?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string fn1, fn2, fn3, ln1, ln2, ln3, tmpName1, tmpName2, tmpName3, name1, name2, name3;     
    
    cout << "Name 1: ";                                                 
    getline(cin, name1);                                                /
    cout << "Name 2: ";                                                 
    getline(cin, name2);                                                  
    cout << "Name 3: ";                                                 
    getline(cin, name3);                                                  
    cout << endl;
    
    
    // Name1
    std::string s(name1);
    std::istringstream iss1(s);
    
    std::string sub1;
    iss1 >> fn1 >> ln1;
    
    // name2
    std::string t(name2);
    std::istringstream iss2(t);
    
    std::string sub2;
    iss2 >> fn2 >> ln2;
    
    // name3
    std::string y(name3);
    std::istringstream iss3(y);
    
    std::string sub3;
    iss3 >> fn3 >> ln3;
    

    tmpName1 = ln1 + " " + fn1;
    tmpName2 = ln2 + " " + fn2;
    tmpName3 = ln3 + " " + fn3;
    
    //tmpName1
    for(int i = 0; i<tmpName1.length(); i++)
    {
        tmpName1[i] = tolower(tmpName1[i]);
    }
    //tmpName2
    for(int i = 0; i<tmpName2.length(); i++)
    {
        tmpName2[i] = tolower(tmpName2[i]);
    }
    //tmpname3
    for(int i = 0; i<tmpName3.length(); i++)
    {
        tmpName3[i] = tolower(tmpName3[i]);
    }
    
    
    if(tmpName2 < tmpName1)
    {
        tmpName2=tmpName1;
        tmpName1=name2;
        name2=name1;
    }
    if(tmpName3 < tmpName1)
    {
        tmpName3=tmpName1;
        tmpName1=name3;
        name3=name1;
    }
    if(tmpName2 < tmpName3)
    {
        tmpName2=tmpName3;
        tmpName3=name2;
        name2=name3;
    }
    
    cout << name1 << endl;
    cout << name2 << endl;
    cout << name3 << endl;
    
    return 0;
}
To check the size you want something like this.
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
43
44
45
46
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string fn1, fn2, fn3, ln1, ln2, ln3, tmpName1, tmpName2, tmpName3, name0, name1, name2, name3;
    
    cout << "Name 1: ";
    getline(cin, name1);
    cout << "Name 2: ";
    getline(cin, name2);
    cout << "Name 3: ";
    getline(cin, name3);
    cout << endl;
    
if (name3.size() < name1.size())
{
name0=name1;
name1=name3;
name3=name0;
}

if (name2.size() < name1.size())
{
name0=name1;
name1=name2;
name2=name0;	
}


if (name3.size() < name2.size())
{
name0=name2;
name2=name3;
name3=name0;	
}

    cout << name1 << endl;
    cout << name2 << endl;
    cout << name3 << endl;
    
    return 0;
}
what am I doing wrong?

- create stringstream objects from name1 etc directly
- variables sub1, sub2, sub3 unused
- use size(), not length(), for strings
- for only 3 names you might just get away with it but for larger # of names consider a menu driven option to add names

compares the size of the last name first and then the first name

the program doesn't compare by size anywhere. If that is what you want to do you'd be better off with something on the following lines:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

struct Name
{
	string f_name;
	string l_name;
	bool operator <(const Name& rhs)
	{
        if(l_name.size() != rhs.l_name.size())
        {
            return (l_name.size() < rhs.l_name.size());
        }
        if(l_name.size() == rhs.l_name.size())
        {
            return (f_name.size() < rhs.f_name.size());
        }
            return false;
    }
};
istringstream& operator >> (istringstream& is, Name& n)
{
    is >> n.f_name >> n.l_name;
    return is;
}
ostream& operator << (ostream& os, const Name& n)
{
    os << n.f_name << " " << n.l_name << '\t';
    return os;
}

int main()
{
    string name1, name2, name3;
    vector<Name> names;
    cout << "Name 1: ";
    getline(cin, name1);
    cout << "Name 2: ";
    getline(cin, name2);
    cout << "Name 3: ";
    getline(cin, name3);
    cout << endl;
    Name n;

    istringstream iss1(name1);
    iss1 >> n;
    names.push_back(move(n));

    istringstream iss2(name2);
    iss2 >> n;
    names.push_back(move(n));

    istringstream iss3(name3);
    iss3 >> n;
    names.push_back(move(n));

   sort(names.begin(), names.end());

    for (auto& elem : names)
    {
        cout << elem;
    }
}

And, finally, are you sure you want to compare by size and not lexicographically
Last edited on
Topic archived. No new replies allowed.