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