It's about input, store, and output data.

can someone check my program? so there's add option for this, i wanted to store multiple medical records, but when i do that it seems that it only overwrites the old data, another is that when i do the update program, it changes the old content, yes, but the it also re-prints again over again.

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
  #include<fstream> 
#include<conio.h> 
#include<string.h> 
#include<iomanip>
#include<iostream> 
#include<time.h> 
 
using namespace std;

struct check_up
             {char weight[30], height[30], bmi[30], temp [30], bloodP[30], heart[30], complications[30], findings[30], treatment[30]; 
			 };
int s;
char patient[30];

class personal
        {int number; char name[40], ad[100], bd[30], con[100], age[30], sex[30];
        public:
void add() //input personal info
{       cout<<"\nPATIENT NUMBER: "; cin>>number;
        cout<<"\nNAME: "; cin.ignore(); cin.get(name,40);
        cout<<"\nADDRESS: "; cin.ignore(); cin.get(ad,100);
        cout<<"\nBIRTHDATE: "; cin.ignore(); cin.get(bd,30);
        cout<<"\nAGE: "; cin.ignore(); cin.get(age,30);
        cout<<"\nSEX: "; cin.ignore(); cin.get(sex,30);
        cout<<"\nCONTACT NUMBER: "; cin.ignore(); cin.get(con,100);
        cout<<"\t\t\t\n\Information saved!\n"<<endl;
		system("pause");
		}

void show() //print info
	{
 		cout<<"\nPATIENT: "<<number;
   		cout<<"\nNAME: "<<name;
     	cout<<"\nADDRESS: "<<ad;
     	cout<<"\nBIRTHDATE: "<<bd;
     	cout<<"\nAGE: "<<age;
     	cout<<"\nSEX: "<<sex;
     	cout<<"\nCONTACT NUMBER: "<<con;}
		    
 
int getNumber()
    {return number;}
char* getName()
    {return name;}
char* getAddress()
    {return ad;}
char* getPhone()
    {return con;}
char* getbirthdate()
    {return bd;}
char* getage()
    {return age;}
char* getsex()
    {return sex;}
void report()
    {cout<<"\t\t"<<number<<setw(40)<<name<<endl;}
};  

fstream medical;
personal info;

void save()
{
	system("cls");
 	int option;
  	medical.open("patient.dat",ios::out|ios::app);
        info.add();
     	medical.write((char*)&info,sizeof(personal));
     medical.close();
}
void locate(int number)
{
          system("cls");
          cout<<"\n[ P E R S O N A L  I N F O R M A T I O N ]\n";
          int check=0;
          medical.open("patient.dat",ios::in);
          while(medical.read((char*)&info,sizeof(personal)))
          {
                    if(info.getNumber()==number)
                    {
                               info.show();
                              check=1;
                    }
          }
          medical.close();
          if(check==0)
          	cout<<"\n\n[PATIENT'S RECORD DOES NOT EXIST]";
        getch();
}
void update()
{
          system("cls");
          int number;
          int found=0;
          cout<<"\n\n\tPATIENT NUMBER: ";
          cin>>number;
          medical.open("patient.dat",ios::in|ios::out);
          while(medical.read((char*)&info,sizeof(personal)) && found==0)
          {
                    if(info.getNumber()==number)
                    {
                               info.show();
                               cout<<"\n\nUPDATE PERSONAL INFORMATION"<<endl;
                               info.add();
                               int pos=-1*sizeof(info); //-1 delete first file to overwrite updates
                              medical.seekp(pos,ios::cur);
                              medical.write((char*)&info,sizeof(personal));
                              cout<<"\n\n\t[SUCCESSFULLY UPDATED]";
                              found=1;
                    }
          }
          medical.close();
          if(found==0)
          cout<<"\n\n [PATIENTS'S RECORD DOES NOT EXIST]";
          getch();
}

