TXT file got edited

closed account (2A54jE8b)
Hey guys..i really need your help.Okay,as you can see,in my void Function AddMembers i was trying to store the particulars and info of the person who wish to be a member of CINESHOW into MEMBER.TXT file.Well,everything went smoothly and the particulars are being stored under MEMBER.TXT as follows.

Now,the problem here is when i run void function Loan the second time, the MEMBER.TXT file somehow got edited and it cant run through the second time. When i opened the TXT file, there's an extra character appearing infront of what is being copied into MEMBER.TXT file before.

For example,
Before running through Loan the second time S1234567G.
After running through Loan the second time, SS1234567G

It would be great if some kind souls out there can rectify the problem and reply to my message.
Thanks.

Here are the codes:

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <Windows.h>
using namespace std;
//C++ Libraries to be used and read by preprocessor.

struct MEMBERINFO
{
    char mID[10];//member NRIC
    char mJoined[9];//date joined
    char mContact[9];//member HP
    char mADD[41];//member Address
};
struct MOVIEINFO
{
    char MID[6];
    char MTitle[21];
    float MCost;
    char MType;
    char MDesc[101];
};
struct LOANINFO
{
    char LID[10];
    char LMID[6];
    char LDDate[9];
};
     
bool checkduplicateid(MEMBERINFO member[],int no,char id[])
{
	for(int b=0;b<no;b++)
	{
		if(strcmp(member[b].mID,id)==0)
		{
			cout<<"Error! "<<member[b].mID<<" exists.\nPlease try again."<<endl;
			return false;
		}
	}
	return true;
}

bool checkduplicatehp(MEMBERINFO member[],int no,char hp[])
{
	for(int b=0;b<no;b++)
	{
		if(strcmp(member[b].mContact,hp)==0)
		{
			cout<<"Error! "<<member[b].mContact<<" exists.\nPlease try again."<<endl;
			return false;
		}
	}
	return true;
}
  
