Name Arrange and Sort

Hello again! I have written a code for this using only int main(), and I need to break this code down into functions. But I am having issues with trying to read in from a file the string data and printing it as well. I'm not sure how to properly setup a function for strings. I tried doing it the same style as I would for an int, but I'm sure there is something special I am missing. Anyways, here is the code that works(as in it arranges the names in the proper order. the last thing I need to add is a bubble sort(teachers requirement)to place the names in alphabetical order).

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const int MAXIMUM = 15;
ofstream out("name.out");
ifstream inf("names.in");

void read_file(string ary[], string& a)
{
    string b; a=0;
    while(inf >> b && a < MAXIMUM)
    {
        ary[a] = b;
        a++;
    }
}
void print_file(string bry[], string c)
{
    string name;
    for(string i=0; i<c; i++)
    {
        name += (last + ", " + first + " " + middle + "\n");

        out << bry[i];
    }

}
int main()
{
    int myarray, counter;
    if(!inf)
    {
       cout<< "Error\n";
        return 0;
    }

        string name, sorted, first, middle, last;

        out<<"Last, First Middle\n\n";
    read_file(myarray,counter);
    print_file(myarray,counter);






   /* while(inf >> name)
    {
        if(name == "lastname" || name == "surname")
        {
          inf >> last >> first >> middle;

          first[0]=toupper(first[0]);
          middle[0]=toupper(middle[0]);
          last[0]=toupper(last[0]);

          sorted += (last + ", " + first + " " + middle + "\n");
        }
            else
            {
                first = name;
                inf >> middle >> name >> last;

                first[0]=toupper(first[0]);
                middle[0]=toupper(middle[0]);
                last[0]=toupper(last[0]);

                sorted += (last + ", " + first + " " + middle + "\n");
            }
    }
    out<<sorted;
*/
    inf.close();
    out.close();
    return 0;
}

INFILE DATA:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
lastname Taylor marie denise
james wilis surname thomas
Stone Rock lastname Brown
surname lea high lee
stephens jabcobs lastname reynolds
lastname russell mack hussell
surname Lewis Michelle Tee
john mark surname marshall
lastname Moto ah sey
o e lastname vey
Twosee ore surname knocktosee
surname finitee two N
lastname ghigher Eve N
T tow surname jammer
Uh nuther lastname one
surname Lotts lue Knew
Ah C lastname moto
lastname phinshed Just about
Thee last surname won

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Last, First Middle

Taylor, Marie Denise
Thomas, James Wilis
Brown, Stone Rock
Lea, High Lee
Reynolds, Stephens Jabcobs
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark
Moto, Ah Sey
Vey, O E
Knocktosee, Twosee Ore
Finitee, Two N
Ghigher, Eve N
Jammer, T Tow
One, Uh Nuther
Lotts, Lue Knew
Moto, Ah C
Phinshed, Just About
Won, Thee Last


As you can tell I have given my entire code with the functions attempt. I believe once I see how to read the strings I should be able to complete the format I need.
Last edited on
a=0; a is a string. what do you think this should do (line 12).
ary[a] = b; //a is a string. index into array must be integer types.

this has nothing to do with functions: your code has some serious issues here, and that is the bigger issue.

you are still using global variables. there is no reason to do this ... you really need to stop doing this.
Last edited on
In this function, what is "a"? What kind of object is it?
1
2
3
4
5
6
7
8
9
void read_file(string ary[], string& a)
{
    string b; a=0;
    while(inf >> b && a < MAXIMUM)
    {
        ary[a] = b;
        a++;
    }
}
originally the function would be int in place of string in the 3 occurances. the code I submitted is buggy and needs to be editted. I just need assitance with turning the commented out code into functions. And I'm unsure how to make a function with strings instead of int
the function we are complaining about IS USING STRINGS NOT INTS.
but you do it the same way.
1
2
3
4
5
6
7
8
9
10
11
string foo()
{
   string s = "foo";
   return foo;
}