void list()
{
          system("cls");
          medical.open("patient.dat",ios::in);
          cout<<"===================================================================\n";
          cout<<"\tPATIENT NUMBER"<<setw(20)<<"NAME"<<endl;
          cout<<"\n==================================================================\n";
          while(medical.read((char*)&info,sizeof(personal)))
          {
                    info.report();
          }
          medical.close();
          getch();
}
void main_menu()
{
time_t rawtime; //codes for current time+date//
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
	int s;
select: 
cout<< "\n\nHELLO!\n";
cout<< "\nWHAT DO YOU WANT TO DO?\n\n";
cout<<"1 RECORD NEW PATIENT\n";
cout<<"2 LOCATE EXISTING MEDICAL RECORD\n";
cout<<"3 UPDATE PATIENT'S PERSONAL INFROMATION\n";
cout<<"4 VIEW LIST OF PATIENTS\n";
cout<<"5 EXIT";
cout<<"\nSelect: ";
cin>>s;

    if(s==1)
	{save();
	system("cls");
	goto select;}
	if(s==2)
	{int number;
     system("cls");
     cout<<"\n\n\tPATIENT NUMBER: ";
     cin>>number;
     locate(number);
     system("cls");
	 goto select;}
    if(s==3)
    {update();
     system("cls");
	 goto select;}
    if(s==4)
    	{list();
	system("cls");
	goto select;}
    if(s==5)
	{exit(0);}
   
}
 

enter() //codes para sa password// 
{ string pass;
   cout<<"\n\nENTER PASSWORD:\n";
   cin>>pass;
   if(pass == "jerjer"){
      system("cls");
   }else{
      cout<< "\n\n[ACCESS DENIED]\n\n";
      system("pause");
      system("cls");
      enter();
   }}
   

int main(int argc, char *argv[])
{
	enter();
	main_menu();
	return 0;
}






Hello queue,

When I tried to compile your program there ae several errors that need fixed.

In your "#includes":

salem c wrote:
> #include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.
Not everyone has this header file available to them. "getch()" is nice and there is no C++ alternative to this.


You have included "string.h" which is fine, but you will also need the C++ "string" header file which is different from the "string.h" header file.

using namespace std; // <--- Best not to use. Enough said. Do a search here if you want more information.

It is best to avoid global variables like:
1
2
int s;
char patient[30];


Although it is easy to put your class functions inside the class it is best to define them outside the class. Eventually you would put the class definition in a header file and the class functions in a ".cpp" file.

It looks like these lines:
1
2
fstream medical;
personal info;
are to be used by the "save" function. They should be done inside the "save" function not outside. If you use "ofstream" then in the line to open the file you will not have to use "ios::out". Your code suggests that you are writing to the file in "binary" mode, but you have opened the file in "txt" mode. This could be a problem as they are two completely different file types.

In the function "locate" again you should define the stream inside the function and as an "ifstream" you would not need the "ios::in", but you do need to tell it that you are using a binary file.

In "menu" you are using a goto statement. It is best to avoid using the "goto" statements. This can be replaced with a do/while loop. When I work on it I will show you what I mean.

In your "main" function you define the parameters "argc" and "argv", but never use them. Unless you have some future use they are not needed.

Using a "binary' file type does have its advantages, but when you open the file stream you need to tell it that it is for "binary" use.

I would consider creating an object of the class in "main" and pass it to the functions that need it. Also you might consider creating a vector of the class in "main" to store the information. Just a thought for now as the "binary" file can be used in much the same way.

Once I correct the errors and see how it runs I will have a better idea of what is happening.

Hope that helps,

Andy
Hi!I'm just a beginner and i just learned most of this here in cplusplus, in youtube and i also based the format from programs i have found on the internet. I'm really trying to learn but i find it hard, but here I came up with another format... can i ask how will i show the list of the patients? and also about the updates.
I really appreciate your help! This one's really tough. Thank you again!!!!


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
#include<iostream>
#include<time.h>
#include<fstream>

using namespace std;
int number;
fstream medical;
string info;

enter() //password// 
{ string pass;
   cout<<"\n\nENTER PASSWORD:\n";
   cin>>pass;
   if(pass == "vital"){
      system("cls");
   }else{
      cout<< "\n\n[ACCESS DENIED]\n\n";
      system("pause");
      system("cls");
      enter();
   }}


main()

{int s, number;
char Patient[30]; 
struct Personal
        {char name[30], ad[30], bd[30], con[30], age[30], sex[30];};
struct Check_up
             {char weight[30], height[30], bmi[30], temp [30], bloodP[30], heart[30], complications[30], findings[30], treatment[30]; 
			 };

time_t rawtime; //codes for current time+date//
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );

enter();

Select:
cout<< "\n\nHELLO!\n";
cout<< "\nWHAT DO YOU WANT TO DO?\n\n";
cout<<"1 RECORD NEW PATIENT\n";
cout<<"2 LOCATE EXISTING MEDICAL RECORD\n";
cout<<"3 VIEW PATIENTS QUEUE\n";
cout<<"5 EXIT";
cout<<"\nSelect: ";
cin>>s;
system("cls");



