Hello HS05669,
I see several problems with your code.
First you need some blank lines and watch your indenting.
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
|
void doctordetails()
{
int doctordetail;
cout << "\t*************** Doctor Details *****************\n\n\n";
cout <<
"\n1. Add New Doctor"
"\n2. View Doctor Details."
"Enter choice: ";
cin >> doctordetail;
ofstream doctor;
doctor.open("doctordetails.txt", ios::app | ios::binary);
doctor.close();
system("cls");
switch (doctordetail)
{
case 1:
char name[50], field[50];
int num, age;
ofstream doctor;
doctor.open("doctordetails.txt", ios::app | ios::binary);
cin.sync();
cout << "\nEnter The Doctor Name = ";
cin.getline(name, 50);
cout << "\nEnter the Doctors Field = ";
cin.getline(field, 50);
cout << "\nEnter Age = ";
cin >> age;
cout << "\nEnter Experience(in years) = ";
cin >> num;
cout << "\nName = " << name;
cout << "\nAge = " << age;
cout << "\nExperience = " << num;
cout << "\nField = " << field;
cout << "\n\nYour Entry Has been saved ";
doctor.close();
break;
case 2:
ifstream doctor;
doctor.open("doctordetails.txt", ios::app | ios::binary);
break;
}
}
|
The blank lines make it easier to read and the first benefit is to you.
Line 6 is just another way to write the "cout" statement that is easier to visualize how what is displayed on the screen will look. Also easier to adjust or add to like I did with the last line.
Lines 12 and 14 define and open the file, but yo could also write it as
ofstream doctor("doctordetails.txt", ios::app | ios::binary);
Any how you open the file stream and then close it. Is there a reason you did this?
I also wondering why you need "binary" mode for the files?
The "switch" starts OK, although I would have used "choice" instead of "doctordetail". "choice" is more descriptive of what it is used for, but it is always your choice for variable names.
In "case 1" you start by defining variables. You can do this, but I have always found that if you do they need to be in a set of {}s.
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
|
switch (doctordetail)
{
case 1:
{
char name[50], field[50];
int num, age;
ofstream doctor("doctordetails.txt", ios::app | ios::binary);
doctor.open("doctordetails.txt", ios::app | ios::binary);
cin.sync();
cout << "\nEnter The Doctor Name = ";
cin.getline(name, 50);
cout << "\nEnter the Doctors Field = ";
cin.getline(field, 50);
cout << "\nEnter Age = ";
cin >> age;
cout << "\nEnter Experience(in years) = ";
cin >> num;
cout << "\nName = " << name;
cout << "\nAge = " << age;
cout << "\nExperience = " << num;
cout << "\nField = " << field;
cout << "\n\nYour Entry Has been saved ";
doctor.close();
}
break;
|
The only problem with this is that the variables are local to the block and lost when you reach the closing }. Best to define those variables outside the "switch".
The rest of the case looks OK except you take input from the keyboard then write what you just input to the screen, but never write to your output file before you close it.
There is no reason or advantage I can see for opening the file in the case statement. Just remove the close on line 16 and on line 50. The file stream will close when the function looses scope.
"case 2" is similar to "case 1". To define variables in the case statement you will need the {}s, but the file stream and any other variables should already be defined before the switch.
Lastly it is best to provide enough code that can be compiled and run for testing. There may be problems before you get to the function.
Aslo if there is an input file if is very helpful to share that with everyone, so that everyone can be using the same information. Also it may show problems that you did not mention or even know about yet. If the file should be large a good sample for testing will work. Do not count on someone fixing your code just to write to a file to be able to use it for input.
Andy