Bubble sorting Strings with strcmp()

Hello everyone, So I'm learning how to use arrays and I was given the task to write a program that declares 10 arrays and asks the user to input a name for each variable in the array... afterwards I was asked to do apply a bubble sort to put the names in order... I have never used a bubble sort before, the code for the bubble sort was given to us.. and I tried to apply it to my program... and I did, but I get an error message when I'm using strcmp(string1[i],string1[j]) Which tells me the following: "No suitable conversion function from "std::string" to "const char" exists"

I'll show you... here is all 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

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string names[10] = { " " };

	for (int index = 0; index <= 9; index++)
	{
		cout << "Enter name " << index + 1 << ": ";
		cin >> names[index];
	}

	cout << endl;

	string tmp;

	for (int i = 1; i < 10; ++i) {
	{
		for (int j = 4; j >= i; --j)
		{
			if (strcmp(names[j - 1], names[j]) > 0)
			{
				tmp = names[j - 1];
				names[j - 1] = names[j];
				names[j] = tmp;
			}
		}
	}

	system("Pause");
	return 0;
}


I'll pull this part out of the codr, it's the bubble sort that they provided us and this is where I'm stuck...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string tmp;

	for (int i = 1; i < 10; ++i) {
	{
		for (int j = 4; j >= i; --j)
		{
			if (strcmp(names[j - 1], names[j]) > 0) /* This is where I get the error message... 
                                                           "No suitable conversion function from "std::string" to "const char" exists" 
                                                           red lines under the arrays of names... */
			{
				tmp = names[j - 1];
				names[j - 1] = names[j];
				names[j] = tmp;
			}
		}
	}


Any help will be greatly appreciated! Thank you!
Last edited on
You use std::string. That's good.
You try using std::strcmp() on it. That's bad.

Comparison operators are already overloaded for std::string.
In plain English, it means you can simply do:

1
2
3
4
5
6
#include <algorithm> // for std::swap()

// ...

if (names[j - 1] > names[j])
    std::swap(names[j - 1], names[j]); // don't reinvent the wheel 


http://www.cplusplus.com/reference/string/string/operators/
http://www.cplusplus.com/reference/algorithm/swap/
Last edited on
And just to add, perhaps this confusion arises because...

1
2
3
#include <string>
// #include <string.h> // deprecated, use the new name "cstring" instead
#include <cstring> 


... string and string.h have very similar names. Yet they are two different libraries.
http://www.cplusplus.com/reference/cstring/
http://www.cplusplus.com/reference/string/

You are on the right path. As a proficient C++ programmer, you should prefer to use std::string instead of char arrays.
@Catfish666

Thank you!! It makes a lot more sense now! I was able to make the program work using the algorithms library! :D
I was able to make the program work using the algorithms library!

Did you cheat, by using std::sort()?
If not, you should at least try it for fun.

http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.