Help me. Always getting an error.

First of all, sorry for the messy codes. Our prof told us to make add,view,search,delete. I made all of them except delete since it's giving me an error always

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
#include <iostream>
using namespace std;

void main()
{
	FILE *fileVar;
	FILE *fileVar2;
	char name[100], course[100];
	int age,c;
	fileVar = fopen ("StudentRecord.txt", "a");
	fileVar2 = fopen ("Temporary.txt", "w");

	do
	{

	cout<<"Student"<<endl<<"1)Add"<<endl<<"2)View"<<endl<<"3)Search"<<endl<<"4)Delete)"<<endl<<"5)Exit"<<endl;
	cout<<"Choose: ";
	cin>>c;
	_flushall();
	
	

	if (c == 1)
	{	
		
		cout<<"Enter Name: ";
		gets (name);
		int wordlength = strlen(name);
		int i = 0;
		while (i < wordlength)
				{
					if (isspace (name[i]))
					{
						name[i] = '_';
						
					}
					i++;
				}
		_flushall();
		cout<<"Enter Age: ";
		cin>>age;
		_flushall();
		cout<<"Enter course: ";
		gets (course);
		_flushall();
		fprintf (fileVar, "%s,%i,%s\n", name, age, course);
		
	}
		

	if (c == 2)
	{
		FILE *fileVar = fopen ("StudentRecord.txt", "r");
		char data[100];
		
		while (!feof(fileVar))
		{
			fscanf(fileVar, "%s\n", &data);
			char *wordSplit = strtok(data, ",");

			while (wordSplit != NULL)
			{
				cout<<"Name: "<<wordSplit<<endl;
				wordSplit = strtok (NULL, ",");
				cout<<"Age: "<<wordSplit<<endl;
				wordSplit = strtok (NULL, ",");
				cout<<"Course: "<<wordSplit<<endl;
				wordSplit = strtok (NULL, ",");
				cout<<endl;
			
				
			}
		}
	}

	if (c == 3)
	{
		
		FILE *fileVar = fopen ("StudentRecord.txt", "r");
		int temp = 0;
		char name[1000];
		char data[1000];
		cout<<"Search Name: ";
		gets (name);
		
		
		while (!feof(fileVar))
		{
			
			fscanf(fileVar, "%s\n", &data);
			char *wordSplit = strtok(data, ",");
		
				
						if (strcmpi (name, wordSplit) == 0)
						{	
							temp = 1;
							break;
							
						}
						else if (strcmpi (name, wordSplit) != 0)
						{
							temp = 0;
							
						}
				
						wordSplit = strtok (NULL, ",");
				
		}
			if (temp == 1)
			{
				cout<<"File Found."<<endl;
			}
			else if (temp == 0)
			{
				cout<<"File Not Found."<<endl;
			}
	}
		
	if (c == 4)
	{
		FILE *fileVar2 = fopen ("Temporary.txt", "a");
		FILE *fileVar = fopen ("StudentRecord.txt", "r");
		char name[1000];
		char data[1000];
		char tempname[1000];
		cout<<"Search Name: ";
		gets (name);
	
		while (!feof(fileVar))
		{
			
			fscanf(fileVar, "%s\n", &data);
			char *wordSplit = strtok(data, ",");
			if (wordSplit != NULL)
						
						if (strcmpi (name, wordSplit) == 0)
						{
							continue;
						}
						else
						{	
							strcpy (tempname, wordSplit);
							fprintf (fileVar2, "%s\n", tempname);
							
						}
											
						wordSplit = strtok (NULL, ",");
		}
		fopen ("StudentRecord.txt", "w");
		while (!feof(fileVar2))
		{
			fscanf (fileVar2, "%s\n", &data);
			char *wordSplit = strtok(data, ",");
				
			while (wordSplit != NULL)
				{
					strcpy (tempname, wordSplit);
					fprintf (fileVar, "%s\n", tempname);
					wordSplit = strtok (NULL, ",");
				}

				
		}
		cout<<"Record Delete!"<<endl;
			
	}
		}while (c != 5);

}


The problem, I think, is within the temporary txt file. It's giving garbage and causing my work to break.
Topic archived. No new replies allowed.