Swap between Chars

I make a program that can read a file and then save it to memory
The memory used to print ouput on cmd.

Swap between Integers going well(swap1 function),
but when I do swap between Chars(swap2 function)... It stopped

Could you help me please ?
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include<malloc.h>
int ch;

void swap2(char **s1,char **s2){
	char *tmp=(char*)malloc(21*sizeof(char));
	strcpy(tmp,*s1);strcpy(*s1,*s2);strcpy(*s2,tmp);
}

void diamond()
{
	printf("\n");
	for(int a = 0; a < 80; a++)
	{
	printf("%c", 4);
	}
	printf("\n");
}
void choose()
{
	printf("Lega Calcio 2010/2011\n");
	printf("=============================\n");
	printf("1. Show Score Table\n");
	printf("2. Switch Club Rank\n");
	printf("3. Exit & Write into File\n\n");

	printf("Your choice[1/2/3] : ");
	scanf("%d", &ch);
	fflush(stdin);
}
void swap1(int &a,int &b)
{
	int simpan;
	simpan = a;
	a = b;
    b = simpan;
}






int main()
{

	FILE *in;
	
	

	char team[20][21];
	int play[20];
	int win[20];
	int draw[20];
	int lose[20];
	char score[20][6];
	int point[20];
	int baris;
	int switch1;
	int switch2;

	in =  fopen("coba.ah","r");

		baris = 0;
		while(!feof(in))
		{
			fscanf(in, "%14s %d %d %d %d %s %d", team[baris], &play[baris], &win[baris], &draw[baris], &lose[baris], score[baris], &point[baris]);
			baris++;

			if(baris == 20)
			{
				break;		
			}
		}
	awal:
	choose();

	
	if(ch == 1)
	{

		
		
		printf("\n");
		printf("Final Result of Lega Calcio 2010/2011\n");
		printf("|==================================================================|\n");
		printf("|No.   Club          Play     W       D       L      Score    Point|\n");
		printf("|------------------------------------------------------------------|\n");
		for(int i = 0; i < 20; i++)
		{
			printf("| %-2d |%-13s|   %2d   |  %2d  |  %2d   |   %2d  |  %s  |  %2d |\n", i+1, team[i], play[i], win[i], draw[i], lose[i], score[i], point[i]);
			
		}
		printf("|==================================================================|\n");
		diamond();
		goto awal;
	}
	if(ch == 2)
	{
		printf("1> Input the rank that want to be switched [1..20] : ");
		scanf("%d", &switch1);
		fflush(stdin);
		printf("2> Input the rank that want to be switched [1..20] : ");
		scanf("%d", &switch2);
		fflush(stdin);
		swap2(&team[switch1 - 1], &team[switch2 -1]);
		swap1(play[switch1 - 1], play[switch2 -1]);
		swap1(win[switch1 - 1], win[switch2 - 1]);
		swap1(draw[switch1 - 1], draw[switch2 - 1]);
		swap1(lose[switch1 - 1], lose[switch2 - 1]);
		swap2(&score[switch1 - 1], &score[switch2 - 1]);
		swap1(point[switch1 - 1], point[switch2 - 1]);
		goto awal;
	}
	

	getchar();
	return 0;
}
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.
Topic archived. No new replies allowed.