if(s==1) //codes for selecting 1- ADD//
   {
    ofstream medical; //write only//
        cout<<"\n\nPATIENT NUMBER : "; 
        cin.ignore();
    gets(Patient);
        medical.open("patient.txt");
    
        Personal info;
            cout<<"\nNAME: ";gets(info.name);medical<<"\nNAME: "<<info.name;
            cout<<"\nADDRESS: ";gets(info.ad);medical<<"\nADDRESS: "<<info.ad;
            cout<<"\nBIRTHDATE:";gets(info.bd);medical<<"\nBIRTHDATE: " <<info.bd;
            cout<<"\nAGE: ";gets(info.age);medical<<"\nAGE: "<< info.age;
            cout<<"\nSEX: ";gets(info.sex);medical<<"\nSEX: " <<info.sex;
            cout<<"\nCONTACT NUMBER: ";gets(info.con);medical<<"\nCONTACT NUMBER: "<<info.con;
  system("PAUSE");
  system("cls");
  goto Select;

}

if(s==2) //codes for selecting 2//
{
fstream medical; //read+write//
    cout<<"\n\nPATIENT NUMBER : ";
    cin.ignore();
    gets(Patient);
    system("cls");
medical.open("patient.txt", ios::in); //Open for input operations//
	string info;
	int j; 
	cout<<"\n[ P E R S O N A L  I N F O R M A T I O N ]\n\n";
	while(medical.good())
	{
	getline(medical,info);
	cout<<info<<"\n";}
	medical.close();
	cout<<"\nPress [1] Add Medical Record\n";
	cout<<"        [2] Update Personal Information: ";
	cout<<"        [3] Back to menu";
	cin>>j;
        if(j==1)
              {medical.open("patient.txt", ios::out | ios::app);
			   medical<<"\n\n[ M E D I C A L  R E C O R D ]\n";
               medical<<asctime (timeinfo)<<"\n";
               	
				Check_up rec; 
               	  cin.ignore();
		          cout<<"\nWEIGHT: "; gets(rec.weight); medical<<"\nWEIGHT: "<<rec.weight<<" kg";
		       	  cout<<"\nHEIGHT: "; gets(rec.height); medical<<"\nHEIGHT: "<<rec.height<<" m";
		       	  cout<<"\nBMI: "; gets(rec.bmi); medical<<"\nBMI: "<<rec.bmi;
		       	  cout<<"\nBODY TEMPERATURE: "; gets(rec.temp); medical<<"\nBODY TEMPERATURE: "<<rec.temp<<" C";
		       	  cout<<"\nBLOOD PRESSURE: "; gets(rec.bloodP); medical<<"\nBLOOD PRESSURE: "<<rec.bloodP;
		       	  cout<<"\nHEART RATE: "; gets(rec.heart); medical<<"\nHEART RATE: "<<rec.heart;
		       	  cout<<"\nCOMPLICATIONS: "; gets(rec.complications); medical<<"\nCOMPLICATIONS: "<<rec.complications;
                  cout<<"\nFINDINGS: "; gets(rec.findings);medical<<"\nFINDINGS: "<<rec.findings;
                  cout<<"\nTREATMENT : "; gets(rec.treatment);medical<<"\nTREATMENT : "<<rec.treatment;
                
                medical.close();}
		    system("cls");
			goto Select;
    
		if(j==2)
			{//update personal record}
		if(j==3)
		    { system("cls");
				goto Select;
			}
		
		}
    

if(s==3) //codes for selecting 3- Delete//
{ //view list

}
if(s==4) //codes for selecting 4- Exit//
{ return(0);

}}}
Hello queue,

Here are some tips:

Posting in two different forums does not mean that you will get any better help. It would be better to choose one post to stick with.

As I recall C and C++ both allow up to 256 characters for a variable name. PLEASE use something more than a single letter for a variable name. Something that describes what it is or what it does. Using a good name for a variable makes the code easier to read and follow because there is no guessing what "s" is for. A good variable name makes it easy to know what it is for.

Try to avoid using "global" variables as the entire program has access to this variable and any where in the program can change its value making it difficult to find where a problem is.

Most use of "goto" statements can be avoided with the use of a do/while or while loop.

