How to solve identifier "gets" is undefined and not found

I have problem with "gets" in my program, it's say that my identifier "gets" is undefined and not found. can you help me solve this 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void header();
void input();
void tampil();
void indtoing();
void ingtoind();
void hapus();
void keluar();
FILE *kamus, *temp;
struct { char nama[35], telp[35]; }data; //deklarasi struct untuk inisialisasi nama field  
char kar;

int main()
{
	char pil;
	do {
		system("cls");
		header();
		puts("\n\n\n  *------MENU PILIHAN------*\n");
		printf("1. Input Data Telepon.\n");
		printf("2. Tampilkan Data Buku Telepon.\n");
		printf("3. Cari nama -> telepon.\n");
		printf("4. Cari telepon -> nama.\n");
		printf("5. Hapus kata dalam kamus.\n");
		printf("6. Keluar\n\n");
		printf(" Masukan pilihan anda = ");
		pil = _getche();
		puts("");

		switch (pil)
		{
		case '1':
			input();
			break;
		case '2':
			tampil();
			break;
		case '3':
			indtoing();
			break;
		case '4':
			ingtoind();
			break;
		case '5':
			hapus();
			break;
		case '6':
			keluar();
			break;
		}
		printf(" Kembali ke menu awal (y/n) => ");
		kar = _getche();
	} while ((kar == 'Y') || (kar == 'y'));
}



void header()
{
	puts("             ========================================");
	puts("             |                                      |");
	puts("             |            *BUKU TELEPON *           |");
	puts("             |             Created by :             |");
	puts("             |            06 achmad sayfudin        |");
	puts("             ========================================");
}

/*input kata*/
void input()
{
	char pil;
	do {
		system("cls");

		if ((kamus = fopen("kamus.txt", "a+")) == NULL)
		{
			printf("data gagal buka ");
			exit(1);
		}
		header();
		puts(" \n\t\t\t-----------------\n");
		puts(" \t\t\t INPUT DATA BARU\n");
		puts(" \t\t\t-----------------\n");
		fflush(stdin);
		printf("Nama => ");
		gets(data.nama); //untuk input data.nama 
		printf("No Telepon => ");
		gets(data.telp); // untuk input data.telp
		fflush(stdin);

		fwrite(&data, sizeof(data), 1, kamus);
		printf("\n input data lagi (y/n)");
		pil = getche();
	} while ((pil == 'Y') || (pil == 'y'));
	puts("");
}


void tampil()
{
	system("cls");
	fflush(stdin);

	if ((kamus = fopen("kamus.txt", "r")) == NULL)
	{
		printf("data gagal di baca ");
		exit(1);
	}
	header();
	puts(" \n\t\t\t------------------------\n");
	puts(" \t\t\tDaftar Data Kontak Telepon\n");
	puts(" \t\t\t--------------------------\n");
	printf("\n%-30s %-30s", "Nama       ", "Telepon      ");
	printf("\n%-30s %-30s", "-----------", "-----------\n");

	while (fread(&data, sizeof(data), 1, kamus) == 1) //membaca data dari file
		printf("\n%-30s %-30s", data.nama, data.telp);
	puts("\n");
	fclose(kamus);
}


void indtoing()
{
	char cari[35], op;
	int i = 1;

	do {
		system("cls");
		if ((kamus = fopen("kamus.txt", "r")) == NULL)
		{
			printf("data tidak dapat dibuka ");
			exit(1);
		}
		header();
		puts(" \n\t\t\t-----------------\n");
		puts(" \t\t\tNAMA - TELEPON\n");
		puts(" \t\t\t-----------------\n");
		printf("masukan nama => ");
		fflush(stdin);
		gets(cari);
		fflush(stdin);

		while (fread(&data, sizeof(data), 1, kamus) == i)
		{
			if (strcmp(cari, data.nama) == 0)//membandingkan data antara cari dengan data.nama
			{
				printf("%s = %s ", data.nama, data.telp);
				i += 1;
			}
		}
		if (i == 0)
		{
			printf(" kata \"%s\" tidak ditemukan ", cari);
		}
		fclose(kamus);
		printf("\n\n Ingin Cari lagi (y/n) => ");
		op = getche();
	} while (op == 'Y' || op == 'y');
	puts("\n");
}

void ingtoind()
{
	int i = 1;
	char cari[35], op;
	do {
		system("cls");
		if ((kamus = fopen("kamus.txt", "r")) == NULL)
		{
			printf("file tidak dapat dibuka");
			exit(1);
		}
		header();
		puts(" \n\t\t\t-----------------\n");
		puts(" \t\t\t TELEPON - NAMA    \n");
		puts(" \t\t\t-------------------\n");
		printf("masukkan no Telepon => ");
		fflush(stdin);
		gets(cari);
		fflush(stdin); //untuk menghapus data yang ada di buffer

		while (fread(&data, sizeof data, 1, kamus) == 1)
		{
			if (strcmp(cari, data.telp) == 0)
			{
				printf("%s = %s", data.nama, data.telp);
				i += 1;
			}
			else if (i == 0)
				printf("no telepon \"%s\" tak terdaftar", cari);
		}

		fclose(kamus);
		printf("\n\n Apakah Ingin Mencari lagi (y/n) => ");
		op = getche();
	} while (op == 'Y' || op == 'y');
	puts("\n");
}


void hapus()
{
	int i = 1;
	char del[35], op;
	do {
		system("cls");
		if ((kamus = fopen("kamus.txt", "r")) == NULL)
		{
			printf("tidak dapat di hapus");
			exit(1);
		}
		header();
		puts(" \n\t\t\t-----------------\n");
		puts(" \t\t\t    HAPUS DATA     \n");
		puts(" \t\t\t-------------------\n");

		printf("hapus data =>");
		fflush(stdin);
		gets(del);
		fflush(stdin);

		while (fread(&data, sizeof(data), 1, kamus) == 1)
		{
			if (strcmp(del, data.nama) != 0) //jika tidak sama
			{
				if ((temp = fopen("temp.txt", "a")) == NULL)
				{
					printf("File tidak bisa diakses");
				}
				else
				{
					fwrite(&data, sizeof(data), 1, temp);
				}
				fclose(temp);
			}
			else
			{
				printf("\"%s = %s\"\n berhasil dihapus", data.nama, data.telp);
				i += 1;
			}
		}

		if (i == 0)
		{
			printf("Kata \"%s\" tidak ditemukan", del);
		}
		fclose(kamus);
		remove("kamus.txt");
		rename("temp.txt", "kamus.txt");

		printf("\n\nhapus lagi (y/n) => ");
		op = getche();
	} while (op == 'Y' || op == 'y');
	puts("\n");
}

void keluar()
{
	exit(1);
}
Last edited on
gets is considered unsafe and has been removed from the latest C and C++ standards.

You might want to consider using fgets instead: https://en.cppreference.com/w/c/io/fgets
Last edited on
As Peter87 said, gets() has been removed from the latest standards. If you don't want to use fgets() there is gets_s().

https://en.cppreference.com/w/c/io/gets
Note that support for gets_s is optional so even though it's a standard function it's not available on all implementations.
Last edited on
i use visual studio here
thanks guys, its works :)
I'm curious, did you use fgets() or gets_s()?
Topic archived. No new replies allowed.