/* write a program to sort names entered in an array in ascending order */
When i enter the names into the array the program halts .Anyone knows why?
#include<stdio.h>
#include<string.h>
void main(void)
{
/* write a program to sort names entered in an array in ascending order */
int in,out,i,x;
char temp[30],string2d[5][30];
printf("ENTER NAMES IN THE ARRAY:");
for(i=0;i<5 ;i++)
{
gets(string2d[i]);
}
for(out=0;out<5-1;out++)
{
for(in=out+1;out<5;in++)
{
x=strcmpi(string2d[out],string2d[in]);
if(x>1)
{
strcmpi(temp,string2d[out]);
strcmpi(string2d[out],string2d[in]);
strcmpi(string2d[in],temp);
}
}
}
for(i=0;i<5;i++)
{
puts(string2d[i]);
}
getch();
}
i have seen the comments and made changes to the real program but the program still loops between the in loop and the i
I suspect the infinite loop:
Your loop condition
out < 5is never changed, I suspect you meantin < 5.Also as previously mentioned, you are probably using
strcmpiinstead ofstrcpy.In addition
strcmp*return an integer either lesser than, equal to, or greater than0, your code compares this against1