void ADDMEMBER(MEMBERINFO member[],int no)
{
    int a=0,b=1,day,month,year,year1,year2,year3,year4;
    char id[255],date[9],contact[255],add[255];
    ofstream outFile;
    outFile.open("MEMBER.txt",ios::out);
    if(outFile.fail())
    {
        cout<<"Error! MEMBER.TXT file cannot be created.\n";
        exit(1);
    }//Error checking is mandatory.
    for(int i=0;i<no;i++)
    {
        do
        {
            cout<<"\nMEMBER #"<<i+1<<endl;
            cout<<"-----------"<<endl;
            cout<<"NRIC:";
            cin.getline(id,255,'\n');
        }
        while((strlen(id)>10)||(isdigit(id[0]))||(isdigit(id[8])));
        while(b<8)
        {
            if(!isdigit(id[b]))
            {
                cout<<"Invalid NRIC number.\nPlease try again.";
                return;
            }
            else
            {
                b++;
            }
        }
        int k=i-1;
        while(k>=0 && strcmp(member[i].mID,member[k].mID)==0)
        {
            k--;
        }
        if(checkduplicateid(member,i,id)==false)
        {
            i--;
 	        return;
  	    }
        strcpy(member[i].mID,id);    
        SYSTEMTIME ST;
        GetSystemTime(&ST);
        day=ST.wDay;
        month=ST.wMonth;
        year=ST.wYear;
        year1=year/1000;
        year2=year/100-year1*10;
        year3=year/10-year2*10-year1*100;
        year4=year-year3*10-year2*100-year1*1000;
        date[0]=day/10+'0';
        date[1]=(day%10)+'0';
        date[2]='-';
        date[3]=month/10+'0';
        date[4]=(month%10)+'0';
        date[5]='-';
        date[6]=year3+'0';
        date[7]=year4+'0';
        date[8]='\0';
        cout<<"Date Joined:"<<date<<endl;
        strcpy(member[i].mJoined,date);
        do
        {
            cout<<"Mobile Number:";
            cin.getline(contact,255,'\n');          
        }
        while(strlen(contact)>8);
        while(a<8)
        {
            if(!isdigit(contact[a]))
            {
                cout<<"Invalid number.\nPlease try again.";
                return;
            }
            else
            {
                a++;
            }
        }                
        int j=i-1;//k is the number of current records
  	    while(j>=0 && strcmp(member[i].mContact,member[j].mContact)==0)
  	    {
            j--;
        }
  	    if(checkduplicatehp(member,i,contact)==false)
  	    {
  	        i--;
      	    return;
  	    }
        strcpy(member[i].mContact,contact);
        do
        {
            cout<<"Address:";
            cin.getline(add,255,'\n');
        }
        while(strlen(add)>40);
        strcpy(member[i].mADD,add);
                     
        outFile<<member[i].mID<<",";
        outFile<<member[i].mJoined<<",";
        outFile<<member[i].mContact<<",";
        outFile<<member[i].mADD<<endl;
    }       
    outFile.close();//Closing of file is mandatory.          
}
void Display(MOVIEINFO movie[])
{
    ifstream InFile;
    InFile.open("Movie.txt",ios::in);
    if(InFile.fail())
    {
        cout<<"Movie.txt file cannot be opened.";
        exit(1);
    }
    int a=0;
    while(!InFile.eof())
    {
        InFile.getline(movie[a].MID,6,',');
        InFile.getline(movie[a].MTitle,21,',');
        InFile>>movie[a].MCost;
        InFile.ignore(256,',');
        InFile>>movie[a].MType;
        InFile.ignore(256,',');
        InFile.getline(movie[a].MDesc,101,'\n');
        a++;
    }
    cout<<"\nMOVIES AVAILABLE"<<endl;
    cout<<"-----------------"<<endl;
    cout<<setiosflags(ios::left)<<setw(6)<<"ID"<<setw(20)<<"Movie Title"<<setw(8)<<"Price"
	<<setw(6)<<"Type"<<resetiosflags(ios::left);
    cout<<"\n-----------------------------------------------------------"<<endl;
    for(int i=0;i<a;i++)
    {
        cout<<setiosflags(ios::left)<<setw(6)<<movie[i].MID<<setw(20)<<movie[i].MTitle
            <<setw(8)<<fixed<<setprecision(2)<<movie[i].MCost
            <<setw(6)<<movie[i].MType<<endl<<movie[i].MDesc<<endl
	    <<endl<<resetiosflags(ios::left);
    }
    InFile.close();    
}

void Loan(MEMBERINFO member[])
{
    ifstream inFile;
    inFile.open("MEMBER.txt",ios::out);
    if(inFile.fail())
    {
        cout<<"Error! MEMBER.TXT file cannot be opened.\n";
        exit(1);
    }//Error checking is mandatory.
    int a=0;
    while(!inFile.eof())
    {
        inFile.getline(member[a].mID,10,',');
        inFile.getline(member[a].mJoined,9,',');
        inFile.getline(member[a].mContact,9,',');
        inFile.getline(member[a].mADD,41,'\n');
        a++;
    }
    /*for(int i=0;i<lno;i++)
    {
        do
        {
            cout<<"\nLOAN #"<<i+1<<endl;
            cout<<"--------"<<endl;            
            cout<<"NRIC:";
            cin.getline(lID,255,'\n');
        }
        while((strlen(lID)>10)||(isdigit(lID[0])||isdigit(lID[8])));
        while(b<8)
        {
            if(!isdigit(lID[b]))
            {
                cout<<"Invalid NRIC number.\nPlease try again.";
                return;
            }
            else
            {
                b++;
            }
        }
        /*if(checkIDexist(member,i,lID)==false)
        {
            i--;
            return;
        }
        strcpy(mloan[i].LID,lID);   
    }*/
    inFile.close();     
}
                
