The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
strcmp compares two strings, if I'm not mistaken... Right?
So saying strcmp(name[x],name[x+1]) is just going to test to see if each of the names are the same or different. If they're the same it will be 1(true), if they are different you're going to get a 0 (false). Since none of the names are the same the output for the if statement is going to stay at 0, and completely skips it.
What you want to do is test each letter in the character string (that's how I would do it, but others may have a better suggestion).
Test the first letter in the first name against the first letter in the second name.
So: name[0][0] would = "L" and name[1][0] = "G", and test which is greater or less. Then switch when necessary. Hope this makes sense. Someone else will probably make a better post, but hopefully I got you thinkin.
urahonky on
0
SmasherStarting to get dizzyRegistered Userregular
edited December 2007
You have name[x] and temp switched in the first strcpy in the inner loop. You were copying from temp into name[x], and since temp is uninitialized you naturally get garbage.
Also, your loop conditions are a little off (although technically they work in this case). You're not iterating through the letters of each word, so there's no reason for y to go up to 10. What you want to have happen is have x go from index 0 to #strings - 2, and inside that loop have y go from 0 to #strings - 1.
Smasher on
0
SmasherStarting to get dizzyRegistered Userregular
strcmp compares two strings, if I'm not mistaken... Right?
So saying strcmp(name[x],name[x+1]) is just going to test to see if each of the names are the same or different. If they're the same it will be 1(true), if they are different you're going to get a 0 (false). Since none of the names are the same the output for the if statement is going to stay at 0, and completely skips it.
What you want to do is test each letter in the character string (that's how I would do it, but others may have a better suggestion).
Test the first letter in the first name against the first letter in the second name.
So: name[0][0] would = "L" and name[1][0] = "G", and test which is greater or less. Then switch when necessary. Hope this makes sense. Someone else will probably make a better post, but hopefully I got you thinkin.
No, that's not how strcmp works. It returns 0 if the strings are equal, a number greater than 0 if str1 is lexigraphically greater than str2, and a number less than 0 if str2 is lexigraphically greater than str1.
There's another problem I missed before; In addition to the changes I made before, you either need to make y your outer loop and x your inner loop, or replace all the occurances of x in the inner loop with y.
Smasher on
0
ASimPersonCold...... and hard.Registered Userregular
edited December 2007
Also, if you're using a static initializer list for the that array of strings, you could just do:
Not recommended in this application, as the OP expects his char * to point to non-const char[10] arrays (I assume this based on the strcpy/temp variable). I believe char *names[] will give you an array of pointers to variable length const strings.
Posts
So saying strcmp(name[x],name[x+1]) is just going to test to see if each of the names are the same or different. If they're the same it will be 1(true), if they are different you're going to get a 0 (false). Since none of the names are the same the output for the if statement is going to stay at 0, and completely skips it.
What you want to do is test each letter in the character string (that's how I would do it, but others may have a better suggestion).
Test the first letter in the first name against the first letter in the second name.
So: name[0][0] would = "L" and name[1][0] = "G", and test which is greater or less. Then switch when necessary. Hope this makes sense. Someone else will probably make a better post, but hopefully I got you thinkin.
Also, your loop conditions are a little off (although technically they work in this case). You're not iterating through the letters of each word, so there's no reason for y to go up to 10. What you want to have happen is have x go from index 0 to #strings - 2, and inside that loop have y go from 0 to #strings - 1.
No, that's not how strcmp works. It returns 0 if the strings are equal, a number greater than 0 if str1 is lexigraphically greater than str2, and a number less than 0 if str2 is lexigraphically greater than str1.
There's another problem I missed before; In addition to the changes I made before, you either need to make y your outer loop and x your inner loop, or replace all the occurances of x in the inner loop with y.
Not recommended in this application, as the OP expects his char * to point to non-const char[10] arrays (I assume this based on the strcpy/temp variable). I believe char *names[] will give you an array of pointers to variable length const strings.