write and read from file ?????

hi everyone i hope there is someone have some free time to check my course work
traying to do everything in the menu. can you see my mistakes?? i know there is losts......
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
     #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
#include<fstream>
  using namespace std;
//declare structure types 
  struct date{
	  int day, mounth ,year;
  };


	struct AppointmenRecord{ 
		string name;
		string description;
		date appdate;
		string time;
	       };

const int listSize = 30;    //number of records in array
int currentSize = 0;
//declare a list of records.
	AppointmenRecord AppointmenList[listSize];     //global array of records

// Functions prototypes.  
void addAppointmenRecords( );
void deleteAppointmenRecords( );
void editAppointmenRecords( );
void displayAllRecords( );
void searchAppointmenRecords( );
void readAppointmenRecords( );
void writeAppointmenRecords( );
void displayMenu(int &option);


 
void main( )
{
    int option; 
    bool endOfSession = false;
//get user action choice
   while (!endOfSession)
   {	
       displayMenu(option);

	   switch (option)
       {   
         case 1: addAppointmenRecords( );
                	 break;
	   //case 2: deleteAppointmenRecords( );
                	 break;
	   //case 3: editAppointmenRecords( );
                	 break;
         case 4: displayAllRecords( );
                	 break;
		//case 5: searchAppointmenRecords( );
					break;
		case 6: writeAppointmenRecords( );
                	 break;
		case 7: readAppointmenRecords( );
                	 break;
         case 8: system("cls");
                 	 cout<<"\nEND OF SESSION\n\n";
                	 endOfSession = true;
                	 break;
         default: cout<<endl<<"Wrong option number!! Try again\n";
			 cin.get();
       }
   }
}

void displayMenu(int &option)
{ // display menu options and choose one 

     system("cls");        //clear screen
                 
    cout<<endl<<endl;
    cout<<"\t    * APPOINTMENTS DATA MENU *"<<endl<<endl;
    cout<<"\t  1. Add An Appointmen Record(s)"<<endl;
    cout<<"\t  2. Delete An Appointmen Record(s)"<<endl;
	cout<<"\t  3. Edit An Appointmen Record(s)"<<endl;
    cout<<"\t  4. Display Appointmens Records"<<endl;
	cout<<"\t  5. Search An Appointmen Record(s)"<<endl;
	cout<<"\t  6. Write An Appointmen Record To The File"<<endl;
	cout<<"\t  7. Read An Appointmen Record From The File"<<endl;
    cout<<"\t  8. End Session"<<endl;
    cout<<endl<<setw(28)<<"Enter option number: ";

    cin>>option;
    cin.get();
}

 
void addAppointmenRecords( )
{  
        int number, i;

system("cls");         //clear screen

	cout<<"\nHow many Appointmen you wish to add? ";
	cin>>number;
	cin.get();                         //read newline character left in the buffer

	if( (number + currentSize ) <= listSize)                   //There is still room in the array	
	      for( i = 1; i<=number; i++)
           {
			cout<<"\nEnter Person name: ";
			getline(cin, AppointmenList[currentSize].name);
			cout<<"Enter Appointmen Descriptions: ";
			getline(cin, AppointmenList[currentSize].description);
			cout<<"Enter Appointmen date: ";
			cin>>AppointmenList[currentSize].appdate.day;
			cin>>AppointmenList[currentSize].appdate.mounth;
			cin>>AppointmenList[currentSize].appdate.year;
			cin.get();
			cout<<"Enter Appointmen time: ";
            getline(cin, AppointmenList[currentSize].time);
			cout<<endl;
	  		//cin.get();          //read newline character left in the buffer
			currentSize += 1;
		}
	else
cout<<"Overflow!!!! Appointmen List is full"<<endl;
         

cout<<"\nPress any key to continue"<<endl;
cin.get();     //read a character 
}


void displayHeading( )