int main()
{
    MEMBERINFO member[10];
    MOVIEINFO movie[10];
    LOANINFO mloan[10];
    int num,no,lno;
    char rspe;
    do
    {
        cout<<"Welcome to CINESHOP MENU";
        cout<<"\n------------------------";
        cout<<"\n1.Add Members";
        cout<<"\n2.Display Movies";
	    cout<<"\n3.Loan";
        cout<<"\n-----------------";
        cout<<"\nWhich of the following do you wish to attempt?(1-4):";
        cin>>num;
        cin.ignore(255,'\n');
        switch(num)
        {
            case 1:
                {
                    cout<<"How many member(s) do you wish to create:";
                    cin>>no;
                    cin.ignore(255,'\n');
                    ADDMEMBER(member,no);
                    break;
                }
            case 2:
                {
                    Display(movie);
                    break;
                }
            case 3:
                {
                    /*cout<<"How many loan(s) do you wish to make:";
                    cin>>lno;
                    cin.ignore(255,'\n');*/
                    Loan(member);
                    break;
                }        
            default:
                {
                    cout<<"Number keyed in wasn't 1!";
                    cout<<"\nPlease follow the instruction and try again.";
                }
        }
        cout<<"\nDo you wish to try again(Y/N)?";
        cin>>rspe;
        cin.ignore(255,'\n');
        cout<<endl;
    }
    while((rspe=='Y')||(rspe=='y'));
    return 0;
}     
Last edited on
1. Use codetags "[code]*code here*[\code]" but change the backslash to a fowardslash

edit: should be more specific, sorry.
Last edited on
And 2. pleasedontramwordstogetherinyourfunctionnames. itmakesthemveryhardtoread.

One of these is better:

bool checkDuplicateId( ... ) {}
bool CheckDuplicateId( ... ) {}
bool check_duplicate_id( ... ) {}

Whitespace please!
closed account (2A54jE8b)
sorry im new here..ermm edited. anyone can find out the problem? sorry for my poor explanation if you dont understand..will be best if you copy and paste the codes to know the problem im facing.

Last edited on
Have you tried using the ifstream::ate setting. That should put the position indicator at the end of the stream when you open it.
closed account (2A54jE8b)
Err sorry..but i dont get what u mean..sorry im a C++ newbie here..is it okay if u could tell me what to add on to my codes?

I don't get it man, Function Display and Loan are the same, that is both copy from file to memory and cout to screen. When i run Display again, it works but when it comes to Function Loan, it just either exit straight away or pop out error and application need to close. reason being the MEMBER TXT file after going through the first round somehow got edited unknowingly..

why?
If you look when you're opening the files, in Display you're using the ios::in setting and in Loan you're using the ios::out setting. (Line 165 and line 205).

I was saying that if you're writing to a file through multiple function calls. Maybe you could use the ios::ate setting. It's supposed to set the indicator at the end of the stream, and I think that would fix your problem.

idk if it will work though, I'm a C++ newbie myself. I do C a lot, but I'm just learning C++
It's at least something to try though.

btw- I believe you would have to use the ios::ate and the ios::in to write to the file.

edit: spelelling :-)
Last edited on
It didn't do the duplicated first letter thing for me when I tried it.

Agree with jpeg - every time you open the member text file and write to it - it will always start over-writing what is there.
You should open it for appending (so that you add to what is already in the file) like this:
1
2
3
4
void Loan(MEMBERINFO member[])
{
    ifstream inFile;
    inFile.open("MEMBER.txt",ios::out|ios::app);


The way theexit(1) are used are not a good way for your program to exit.
It just flashes and goes away - there's no chance of the user reading
the error messages - and it gives the impression that the program has crashed out.
closed account (2A54jE8b)
A big thank you to jpeg and guestgulkan for your help..problem solved! :D
Topic archived. No new replies allowed.