Line 4:
Don't do that. Just return the choice to the main function instead of some random global variable.
20 21 22 23 24 25
|
int choose()
{
int ch;
...
return ch;
}
|
77 78
|
int ch;
ch = choose();
|
─────────────────────────
Lines 6-9: Why the extra level of indirection? You realize, also, that you are allocating but not freeing memory? BTW you are safe assuming a
char is one byte wide.
6 7 8 9 10 11 12 13
|
void swap2(char* s1, char* s2)
{
char* st = malloc( 21 );
strcpy( st, s1 );
strcpy( s1, s2 );
strcpy( s2, st );
free( st );
}
|
I also recommend you get rid of magic numbers like '21'. What if you change it below? Your swap function could be much more efficient:
6 7 8 9 10 11 12 13 14 15 16 17
|
void swap2( char* s1, char* s2 )
{
while (*s1 || *s2)
{
char c = *s1;
*s1 = *s2;
*s2 = c;
s1 += 1;
s2 += 1;
}
}
|
This function
assumes (as does your original) that both
s1 and
s2 refer to enough memory to perform the swap. The swap will end at the end of the
longer string. If you want more safety, change line 8 to read:
8 while (*s1 && *s2)
The safe was causes the swap to end at the end of the shorter string.
Also, the name of your function stinks. How about something that describes it, like "strswap" or "swap_strings" or some such?
─────────────────────────
Lines 30, 103, 106.
Don't do that. It may work for you, but your professor's compiler (any decent compiler) may not like it. Why not just read to end of line?
|
while (getchar() != '\n') ;
|
That does the right thing.
─────────────────────────
Lines 32-38. That thar's C++, not C.
32 33 34 35 36 37 38
|
void swap_ints( int* a, int* b )
{
int c;
c = *a;
*a = *b;
*b = c;
}
|
─────────────────────────
Lines 66, 68, 69: You assume that
fscanf() succeeded when you increment
baris. You need to check for read failure (or just plain EOF)
after using
fscanf() but
before incrementing to accept the new input in the array.
BTW, you are using a magic number again. Surely there is something better you can do than assume the array takes 20 elements. (Try using a constant.)
─────────────────────────
Line 76: Why are you using a
goto as a replacement for a very simple loop?
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
while (1)
{
int ch = choose();
if (ch == 1)
{
...
}
if (ch == 2)
{
...
}
if (ch == 3)
{
break;
}
}
|
─────────────────────────
Finally, you need to call your swap functions correctly.
107 108 109
|
swap_strings( team[switch1 - 1], team[switch2 - 1] );
swap_ints( &play[switch1 - 1], &play[switch2 - 1] );
...
|
Be careful with your indentation and brace style -- consistency is the key.
Hope this helps.