In the line char name[40], ad[100], bd[30], con[100], age[30], sex[30]; "name" is OK. "fullName" would be a better choice for the vaariable name. "ad" (should be assress) 100 seems like a bit much. Fifty might work just as well. "bd" is 50/50 I would call it "birthDay". "con" What does this mean? "contactNumber" makes more sense and it should only need a size of 17 or 18 20 at the max. "age" should be no more than three characters. "sex" only needs to be a single character to hole either "F" or "M". All of this will reduce the size of what you have to store. Think about what you are storing. There is no reason to overdo the size of an array and then only use half or less. This leaves a lot of unused space. In the end if you want forty characters for "fullName" the size should be "41" which is the forty characters for the name and one extra for the "\0" to end the string. This applies to any character array.

Your use of cin.ignore(); may be useful, but you are using it wrong. First it should follow the extraction operator (cin >>) because "cin >>" will leave a new line character in the input buffer. The second problem is that "cin.ignore();" will use the defaults of one character and the new line. One character may not always be enough to clear the input buffer and that will lead to a problem. The most common use is:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.

Another try to avoid is "system(anything);".
Now for the program.

Your original program is a good start, but it does need some small fixes. Rewriting the program is not a help. As a beginner the use of the class may be a little ahead of what you are ready for, but it is working although it could be written better, something to improve on, that does not mean that you have to completely redo the class or the program.

As I fixed the errors I had and started running it I found it did work. I would go back to that version and just fix the problems.

I did put a comment on the global variable "s" and in the menu function I defined the variable int choice{}; to take its place. You will see shortly.

For now I have not looked at char patient[30]; and why you would need this as a "global" variable.

Getting started in the program you should find this or a similar variation useful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void enter() //codes para sa password// 
{
	string pass{ "jerjer" };

	do
	{
		cout << "\n\nENTER PASSWORD: ";
		//cin >> pass;

		if (pass == "jerjer")
		{
			system("cls");
		}
		else
		{
			cout << "\n\n[ACCESS DENIED]\n\n";
			system("pause");  // <--- Best not to use "system(anything);". A prompt and "_getch()" is better, but still not the best.
			system("cls");
			//enter();
		}
	} while (pass != "jerjer");
}

This code also shows a little trick to help with debugging and testing the program. By initializing the string "pass" with the needed pass word and putting a comment on the "cin" statement you do not have to type in the pass word each time you run the program. This method is best done after you know the function works.

The do/while loop does eliminate the need for the function call to "enter()" in the else block. Something to consider is in the else block you may want to leave the program with an exit(1); instead of repeating the code until you get a correct answer.

And for the "menu" function I did this:
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
void main_menu()
{
	time_t rawtime; //codes for current time+date//
	struct tm * timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	int choice;  // <--- Added.
	bool cont{ false };  // <--- Added.

	do
	{
		cout << "\n\nHELLO!\n";
		cout << "\nWHAT DO YOU WANT TO DO?\n\n";
		cout << "1 RECORD NEW PATIENT\n";
		cout << "2 LOCATE EXISTING MEDICAL RECORD\n";
		cout << "3 UPDATE PATIENT'S PERSONAL INFORMATION\n";
		cout << "4 VIEW LIST OF PATIENTS\n";
		cout << "5 EXIT";
		cout << "\nSelect: ";
		cin >> choice;

		if (choice == 1)
		{
			save();
			system("cls");
			//goto select;
		}
		else if (choice == 2)
		{
			int number;
			system("cls");
			cout << "\n\n\tPATIENT NUMBER: ";
			cin >> number;
			locate(number);
			system("cls");
			//goto select;
		}
		else if (choice == 3)
		{
			update();
			system("cls");
			//goto select;
		}
		else if (choice == 4)
		{
			list();
			system("cls");
			//goto select;
		}
		else if (choice == 5)
		{
			cont = true;  // <--- Changed.
		}
		else
			std::cout << "\n  Invalid choice! Try again." << std::endl;

	} while (!cont);
}

In this case the do/while loop will eliminate the need for the "goto" statements. And it will stay in the loop until you enter "5".

