Swap function failure

Hello all! I am trying to write a program that takes its input from a file that has a list of people's names, ages and genders. My problem is in my sort function. The error statements that I am getting are:

cannot convert from 'char [31]' to 'char'
left operand must be l-value
cannot convert from 'char' to 'char [31]'

The errors occurs on the last three lines of code when I assign the variable temp equals .......

After searching Google about this, most people have said that the strcpy function is what someone should use to swap. Please take the time to look at my code and point me in the right direction. Any and all advice would be greatly appreciated. I also have previewed this post and cannot get it to indent the code for me, so I do apologize for that. Thank you.

This is all of the code for the program.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

const int MAX_PEOPLE(10);
const int NAME_LENGTH(31);

struct Person
{
char name[NAME_LENGTH];
char gender;
int age;
};

int readPeople(ifstream& in, Person people[]);
void printPeople(Person people[], int count);
void sortPeople(Person people[], int count);

int main()
{
ifstream infile;
Person people[MAX_PEOPLE];
int count;

infile.open("ch10People.txt");
if(infile.fail() )
{
cout << "Input file failed to open.\n";
exit(1);
}

count = readPeople(infile, people);
sortPeople(people, count);
printPeople(people, count);
infile.close();

return 0;
}

int readPeople(ifstream& in, Person people[])
{
int count(0);

in >> people[0].name >> people[0].age >> people[0].gender;
while(!in.eof() && count < MAX_PEOPLE)
{
count++;
in >> people[count].name >> people[count].age >> people[count].gender;
}

return count;
}

void printPeople(Person people[], int count)
{
int i;

cout << "Name Age Gender\n";
cout << "-----------------------------------------\n";

for(i = 0; i < count; i++)
{
cout << setw(NAME_LENGTH) << left << people[i].name;
cout << setw(3) << right << people[i].age;
cout << setw(5) << right << people[i].gender << endl;
}
}

void sortPeople(Person people[], int count)
{
int i, j, minIndex, topIndex;
char temp;

for(topIndex = 0; topIndex < count - 1; topIndex++)
{
minIndex = topIndex;
for(j = topIndex + 1; j < count; j++)
{
if(people[j].name > people[topIndex].name)
{
minIndex = j;
}
}

temp = people[minIndex].name;
people[minIndex].name = people[topIndex].name;
people[topIndex].name = temp;
}
}
use a std::string in place of an array of char, and it will work.
I'm not exactly sure what you mean by use std::string. I replaced the statement:

char name[NAME_LENGTH]; to:

string name[NAME_LENGTH]; as well as included the directive #include string

but found even more errors. Could you elaborate on this subject more please? Thank you very much for your help and reply.
A string already contains an array, so you don't need to declare an array:
string name;

Your problem came from the fact that name is an array, while temp is not an array.
And you can't copy two arrays by doing something like array1 = array2;. It may compile, but the results will not be what you expect.

Also, are you aware that your sorting function only reoders the name ? That means that the age and gender information will not move and will not be associated with the correct name anymore.
Thank you for your help. It is compiling now that I have changed the necessary parts to string. I think there may be something wrong though in my bool statement, because it swaps the names of the people but not into alphabetical order like I need it to. And, yes I saw that I had not yet swapped the ages and genders. I was just trying to get one part of it working first. That whole "divide and conquer" thing. Im sorry that I had forgot to mention that I was trying to sort the names alphabetically. Do you see anything that I'm missing? I having to use the ASCII character set chart, whereas, the capital letter A = 65, B = 66 and so forth. Once again, your help is very appreciated!
In your sortPeople() function, I think the condition should be if(people[j].name < people[minIndex].name)
I did try that, but so far no luck. I'm going to keep playing with it and see what happens. Just out of curiosity, is there a way I could have done it with using the char type in my arrays and stuff? If so, what would the code look like for it?
Thank you.
I tried your code with the suggested corrections, and it worked as intended.

You could have used char arrays. In that case you would have had to define temp as an array: char temp[NAME_LENGTH];
Your swapping code would have been:
1
2
3
strcpy( temp, people[minIndex].name );
strcpy( people[minIndex].name, people[topIndex].name );
strcpy( people[topIndex].name, temp );

You would have had to change your comparison code too.
When you compute if(people[j].name > people[topIndex].name) you're in fact comparing 2 pointers.
There is no ready to use c-strings comparison functions, so you'd have had to write you own:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool IsGreaterThan( const char str1[], const char str2[] )
{
    // this function returns true if str1 is greater than str2 in lexicographic order
    // it is assumed that str1 and str2 are valid pointers and are NULL terminated

    int n = 0;

    // find the first position where str1 is different from str2
    while( str1[n] != '\0' && str2[n] != '\0' && str1[n]==str2[n] )
    {
        n++;
    }

    // truly ugly, but beginner-friendly
    if( str1[n] > str2[n] )
        return true;
    else
        return false;
}


And your comparison code would have been
if( IsGreaterThan( people[j].name, people[topIndex].name ) )
Last edited on
With your suggestions, I plugged everything in and it worked perfect. I'm still not sure of myself with the concept of cstrings, strings and arrays, but slowly and surely I am getting there. The book I have been working out of sometimes isn't always the clearest to understand. I must admit, I am currently a student, and yes this was an assignment, but I don't feel like I'm the average student just wanting someone to give code to me that works to me. I really do want to try and figure it out, just sometimes I need a little direction. I am very appreciative for your help and thank you once again. Now if I can just get a grasp on defining structures and classes and knowing when and how to use all of the member functions such as strlen, strcpy etc..
Topic archived. No new replies allowed.