{      
    cout<<setiosflags(ios::left);                //left justify output
	cout<<endl<<setw(20)<<"Person"<<setw(20)<< "Appointment"<<setw(12)<<"Appointment"<<setw(15)<<"Appointment"<<endl;
	cout<<setw(20)<<"Name"<<setw(20)<< "Descriptions"<<setw(12)<<"Date"<<setw(15)<<"Time\n"<<endl;
}

 
void displayAllRecords( )
{   /* print the data from the array of records under a suitable header*/
    
      int index, option,sdate;
	  system("cls");

		cout<<"\n\t1. Display For Certein Date"<<endl;
		cout<<"\t2. Display All Appointments"<<endl;
		cout<<"\nEnter Option Number: ";
		cin>>option;
		cin.get();


	//if(option = 1)
	//{
	//	system("cls");
	//cout<<"\nEnter the Date You want To Display: ";
	//cin>>sdate;
	//system("cls");
	//displayHeading();
	
	//}


	if (option = 2)
	{
	   displayHeading();
       cout<<setiosflags(ios::left);                //left justify output

       for (index = 0; index < currentSize; index++)
	{
		cout<<setw(20)<<AppointmenList[index].name;
		cout<<setw(15)<<AppointmenList[index].description;
		cout<<AppointmenList[index].appdate.day;
		cout<<"/"<<AppointmenList[index].appdate.mounth;
		cout<<"/"<<setw(12)<<AppointmenList[index].appdate.year;
		cout<<setw(12)<<AppointmenList[index].time;
		cout<<endl<<endl;
	}	   
	
		
   //give the user a chance to read the output data
  		cout<<endl<<"Press any character to continue  ";
		cin.get();          //read entered character  
		
	}
else 
		cout<<endl<<"Wrong option number!! Try again\n";
		cin.get();


}
void writeAppointmenRecords( )
{
	double value;
    //create and open an output text file

       ofstream outfile("C:\\Desktop\\AppointmenRecords.txt", ios::out);
// Check if file is opened
            if(!outfile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
                  cout<<"\nPress any key to proceed ";
	                      
	  }
  displayAllRecords( )
	  cin>>2;
   outfile<<setw(8)<<value;    //option2: write values to file in an 8 characters field.

    outfile.close( );                     // close file

}

void readAppointmenRecords( )
{
    double value;

    //create a stream and open the file 'test.txt' for input
ifstream infile("C:\\Desktop\\AppointmenRecords.txt", ios::in);
    // check if file is opened
	  if(!infile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
	                            //indicate program failed
	  }
	while (!infile.eof( ))    //eof( ) End Of File function. Returns false if end file reached
             {    infile>>value;
	      cout<<endl<<value;
              }
    cout<<endl;	
    infile.close( );           // close file
    
}



Last edited on
A couple of suggestions:

1. rather than doing this:
1
2
3
4
cout<<"\nHow many Appointmen you wish to add? ";
cin>>number;
cin.get();                         //read newline character left in the buffer
if( (number + currentSize ) <= listSize)  

take one appointment at a time, because if you have a free space for 6 appointments and the user wants to add 7 then it will fail. and there will always wasted space for 6 appointments.

2. it should be if (option == 2) instead of if (option = 2)

3. check your
writeAppointmenRecords
function. there are errors in it.
4. your read appointment function should read from the file and should update the global array of records. then you should call
displayAllRecords
to display. but i think its not doing anything of that sort.
5. the last which has some importance, use proper indentation of the code and comments. thats very important for others to read your code and your code looks beautiful.
hi thank you for replaying
this is my courswork and asking me to write this way "Ask to user how many Apointmen wish to add. so i can not change that, i made a few changes,
i can write to the file now but can not reading from file.
Display record options 1 need to display For Certein Date
actually its giving an error for illegal else witouth matching if, i could not see my mistake there.
im still workung on delete, edit and search Record options.....
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
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
#include<fstream>
  using namespace std;
//declare structure types 
  struct date{
	  int day, mounth ,year;
  };
  struct AppointmenRecord{ 
		string name;
		string description;
		date appdate;
		string time;
  };

const int listSize = 30;    //number of records in array
int currentSize = 0;
//declare a list of records.
	AppointmenRecord AppointmenList[listSize];     //global array of records