The last function I worked on is "add", (addRecord is a better name and more descriptive of what it does). This is what I did:
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
void add() //input personal info  // <--- Changed.
{
	cout << "\nPATIENT NUMBER: ";
	cin >> number;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	cout << "\nNAME: ";
	cin.get(fullName, 40);

	cout << "\nADDRESS: "; cin.ignore();
	cin.get(address, 50);

	cout << "\nBIRTHDATE: ";
	cin.get(birthDay, 10);

	cout << "\nAGE: "; cin.ignore();
	cin.get(age, 3);

	cout << "\nSEX: "; cin.ignore();
	cin >> sex;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	cout << "\nCONTACT NUMBER: ";
	cin.get(contactNumber, 19);

	cout << "\t\t\t\nInformation saved!\n" << endl;  // <--- Changed. Removed th "\" before "I".

	system("pause");
}

The "ignore" statements are put where they should be and in the best way to use the "ignore".

Writing a line as cout<<"\nPATIENT NUMBER: "; cin>>number; is OK, but it makes it hard to read and follow. Now the compiler does not care about white space or blank lines, but the reader does. I added the blank lines to break it up a bit. Notice that some of the "cin.get()" statements have different numbers than what you started with. This is because of the changes I made in the class. I have not worked on the struct yet, but it will most likely need some changes.

I have not tested the "add" function yet with the new changes, but it should work.

I noticed in the "list" function inside the while loop you call the function "report". This works, but not necessary as you could just as easily replace the function call with cout << std::setw(14) << ' ' << number << setw(21) <<' ' << fullName << endl;. I changed this to give the output a better look. Something you will learn in time.

I noticed in your new code you put "s" inside "main". When I first said to move "s" to "main" I did not have a full understanding of what "s" was used for or where it was being used.

As a beginner you need an understanding of "scope" in a program. This should help with "scope" http://www.cplusplus.com/doc/tutorial/namespaces/

Hope that helps,

Andy
Hello queue,

When I started working on the "add" function I found that cin.getline(fullName, 40); worked better than cin.get(fullName, 40);. The only problem I had was with cin.getline(birthDay, 10);. I allowed ten characters for a birthday of "10/15/1960" plus one for the terminating null (\0). This in not something I use often, so I had to look it up. "cin.getline" will extract (n - 1) characters that are typed in and append the "\0" to the end. To get the full ten characters of the birth day you need to write cin.getline(birthDay, 11);. The other variables are large enough for this not to be a problem.

When I started into the "update" function I found some problems. It works down to the write part, but you are asking the user to enter all the information just to change one field.

I created this function:
1
2
3
4
5
6
7
8
9
10
11
void showUpdate() //print info
{
	cout << "\n 1. PATIENT: " << number;
	cout << "\n 2. NAME: " << fullName;
	cout << "\n 3. ADDRESS: " << address;
	cout << "\n 4. BIRTHDATE: " << birthDay;
	cout << "\n 5. AGE: " << age;
	cout << "\n 6. SEX: " << sex;
	cout << "\n 7. CONTACT NUMBER: " << contactNumber;
	cout << "\n 8. Exit: ";
}

With the intent that you only change the field that you need.

I need to do more checking on the "write" part as it is not working as it should.

Hope that helps,

Andy

P.S. I also had to add a set function to the class in order to make the change.
void setContactNumber(std::string line) { strcpy(contactNumber, line.c_str()); }.
Last edited on
is the program you made don't overwrite the old data when you update or add a new one?
i have used the cin.getline() before but it didn't turned out well
Hello queue,

is the program you made don't overwrite the old data when you update or add a new one?
It is not the program I made it is what you have made. I have not yet changed anything that reads or writes the file.

You have two lines of code:
1
2
3
int pos=-1*sizeof(info); //-1 delete first file to overwrite updates

medical.seekp(pos, ios::cur);

The first problem is that "pos" can not be a negative number. The least it can be is zero.

The second problem is line 3. I do not think you fully understand what "seekp" and "ios::cur" means. The first parameter is the starting point, usually this is zero, but could be something else, and the second parameter is the offset from the first parameter.

So given a file with three records and it is the third record that you want to change. By the time a match is found you have read the third record and the file pointer is at the end of the third record and just before the end of file marker. When you use "ios:cur" it is getting the position of the file pointer which is one record after what you want to change. This means that you are appending to the end of the file instead of changing what you need.

I have yet to test this, but what might work better is:
medical.seekp(pos, (ios::cur - sizeof(info))); To back up the file pointer to where you need to be.

i have used the cin.getline() before but it didn't turned out well
Just a guess, but maybe you were not using it correctly. I would have to see what you did.

Hope that helps,

Andy
Topic archived. No new replies allowed.