Help with a sorting program.

So this is my code...


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
90
91
92
93
94
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Person{
	string name; 
	string number;
};

void namesort(Person data[], int antal);
void telesort(Person data[], int antal);

int _tmain(int argc, _TCHAR* argv[])
{
	int ans;
	const int NUMBER = 3;
	Person man[NUMBER];
	cout << "Write the full name of 3 persons: \n";
	for (int i = 0; i < NUMBER;){
		cout << "Person " << i+1 << ": ";
		getline(cin, man[i].name);
		cin.ignore(1000, '\n');
		cout << "Telephone: ";
		getline(cin, man[i].number);
		cout << "Put new person? 1 = Yes , 2 = Present Current Persons";
		cin >> ans;
		if(ans == 1){
			i++;
	}

	for (int i = 0; i < NUMBER; i++){
	int pos = man[i].name.find(' ', 0);
	if (pos != -1){
		string fName = man[i].name.substr(0, pos);
		int start = pos+1;
		int langd = man[i].name.length()-start;
		string eName = man[i].name.substr(start, langd);
		cout << eName << ", " << fName << " Telephone " << man[i].number << "\n";
	}
	}
	}

	namesort(man, NUMBER);

	int answer;
	cout << "\nHow would you like to sort the list? (1 = Surname, 2 = Telephone number):";
	cin >> answer;
	if (answer == 1){
		namesort(man, NUMBER);
		cout << "\nSorted after surname.\n";
		for(int o=0; o < NUMBER; o++){
			cout << left << setw(20) << man[o].name << ", " << man[o].name
				 << setw(15) << man[o].number << endl;
		}
	}
	if (answer == 2){
		telesort(man, NUMBER);
		cout << "\nSorted after number.\n";
		for(int p = 0; p < NUMBER; p++){
			cout << left << setw(20) << man[p].name
				<< setw(15) << man[p].number << endl;
		}
	}

	return 0;
}

void namesort(Person data[], int numberof)
{
	for(int m = 1; m < numberof; m++){
		int pos = m;
		Person temp = data[m];
		while (pos > 0 && data[pos-1].name > temp.name){
			data[pos] = data[pos-1];
			pos--;
		}
		data[pos] = temp;
	}
}

void telesort(Person data[], int numberof)
{
	for (int m = 1; m < numberof; m++){
		int position = m;
		Person temp = data[m];
		while (position > 0 && data[position-1].number > temp.number){
			data[position] = data[position-1];
			position--;
		}
		data[position] = temp;
	}
}


... It is supposed to let me enter 10 full names (going with 3 here for simplicity) then present the names like this:

Person 1: Wilkinson, Paula Telephone 0224-51829
Person 2: Mullen, Robert Telephone 0227-12490
etc..

And then it's supposed to ask me whether if I want to sort the list after surnames or telephonenumber. Now it worked just fine before I started dealing with the functions.

First of I'm not quite sure how to "port" the eName(surname) and fName(forename) so that they get written the same way after I sorted it as they are 2 different strings.

Then something else is going wrong aswell as after each time I put names in the cmd prompt it just cout's the name in "surname, forename" form and it's all really screwed up. Appreciate any help I can get. Thanks.
Last edited on
Line 28: Reads the choice but leaves the newline character in the buffer, making your next getline() to read an empty line. Use cin.ignore() after line 28, or better yet, stick to getline() and forget about operator>> whenever you use cin. It is just simpler.

Your sort is Bubble Sort. It's about the slowest sorting algorithm out there. Consider using std::sort().

Line 45 is unnecessary, I would say.

As for your eName and fName issue, I don't understand it. Please clarify.
Well my english isn't perfect. I'll try to explain myself better. But what I mean is I am supposed to sort after surname. And it's supposed to be written like Aleson, Adam ... Gibson, Mel etc. If I was only using 1 name I can use the data[pos-1].name for example but it is 2. eName and fName not just 1 string "name".
Then change Person:

1
2
3
4
5
6
struct Person
{
    string givenName;
    string surName;
    string telephone;
};


In order to receive the user's input, you have 2 choices:

1. Modify the input loop to ask for the name components separately (first given name, then surname, or the other way around).
2. Continue to ask for the full name and read the value into a temporary string variable. Then use your logic in lines 34 to 41 to break the full name into given name and surname.

Finally, change namesort() to sort by surName.
Topic archived. No new replies allowed.