Cannot convert to 'Int'

I don't quite understand what exactly I change in the bubbleSort function to make it work.

1
2
 cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'int' in assignment
         temp = array[j];


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

//Function Prototypes
void displayArray(string[], int);
void bubbleSort(string[], int);

int main ()
{
  const int NUM_NAMES = 8;
  	
  string names [NUM_NAMES] = {"Collings, Bill ", "Smith, Bart ", "Allen, Jim ", "Griffin, Jim ", "Stamey, Marty ", "Rose, Geri", };
  
  //Display the unsorted array.
  displayArray(names, NUM_NAMES);
  
  //Sort the array.
  bubbleSort(names, NUM_NAMES);
  
  return 0;
}

void displayArray(string j[], int SIZE)
{
  for (int i=0; i < SIZE; i++)
    cout << j[i] << endl;
}

void bubbleSort(string array[], int size)
{
  cout << "Here are the sorted names:\n";
  cout << "---------------------------\n";
  int j;
  int temp;
  for (int i=0;i<(size-1);i++){
    for(j=i+1;j<size;j++){
      if (array[j] < array[i]){
        temp = array[j];
        array[j] = array[i];
        array[i] = temp;
        cout << array[i] << endl;
      }
    }
  }
    
}
Last edited on
you can't mix string and int. You are doing that here:

1
2
3
4
5
6
7
8
9
10
11
void bubbleSort([b]string array[], [/b]int size)
{
  cout << "Here are the sorted names:\n";
  cout << "---------------------------\n";
  int j;
  int temp;
  for (int i=0;i<(size-1);i++){
    for(j=i+1;j<size;j++){
      if (array[j] < array[i]){
       [b] temp = array[j];  //No can do!  
[/b]        
Last edited on
Got it! Changed it string temp; and works perfectly. Thank you!
Last edited on
Topic archived. No new replies allowed.