void bar(string &s)
{
   s = "bar";
}


they work *exactly like ints* in terms of how to use them in functions.
Last edited on
I've finally got an internet connection to make an update. I'm still stuck, the program will read and write, but I can't get the sorting to properly work(confliction with int* and int conversions.) Here's the code and output as it is right now.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const int MAXIMUM = 15;
ofstream out("name.out");
ifstream inf("names.in");

void read_file(ifstream& inf)
{
    string b,ary[MAXIMUM]; int a=0;
    while(inf >> b && a < MAXIMUM)
    {
        ary[a] = b;
        a++;
    }
}
void print_file(ofstream& out)
{
     string fullname,name,s1,s2,s3;
    while(inf >> fullname)
        {
            if(fullname == "lastname" || fullname == "surname")
            {
                inf >> s3 >> s1 >> s2;
                s1[0]=toupper(s1[0]);
                s2[0]=toupper(s2[0]);
                s3[0]=toupper(s3[0]);
                name += (s3 + ", " + s1+ " " + s2+ "\n");
            }
            else
            {
                inf >> s1 >> s2 >> s3;
                s1[0]=toupper(s1[0]);
                s2[0]=toupper(s2[0]);
                s3[0]=toupper(s3[0]);
                name += (s3 + ", " + s1+ " " + s2+ "\n");
            }
        }
        out << name;
}
void sorting(ofstream& out)
{
    string temp, ary[MAXIMUM] ;
    for(int pass=0; pass < MAXIMUM-1; pass++)
    {
        for(int i=0; i < MAXIMUM-1; i++)
        {
            if(ary[i]>ary[i+1])
            {
                temp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=temp;
            }
        }
    }
}

