alphabetic sorting

hi everyone.I want to alphabetic sorting of this program.But it didn't working.why? thank you
How could we know? Post some code
hahahah sorry::D:D I Forget :) stupid me
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
38
39
40
41
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int cmpstr(char* , char*);
    char s1[]={"kanye"};
    char s2[]={"waste"};
    int result;
    result=cmpstr(s1,s2);
    if(result==1)
    cout << "s1 bigger than s2"<<endl;
    if(result==0)
    cout<<"s1 and s2 is equal"<<endl;
    if(result==-1)
    cout<<"s2 bigger than s1"<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
   int cmpstr(char* s1,char* s2)
   {
       int result;
       int i = -1;
       do
       {
           i++;
       if(*(s1+i) > *(s2+i))
       result=1;
       break;
       if( *(s1+i) < *(s2+i) )
       result=-1;
       break;
       if( *(s1+i) == *(s2+i) )
       result = 0;
       break;
       } while (*(s1+i)!=0 || *(s2+i)!=0);
       return result;
       }
whoa whoa, way too complicated. This would be much better

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    string s1 = "kanye";
    string s2 = "waste";
    string result = max(s1,s2);
    cout << result << endl;

    return 0;
}


Also, you shouldn't declare a function within main (line 8) and there is no reason to be passing those arrays by pointer. You should initialize variables as you define them (line 25).
Last edited on
thanks for your answers but
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/ I want to make alike this. with pointers.Because i'am learning pointers
Last edited on
It is failing because your indentation is bad.

Reindented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int cmpstr(char* s1,char* s2)
{
    int result;
    int i = -1;
    do
    {
        i++;
        if(*(s1+i) > *(s2+i))
            result=1;
        break;
        if( *(s1+i) < *(s2+i) )
            result=-1;
        break;
        if( *(s1+i) == *(s2+i) )
            result = 0;
        break;
    } while (*(s1+i)!=0 || *(s2+i)!=0);
    return result;
}
Notice how the break statements do not belong to the preceding if statement? That is your error.

Try again:
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
int cmpstr(char* s1,char* s2)
{
    int result;
    int i = -1;
    do
    {
        i++;
        if(*(s1+i) > *(s2+i))
        {
            result=1;
            break;
        }
        if( *(s1+i) < *(s2+i) )
        {
            result=-1;
            break;
        }
        if( *(s1+i) == *(s2+i) )
        {
            result = 0;
            /* break; */  /* <-- this was a mistake! */
        }
    } while (*(s1+i)!=0 || *(s2+i)!=0);
    return result;
}

You could also considerably shorten it.
1
2
3
4
5
6
7
8
9
10
11
int cmpstr(char* s1,char* s2)
{
    while (*s1 && *s2)
    {
        if (*s1 < *s2) return -1;
        if (*s1 > *s2) return  1;
        s1++;
        s2++;
    }
    return 0;
}


@Tevsky
Line 8 is valid and correct C and C++: it is called a function prototype, and is required to use the function following main(). Further, choice of where functions are prototyped is often a matter of opinion and/or project style.

Further, since the arrays are, in fact, arrays of char, there is every reason in the book to be passing them as pointers. It is, actually, the way the C standard library is written when dealing with strings; to do similarly doesn't deserve criticism.

And again, 'initialize variables as you define' them isn't a hard and fast rule -- albeit a good one. In this case, you might want to point out that failing to initialize the variable leaves him open to a possible nonsense result -- since any changes for every possibility inside the loop means it could terminate before result was given value. As it is now it works...

Hope this helps.
@duoas
very very thank you.it's working xD.cool again thank you :).My indentation is bad because.I'am beginner in c++ and english :)
Topic archived. No new replies allowed.