Issues with if loop and strcmp function
Mar 30, 2013 at 11:31pm Mar 30, 2013 at 11:31pm UTC
I am almost done with this program except for one last issue! I have managed to isolate the problem to the if loop in the sort function, but I am unable to find any problems? However, all I am getting back is a blank window, the program never gets out of the void function.
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char [][11]);
main()
{
char state[3][11];
char str1[]="Florida" ;
char str2[]="Oregon" ;
char str3[]="California" ;
char str4[]="Georgia" ;
strcpy(state[0], str1);
strcpy(state[1], str2);
strcpy(state[2], str3);
strcpy(state[3], str4);
sort(state);
printf("%s\n" , state[0]);
printf("%s\n" , state[1]);
printf("%s\n" , state[2]);
printf("%s\n" , state[3]);
system("PAUSE" );
return 0;
}
void sort (char Str[][11])
{
int i=1;
char temp[3][10];
int flag=1;
int d=4;
while (flag =1 )
{
flag=0;
d=(d+1)/2;
for (i=0; i<4-d; i++)
{
if ( strcmp(Str[i+d],Str[i])>0)
{
*temp[0]=*Str[i+d];
*Str[i+d]=*Str[i];
*Str[i]=*temp[0];
flag=1;
}
}
}
return ;
}
Mar 31, 2013 at 12:14am Mar 31, 2013 at 12:14am UTC
42: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Mar 31, 2013 at 12:27am Mar 31, 2013 at 12:27am UTC
I don't quite understand? There are already parenthesis around the truth value?
Mar 31, 2013 at 12:28am Mar 31, 2013 at 12:28am UTC
Line 10 should look like this char state[4][11];
and line 42 should look like this while (flag ==1 )
Mar 31, 2013 at 12:32am Mar 31, 2013 at 12:32am UTC
Thanks for the help! It now compiles properly, but however it won't sort?
It just outputs
Florida
Oregon
California
Georgia
and the goal of the program is to get them alphabetized!
Here is the updated program
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char [][11]);
main()
{
char state[4][11];
char str1[]="Florida" ;
char str2[]="Oregon" ;
char str3[]="California" ;
char str4[]="Georgia" ;
strcpy(state[0], str1);
strcpy(state[1], str2);
strcpy(state[2], str3);
strcpy(state[3], str4);
sort(state);
printf("%s\n" , state[0]);
printf("%s\n" , state[1]);
printf("%s\n" , state[2]);
printf("%s\n" , state[3]);
system("PAUSE" );
return 0;
}
void sort (char Str[][11])
{
int i=1;
char temp[3][10];
int flag=1;
int d=4;
while (flag==1)
{
flag=0;
d=(d+1)/2;
for (i=0; i<4-d; i++)
{
if ( strcmp(Str[i+d],Str[i])>0)
{
*temp[0]=*Str[i+d];
*Str[i+d]=*Str[i];
*Str[i]=*temp[0];
flag=1;
}
}
}
return ;
}
Mar 31, 2013 at 1:04am Mar 31, 2013 at 1:04am UTC
Something like this
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char [][11]);
int main()
{
char state[4][11];
char str1[]="Florida" ;
char str2[]="Oregon" ;
char str3[]="California" ;
char str4[]="Georgia" ;
strcpy(state[0], str1);
strcpy(state[1], str2);
strcpy(state[2], str3);
strcpy(state[3], str4);
sort(state);
printf("%s\n" , state[0]);
printf("%s\n" , state[1]);
printf("%s\n" , state[2]);
printf("%s\n" , state[3]);
system("PAUSE" );
return 0;
}
void sort (char Str[][11])
{
int i= 0;
int j = 0;
char temp[11];
for (j = 0; j < 4; j++)
{
for (i=0; i<4; i++)
{
if ( strcmp(Str[i], Str[i+1]) > 0)
{
strcpy(temp,Str[i]);
strcpy(Str[i], Str[i+1]);
strcpy(Str[i+1], temp);
}
} /*End inner for loop*/
} /*End outer for loop*/
}
Topic archived. No new replies allowed.