Reading in string/ int arrays and bubble sorting

I need to use an infile which contains 15 lines like this data.

Firstname Lastname 1 2

I can't figure out how do I read in the first and last name as a single string and put it in an array at the same time as putting the numbers in another array. I need to count each line of data so I can bubble sort it.

Also the the number on right is supposed to represent a title {Mr,Mrs,Ms,Miss}. The title obviously stays with each person but I'm not sure if I can convert the numbers to string and combine it with the name before or after sorting.

Typically I would use something like this to read in two arrays of numbers for sorting, but it doesn't work when I add a string array. Also it's supposed to be one dimensional arrays no 2D.

1
2
3
4
  count = 0;
    while (!infile.eof() && infile >> seat[count] && infile
            >> title [count])
        count++;


Edit: So I'm able to read in all the data like this but I still want to combine the first,last name, and change the digit into a string.

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

const int MAXNUMBER = 30;

using namespace std;

int main()
{
    string first [MAXNUMBER], last [MAXNUMBER];
    int seat [MAXNUMBER], digit [MAXNUMBER], n, i;

    ifstream infile;
    infile.open("party.txt");

    if( !infile.is_open()) {
        cout << endl << "ERROR: Unable to open file" << endl;
        system("PAUSE");
        exit(1);
    }

        n = 0;
    while (!infile.eof() && infile >> first[n] >> last [n] >> seat[n]
           >> digit[n])
        n++;
        infile.close();
Last edited on
So I assume 1=Mr 2=Mrs 3=Ms and 4=Miss ??
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string first[20];
    string last[20];
    int titleNumber[20];
    string titles[4] = {"Mr","Mrs","Ms","Miss"};
    int count = 0;

    ifstream iFile("example2.txt");	//

    while (!iFile.eof())
    {
    	iFile >> first[count] >> last[count] >> titleNumber[count];
    	cout  << first[count] << " " << last[count] << " " << titleNumber[count] << endl;
    	count = count + 1;
    }

    count = count - 1;
    cout << endl << "count = " << count << endl;

    for (int i=0; i<count; i++)
    {
        cout  << first[i] << " " << last[i] << " " << titles[titleNumber[i]-1] << endl;
    }
    cout << count << endl;

    return 0;
}

Thanks I figured the rest out.
Topic archived. No new replies allowed.