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 ' to 'char'
left operand must be l-value
cannot convert from 'char' to 'char '
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.
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!
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?
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( constchar str1, constchar 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] )
// truly ugly, but beginner-friendly
if( str1[n] > str2[n] )
And your comparison code would have been if( IsGreaterThan( people[j].name, people[topIndex].name ) )
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..