Sorting a string array

Hello everyone. I believe this is my first post here. This forum is incredibly helpful. Thank you to everyone who contributes!

I'm building a program that reads a list of names (first name and last name) stored in a file and assigns it to an array. I would then like to sort the array in alphabetical order (by first name). Lastly, I have to include a binary search so that the user can type in a name and the program will be able to tell if that name is in the array or not. However, I haven't even gotten to that part because I keep getting the following error message when I compile my code:

"error C2440: '=' : cannot convert from 'std::string' to 'int'"

with reference to this line "friendNames[smallestIndex] = temp;"

I'm a bit confused. Can anyone please point out what I'm doing wrong. Thank you!

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
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

//Function prototypes
int selectionSort(string friendNames[], int SIZE);

int main ()
{
	//Opens input file
	ifstream inputFile;
	inputFile.open("myFriends.dat");	

	//Max array size
	const int SIZE= 200;
	
	//Array to friends' names
	string friendNames[SIZE];
	
	//Loop counters
	int count = 0;
	int i = 0;

	//Assigns contents of myFriends.dat to array friendNames
	while (count < SIZE && inputFile >> friendNames[count])
		count ++;

	//Calls selectionSort algorithm
	selectionSort(friendNames, count);

	//Displays sorted array
	for (i=0; i<count; i++)
		cout << friendNames[i];

	//Closes input file
	inputFile.close();
	
	system("pause");
	return 0;
}

//**************************************************************************************
// The selectionSort algorithm sorts the array friendNames in						   *
// alphabetical order.																   *
//**************************************************************************************

void selectionSort(int friendNames[], int SIZE)
{
	int smallestIndex;
	string temp;
	 
	for (int i = 0; i < SIZE - 1; i++)
	{
		smallestIndex = i;

		for (int j = i + 1; j < SIZE; j++)
		{
			if (friendNames[j] < friendNames[smallestIndex])
				smallestIndex = j;
		}
		if (smallestIndex != i)
		{
			temp = friendNames[i];
			friendNames[i] = friendNames[smallestIndex];
			friendNames[smallestIndex] = temp;
		}
	}
}
void selectionSort(int friendNames[], int SIZE)

See that parameter, int friendNames[] ? Did you mean string friendNames[]?
Topic archived. No new replies allowed.