sircrisp
sircrisp

Reputation: 1067

cannot convert from 'std::string' to 'char'

Changed completely due to suggestions from other member. Most problems solved, still having problems. Now won't output any names from the array in main. Not sure if I'm passing them back correctly from function.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubblesort(string[], const int);
int sub = 0;

int main()
{
const int maxsize = 100;
string friendArray[maxsize];

ifstream friends;
friends.open("myFriends.dat");

while (sub < maxsize)
 {
  getline(friends, friendArray[sub]);
  sub++;
 }

 bubblesort(friendArray, maxsize);


 cout<<friendArray[0]<<" "<<friendArray[1]<<" "<<friendArray[2];

 system("pause");
 return 0;
}



void bubblesort(string *array, const int size)
{
    bool swap;
    string temp;

    do
    {
        swap = false;
        for (int count = 1; count < (size - 1); count++)
        {
            if(array[count-1] >array[count])
            {
                temp = array[count-1];
                array[count-1] = array[count];
                array[count] = temp;
                swap = true;
            }
        }
    }
    while(swap);

}

Upvotes: 1

Views: 4299

Answers (3)

AusCBloke
AusCBloke

Reputation: 18492

Your problem isn't necessarily that temp inside bubblesort is not a char, the problem is that array is declared as a string and not a string[].

The reason you're getting the error is because array[count+1] is of type char, and temp is of type string. std::swap expects two elements of the same type.

However, that may be the least of your problems, your code doesn't compile for quite a few reasons. Not just that but you're passing in maxsize to bubblesort at each iteration. There's a flaw in both your logic and your syntax.

EDIT: Since you're still having trouble getting the sorting to work, here's a working modification of your code:

#include <iostream>

void bubblesort(std::string array[], size_t size)
{
  bool bSwapped;
  std::string temp;

   do
   {
      bSwapped = false;
      for (size_t count = 1; count < size; count++)
      {
         if(array[count-1] > array[count])
         {
            std::swap(array[count-1], array[count]);
            bSwapped = true;
         }
      }
   }
   while(bSwapped);
}

int main(void)
{
   std::string array[] = { "def", "ghk", "abc", "world", "hello" };

   bubblesort(array, sizeof(array)/sizeof(*array));

   for (size_t i = 0; i < sizeof(array)/sizeof(*array); ++i)
      std::cout << array[i] + " ";

   std::cout << std::endl;

   return 0;
}

bubblesort could also be written as: void bubblesort(std::string *array, size_t size). There's no difference in this case since, when passed to a function, arrays decay into pointers.

Since arrays are passed by reference, a pointer to the first element, any modifications made to array inside of bubblesort will actually be modifying your array in main. So that's how arrays are "returned".

std::vector is a good alternative to the standard array, since it automatically resizes and obviously contains the length of the internal array so that you don't have to pass the size everywhere you pass an std::vector. You can also use it the same way as a regular array.

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726709

You need to declare temp as char. You can use std::swap to avoid such mistakes in the future:

std::swap(array[count], array[count+1]);

This would make your code compile, but it would not do what you're trying to do (bubblesort). The problem is that you are passing a single string (which is also an "array" of characters) instead of an array of strings, which is, in a very lose sense, "an array of arrays of characters". Your bubblesort needs to accept string *array as its first parameter.

Upvotes: 4

Ori Pessach
Ori Pessach

Reputation: 6833

temp is a string, array[count] is a char (since an std::string is a vector of char elements.) I'm not sure what you're trying to do here, but the compiler is correct - you can't assign a char to a string.

You could change temp to be a char, since all you do with it is assign a char to it, and then assign it back to an element of array, which is also a char.

Upvotes: 4

Related Questions