alphabetizing words in c++

closed account (428bqMoL)
i made a program, used to alphabetize 10 words.
it is already running, but, even if my array is for 15 letters, it can only alphabetize 2 / 3 letter word,,
how can i fix this,,
can someone help,,
this is 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<string.h>
#include<stdio.h>
#include<conio.h>

int z,x,y=1,i=0,j=0;
char let[20][10],temp[100];
int alpha();
int arr();

main()
{
clrscr();
while(x!=10)
{
printf("Input %d: ",y);
scanf("%s",&let[x]);
x++;
y++;
}
clrscr();
printf("\nWORDS BEFORE SORTING:\n\n");
for(x=0;x!=10;x++)
{
printf("%s\n",let[x]);
}

alpha();

printf("\nWORDS AFETER SORTING:\n\n");
for(x=0;x!=10;x++)
{
printf("%s\n",let[x]);
}
getch();
}

int alpha()
{

for(i=0;i!=10;i++)
for(j=i+1;j!=10;j++)
{
if(strncmp(let[i],let[j],1)==0)
	{
	z=2;
	arr();
	strcpy(temp,let[i]);
	strcpy(let[i],let[j]);
	strcpy(let[j],temp);
	}
else if(strncmp(let[i],let[j],1)>0)
	{
	strcpy(temp,let[i]);
	strcpy(let[i],let[j]);
	strcpy(let[j],temp);
	}
}
}

arr()
{
while(z!=15)
{
if(strncmp(let[i],let[j],z)==0)
{
z++;
}
else
{
i++;
exit();
}
}
}
Last edited on
The problems I encountered when I tried to compile it are...

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<string.h>
#include<stdio.h>
#include <conio.h>

int z,x,y=1,i=0,j=0;    // do try to avoid global variables
char let[20][10],temp[100];
int alpha();
int arr();

main()
{
    clrscr();
    x = 0; // remember to initialize x, you didn't initialize it
    while(x!=10)
    {
        printf("Input %d: ",y);
        scanf("%s",&let[x]);
        x++;
        y++;
    }
    
    clrscr();
    printf("\nWORDS BEFORE SORTING:\n\n");
    for(x=0;x!=10;x++)
    {
        printf("%s\n",let[x]);
    }
    
    alpha();
    
    printf("\nWORDS AFETER SORTING:\n\n");
    for(x=0;x!=10;x++)
    {
        printf("%s\n",let[x]);
    }
    getch();
    return 0;    // don't forget the return
}

int alpha()
{
    //use braces in nested structures for less confusion. You left out a few.
    for(i=0;i!=10;i++)
    {
        for(j=i+1;j!=10;j++)
        {
            if(strncmp(let[i],let[j],1)==0)
            {
                z=2;
                arr();
                strcpy(temp,let[i]);
                strcpy(let[i],let[j]);
                strcpy(let[j],temp);
            }
            else if(strncmp(let[i],let[j],1)>0)
            {
                strcpy(temp,let[i]);
                strcpy(let[i],let[j]);
                strcpy(let[j],temp);
            }
        }
    }// end for
}// end alpha

int arr()
{
    while(z!=15)
    {
        if(strncmp(let[i],let[j],z)==0)
        {
            z++;
        }
        else
        {
            i++;
            exit(0);        // exit needs an integer parameter
        }
    }
}// end arr 


other than those, the sorting works fine on Dev C++ o.O"

P.S. and try to use indentation ^_^
Last edited on
closed account (428bqMoL)
i just got the same error, i am using turbo c++, in there the program stops in just the input section, after that, it just stops, if you press any key, the program will terminate
http://www.cplusplus.com/forum/articles/6046/
You guys should be using C++ string type, not fixed length arrays without bounds checks.


Topic archived. No new replies allowed.