int main()
{
    int  counter;
    string name,myarray, sorted,print, first, middle, last;

    out<<"Last, First Middle\n\n";
    read_file(inf);
   print_file(out);
   sorting(out);


    inf.close();
    out.close();
    return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Last, First Middle

Reynolds, Jabcobs Lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, Mark Surname
Moto, Ah Sey
Vey, E Lastname
Knocktosee, Ore Surname
Finitee, Two N
Ghigher, Eve N
Jammer, Tow Surname
One, Nuther Lastname
Lotts, Lue Knew
Moto, C Lastname
Phinshed, Just About
Won, Last Surname

I'm unsure of what is wrong.The first few names are skipped and on some of the output it includes "lastname" or "surname" I need the program to read in all the names and sort them alphabetically.
Last edited on
I've made a few adjustments and the sorting function at least isn't receiving an error,but it isn't sorting the names as its meant. I'm still uncertain of how to get the whole file to read. And how to get the outfile to not print "lastname" or "surname"
your array in sort is a local variable with no (useful) data in it.
you need to pass it into sort.

out is still a global.

look. you are missing some very key concepts here.
local variables are created and destroyed when a function is called.
your read file creates an array, reads a file into it, and then throws that away. Its gone.

what you want is to make your array in main, once.
then pass it to readfile to be populated, then pass it to sort to be sorted.

do you understand this?
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <limits>

constexpr int max_names = 15;

void ignore_line(std::istream& is)
{ is.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }

int read_names(std::istream& is, std::string* a, int sz)
{
    ignore_line(is); 
    ignore_line(is);
    
    int nread = 0;
    while (nread < sz && std::getline(is, a[nread])) 
        ++nread;
        
    return nread;
}

void print_lines(std::ostream& os, std::string* a, int sz)
{ for (int i = 0; i < sz; ++i) os << a[i] << '\n'; }

void my_sort(std::string* a, int sz)
{
    for (int i = 0; i < sz; ++i)
    {
        for (int j = 0; j < sz - 1; ++j)
            if (! (a[j] < a[j + 1])) std::swap(a[j], a[j + 1]);
    }
}

int main()
{
    std::istream& input  = std::cin;
    std::ostream& output = std::cout;
    std::string names[max_names]; 

    int const names_used = read_names(input, names, max_names);    

    my_sort(names, names_used);
    print_lines(output, names, names_used);
}


Live demo:
http://coliru.stacked-crooked.com/a/85f4cbee948b6a7f

You will need to adapt this program to your assignment's requirements.
Last edited on
mboozi, what is your function
1
2
 void ignore_line(std::istream& is)
{ is.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }
doing?
ignore_line
^^^ Take a wild guess :P

his file appears to have lines he wanted to skip, and this function does that.
Unclear how to resolve but my outfile is blank and only prints out a total number of names rather than the names themselves.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAXIMUM = 15;

ofstream out("name.out");
ifstream inf("names.in");

int read_file(ifstream& inf, string* a, int sz)
{
    int nread=0;
    while(nread < sz )
        ++nread;

    return nread;
}
void print_file(ofstream& out, string* ary, int sz)
{ for(int i=0; i < sz; i++)
    out << ary[i] << '\n';
}
void sorting(string* ary, int sz)
{
    string temp;
    for(int j=0; j < sz; j++)
    {
        for(int i=0; i < sz-1; i++)
        {
            if(!(ary[i]>ary[i+1]))
            {
                temp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=temp;
            }
        }
    }
}

int main()
{
    out<<"Last, First Middle\n\n";
    string name[MAXIMUM];
    int const names_used= read_file(inf,name,MAXIMUM);

    sorting(name,names_used);
    print_file(out,name,names_used);

    inf.close();
    out.close();
    return 0;
}
Last edited on
I deduced the issue to line 15. I'm unsure how to make it read names rather than a number. I've attempted several different alternatives, with as you guessed, no luck.
After playing around, I've gotten the program to print in alphabetical order, but I need the rest of the data from the names.
The program is taking the string and progressing through each one. Where in the code would I place the syntax for the format of the output I need?

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAXIMUM = 15;

ofstream out("name.out");
ifstream inf("names.in");

void read_file(string* a, int& sz)
{
      sz=0;string b;
    while(inf>>b && sz < MAXIMUM )
    {
        a[sz]=b;
        ++sz;
    }
}
void print_file(string* ary, int sz)
{ for(int i=0; i < sz; i++)
    out << ary[i] << '\n';
}
void sorting(string* ary, int sz)
{
    string temp;
    for(int j=0; j < sz; j++)
    {
        for(int i=0; i < sz-1; i++)
        {
            if((ary[i]>ary[i+1]))
            {
                temp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=temp;
            }
        }
    }
}

int main()
{
    out<<"Last, First Middle\n\n";
    string name[MAXIMUM];int counter;
     read_file(name,counter);

    sorting(name,counter);
    print_file(name,counter);

    inf.close();
    out.close();
    return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Last, First Middle

Brown
Rock
Stone
Taylor
denise
high
james
lastname
lastname
lea
marie
surname
surname
thomas
wilis
Last edited on
Did this but it repeats the names multiple times..

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAXIMUM = 20;

ofstream out("name.out");
ifstream inf("names.in");

void read_file(string* a, int& sz)
{
    string last,c, first, middle, name,b;
      sz=0;
    while(inf>>b && sz < MAXIMUM )
    {
        a[sz]=b;
        ++sz;
    if(b == "lastname" || b == "surname")
    {
        inf>>last>>first>>middle;
        name += (last + ", " + first + " " + middle + "\n");
    }
    else
    {
        first=b;
        inf>>middle>>b>>last;
    }
    out << name;
    }
}
/*void print_file(string* ary, int sz)
{
     for(int i=0; i < sz; i++)
    out << ary[i] << '\n';
}*/
void sorting(string* ary, int sz)
{
    string temp;
    for(int j=0; j < sz; j++)
    {
        for(int i=0; i < sz-1; i++)
        {
            if((ary[i]>ary[i+1]))
            {
                temp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=temp;
            }
        }
    }
}

int main()
{
    out<<"Last, First Middle\n\n";
    string name[MAXIMUM];
    int counter;

    read_file(name,counter);
    sorting(name,counter);
   // print_file(name,counter);

    inf.close();
    out.close();
    return 0;
}

Last edited on
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAXIMUM = 20;

ofstream out("name.out");
ifstream inf("names.in");

void read_file(string* ary, int& sz)
{
    string a,b,c,d,name;
      sz=0;
    while(inf>>d && sz < MAXIMUM )
    {
        if(d=="lastname" || d=="surname")
        {
            inf >> c >> a >> b;
            ary[sz]=name;
            sz++;
            a[0]=toupper(a[0]);
            b[0]=toupper(b[0]);
            c[0]=toupper(c[0]);
            name += (c + ", " + a + " " + b + "\n");
        }
        else
        {
            a=d;
            inf >> b >> d >> c;
            a[0]=toupper(a[0]);
            b[0]=toupper(b[0]);
            c[0]=toupper(c[0]);
            name += (c + ", " + a + " " + b + " " + d + "\n");
        }
    }
}
void print_file(string* ary, int sz)
{
     for(int i=0; i < sz; i++)
    {
        out << ary[i] << "\n";
    }
}
void sorting(string* ary, int sz)
{
    string temp;
    for(int j=0; j < sz; j++)
    {
        for(int i=0; i < sz-1; i++)
        {
            if((ary[i]>ary[i+1]))
            {
                temp=ary[i];
                ary[i]=ary[i+1];
                ary[i+1]=temp;
            }
        }
    }
}

int main()
{
    out<<"Last, First Middle\n\n";
    string name[MAXIMUM];
    int counter;

    read_file(name,counter);
    sorting(name,counter);
    print_file(name,counter);

    inf.close();
    out.close();
    return 0;
}

OUTPUT:
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
88
89
Last, First Middle


Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark surname

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark surname
Moto, Ah Sey
Vey, O E lastname
Knocktosee, Twosee Ore surname

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark surname
Moto, Ah Sey
Vey, O E lastname
Knocktosee, Twosee Ore surname
Finitee, Two N

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark surname
Moto, Ah Sey
Vey, O E lastname
Knocktosee, Twosee Ore surname
Finitee, Two N
Ghigher, Eve N
Jammer, T Tow surname
One, Uh Nuther lastname

Taylor, Marie Denise
Thomas, James Wilis surname
Brown, Stone Rock lastname
Lea, High Lee
Reynolds, Stephens Jabcobs lastname
Russell, Mack Hussell
Lewis, Michelle Tee
Marshall, John Mark surname
Moto, Ah Sey
Vey, O E lastname
Knocktosee, Twosee Ore surname
Finitee, Two N
Ghigher, Eve N
Jammer, T Tow surname
One, Uh Nuther lastname
Lotts, Lue Knew
Moto, Ah C lastname


Gotten this far. It prints the name. But it prints one name then stacks the names until the end, rather than printing the list once. Also it isn't sorting anymore. I assume this is because of the miscoding in the read function?
Last edited on
I don't know what or how to alter this code to function as its suppose to. I'm so far away from being novice at code writing. You see the obvious error. Prints way to many times.
I think you need to
set name = ""
in your read-file while loop.

If I am right, what it is doing is adding a line to name every time it gets a line from the file.
so the first time
name is 1 line, with first name, put that in array[0]
then its first name and second name, 2 lines, in array[1]
then its first, second, and third in array[2]...
etc. you need to clear name every time so that array[0] is first, array[1] is second, array[2] is third, ... etc!

Last edited on
jonnin, you're absolutely correct. I mistakingly figured this out by asking a fellow student. I can't believe it was that easy of a fix, to repair what I thought would have been a means to recode the entire program. Thank you for taking the time to respond. I greatly appreciate it!
Topic archived. No new replies allowed.