// Functions prototypes.  
void addAppointmenRecords( );
void deleteAppointmenRecords( );
void editAppointmenRecords( );
void displayRecords( );
void searchAppointmenRecords( );
void readAppointmenRecords( );
void writeAppointmenRecords( );
void displayMenu(int &option);


 
void main( )
{
    int option; 
    bool endOfSession = false;
//get user action choice
   while (!endOfSession)
   {	
       displayMenu(option);

	   switch (option)
       {   
         case 1: addAppointmenRecords( );
                	 break;
	   //case 2: deleteAppointmenRecords( );
                	 break;
	   //case 3: editAppointmenRecords( );
                	 break;
         case 4: displayRecords( );
                	 break;
		//case 5: searchAppointmenRecords( );
					break;
		case 6: writeAppointmenRecords( );
                	 break;
		case 7: readAppointmenRecords( );
                	 break;
        case 8: system("cls");
                 	 cout<<"\nEND OF SESSION\n\n";
                	 endOfSession = true;
                	 break;
         default: cout<<endl<<"Wrong option number!! Try again\n";
			 cin.get();
       }
   }
}

void displayMenu(int &option)
{ // display menu options and choose one 

     system("cls");        //clear screen
                 
    cout<<endl<<endl;
    cout<<"\t    * APPOINTMENTS DATA MENU *"<<endl<<endl;
    cout<<"\t  1. Add An Appointmen Record(s)"<<endl;
    cout<<"\t  2. Delete An Appointmen Record(s)"<<endl;
	cout<<"\t  3. Edit An Appointmen Record(s)"<<endl;
    cout<<"\t  4. Display Appointmens Records"<<endl;
	cout<<"\t  5. Search An Appointmen Record(s)"<<endl;
	cout<<"\t  6. Write An Appointmen Record To The File"<<endl;
	cout<<"\t  7. Read An Appointmen Record From The File"<<endl;
    cout<<"\t  8. End Session"<<endl;
    cout<<endl<<setw(28)<<"Enter option number: ";

    cin>>option;
    cin.get();
}

 
void addAppointmenRecords( )
{  
        int number, i;

system("cls");         //clear screen

	cout<<"\nHow many Appointmen you wish to add? ";
	cin>>number;
	cin.get();                         //read newline character left in the buffer

	if( (number + currentSize ) <= listSize)                   //There is still room in the array	
	      for( i = 1; i<=number; i++)
           {
			cout<<"\nEnter Person name: ";
			getline(cin, AppointmenList[currentSize].name);
			cout<<"Enter Appointmen Descriptions: ";
			getline(cin, AppointmenList[currentSize].description);
			cout<<"Enter Appointmen date: ";
			cin>>AppointmenList[currentSize].appdate.day;
			cin>>AppointmenList[currentSize].appdate.mounth;
			cin>>AppointmenList[currentSize].appdate.year;
			cin.get();
			cout<<"Enter Appointmen time: ";
            getline(cin, AppointmenList[currentSize].time);
			cout<<endl;
	  		          
			currentSize += 1; //update CurrentSize
		}
	else
cout<<"Overflow!!!! Appointmen List is full"<<endl;
         

cout<<"\nPress any key to continue"<<endl;
cin.get();     //read a character 
}

void displayHeading( )

