Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:48:58+00:00 2026-05-27T06:48:58+00:00

Changed completely due to suggestions from other member. Most problems solved, still having problems.

  • 0

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);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:48:59+00:00Added an answer on May 27, 2026 at 6:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow, due to folder renamings and various other source code changes, I'm having an
Using Complete C++ i18n gettext() hello world example I changed the locale from es_MX
I changed the output path of the test project, because the default path doesn't
I changed some parameters in a Visual Studio C++ project, and now I don't
Currently the project I'm working with does not have completely fixed models (due to
We've completely lost our repository and we have 8 developers with uncomitted changes. Restoring
Say there is a folder, '/home/user/temp/a40bd22344'. The name is completely random and changes in
I changed the folder that my WordPress installation was in. However, this worked fine,
I changed my .autotest file to use it with a Ruby-based project. After that,
I changed the folder permission of my Home directory by mistake and it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.