{      
    cout<<setiosflags(ios::left);                //left justify output
	cout<<endl<<setw(20)<<"Person"<<setw(20)<< "Appointment"<<setw(12)<<"Appointment"<<setw(12)<<"\t  Appointment"<<endl;
	cout<<setw(20)<<"Name"<<setw(20)<< "Descriptions"<<setw(12)<<"Date"<<setw(15)<<"\t  Time\n"<<endl;
}

 
void displayRecords( )
{   /* print the data from the array of records under a suitable header*/
    
      int index, option,sdate;
	  system("cls"); //clear screen

		cout<<"\n\t1. Display For Certein Date"<<endl; //Display the records for certein Date.
		cout<<"\t2. Display All Appointments"<<endl;
		cout<<"\nEnter Option Number: ";  //Enter option
		cin>>option;
		cin.get();


	if(option == 1)
	
		system("cls");  //clear screen
	cout<<"\nEnter the Date You want To Display: ";
	cin>>sdate;
	system("cls"); //clear screen
	displayHeading( );

	if (option == 2)

	   displayHeading();
       cout<<setiosflags(ios::left);                //left justify output

       for (index = 0; index < currentSize; index++)
	{
		cout<<setw(20)<<AppointmenList[index].name;
		cout<<setw(15)<<AppointmenList[index].description;
		cout<<AppointmenList[index].appdate.day;
		cout<<"/"<<AppointmenList[index].appdate.mounth;
		cout<<"/"<<setw(12)<<AppointmenList[index].appdate.year;
		cout<<setw(12)<<AppointmenList[index].time;
		cout<<endl<<endl;
	}	   
	
		
   //give the user a chance to read the output data
  		cout<<endl<<"Press any character to continue  ";
		cin.get();          //read entered character  
		
	
		else 
			cout<<endl<<"Wrong option number!! Try again\n";
			cin.get();


}
void writeAppointmenRecords( )
{
	int index;
    //create and open an output text file

       ofstream outfile("C:\\Documents and Settings\\Administrator\\Desktop\\AppointmenRecords.txt", ios::out);
// Check if file is opened
            if(!outfile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
         cout<<"\nPress any key to proceed ";
		 cin.get();                 
	  }
		
	for (index=0; index<currentSize; index++)
	{
        outfile<<AppointmenList[index].name;
		outfile<<setw(15)<<AppointmenList[index].description;
		outfile<<setw(15)<<AppointmenList[index].appdate.day;
		outfile<<"/"<<AppointmenList[index].appdate.mounth;
		outfile<<"/"<<AppointmenList[index].appdate.year;
		outfile<<setw(15)<<AppointmenList[index].time<<endl;
		
	}  
     //write values to file in an 15 characters field.
	
    outfile.close( );    // close file
    cin.get();

}

void readAppointmenRecords( )

{  
	currentSize=0;
	//create a stream and open the file 'AppointmenRecords.txt' for input

	ifstream infile("C:\\Documents and Settings\\Administrator\\Desktop\\AppointmenRecords.txt", ios::in);
		// check if file is opened
		  if(!infile)          //return true if file is not opened
	  {  cout<<"\nFailed to open file!!!!\n";
	                            //indicate program failed
	  cin.get();
	  }
	while (!infile.eof( ))    //eof( ) End Of File function. Returns false if end file reached
       {
		getline(infile, AppointmenList[currentSize].name);
		getline(infile, AppointmenList[currentSize].description);
		infile.get();
		infile>>AppointmenList[currentSize].appdate.day;
		infile>>AppointmenList[currentSize].appdate.mounth;
		infile>>AppointmenList[currentSize].appdate.year;
		getline(infile, AppointmenList[currentSize].time);
		currentSize+=1;
        }
    infile.close( ); // close file
	currentSize = currentSize -1;
	cin.get();
    
}


actually its giving an error for illegal else witouth matching if, i could not see my mistake there.

line 180 has error. where is its matching if??

i can write to the file now but can not reading from file.

you cannot read from the created file because you are writing everything in one line, there is no new line.
and when you try to read from file you do getline() for each entry, but there are no lines actually. I can exactly tell when i look at the created file. can you post your out file.

you are not using braces in if-else statements!!

instead of this:
if(option == 1)

system("cls"); //clear screen
cout<<"\nEnter the Date You want To Display: ";
cin>>sdate;
system("cls"); //clear screen
displayHeading( );


i think this should be like this..
1
2
3
4
5
6
7
8
if(option == 1)
{	
	system("cls");  //clear screen
	cout<<"\nEnter the Date You want To Display: ";
	cin>>sdate;
	system("cls"); //clear screen
	displayHeading( );
}

am i correct??
line 180 has error. where is its matching if??

how should i use the else then cos i dont know why its saying its wrong??

actually its writign to the file ok, not on the same line
zafer lunch 12/12/2009 12:12
zafer lunch 13/12/2009 12:12

Topic archived. No new replies allowed.