#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class student{
int admno;
int class_rollno;
char name[30];
char mobileno[11];
float marks[5];
float fees;
public:
int getadmno(){return admno;}
int getclass_rollno(){return class_rollno;}
};
int main(){
student s1;//To read and write to the database.
int main_menu_choice,admin_menu_choice;//main_menu_choice for the main menu options.
//admin_menu_choice for the admin menu option.
char menu_choice_1;//Do..while loop in level 2 menu.
char main_menu_continue,admin_menu_1='y';//main_menu_continue to loop contents in main()
//admin_menu_1 is used with do..while loop in the admin menu.
int passkey;//stores the passkey which the user inputs.
int psk;//stores the passkey after reading it from a file.
int admin_check=0;//To check if the user has admin rights.
int newpass;//To change the passkey.
char ans;
do{
clrscr();
cout<<"\t\t\tSTUDENT DATABASE MANAGEMENT SYSTEM\n\n";
//If the passkey hasn't been entered already,
//the program will ask the user to enter one.
//The user can also change the passkey at the starting of the program only...
if(admin_check==0){
cout<<"\nDo you have the admin passkey? This will be used to access functions marked with '(*)'";
cout<<"\n1.Login\t2.Change passkey\t3.Skip.";
}
//Read the passkey file.
ifstream pass;
pass.open("pass.txt",ios::in);
//Store the passkey read from the file in a variable psk;
pass.getline(psk,40,'\n');
pass.close();
if(admin_menu_choice==1)
{
//The user may retry entering the passkey, if the entered passkey is incorrect.
do{
cout<<"\nEnter the passkey.";
cin>>passkey;
if(passkey==psk)
admin_check=1;
else{
cout<<"\nThe entered passkey is wrong! Do you want to retry?(Y-Yes||Any other key-No)";
cin>>admin_menu_1;
}
}while(admin_menu_1=='y'|admin_menu_1=='Y');
}
if(admin_menu_choice==2)
{
//Open passkey file in output mode.
//The user may change the passkey. To change the passkey, the user must
//first enter the old passkey.
do{
cout<<"\nEnter existing passkey.";
if(passkey==psk)
admin_check=1;
else{
cout<<"\nThe entered passkey is wrong! Do you want to retry?(Y-Yes||Any other key-No)";
cin>>admin_menu_1;
}while(admin_menu_1=='y'|admin_menu_1=='Y');
cout<<"\nEnter new password";
cin>>newpass;
ofstream outpass;
outpass.open("pass.txt",ios::out);
outpass<<newpass;
outpass.close();
}
cout<<"Do you want to?\n1.Access basic student information.\n2.Access secure student information.(*)\n3.Edit the database.(*)\n4.Append to the database.(*)\n0.Create a new Database-Deletes the existing database(*)\n";
cout<<"\nEnter your choice (1/2/3/4/0)\t";
cin>>main_menu_choice;
if(main_menu_choice!=0||main_menu_choice!=1||main_menu_choice!=2||main_menu_choice!=3||main_menu_choice!=4)
{
clrscr();
cout<<"\n\n\t\tYou have entered a wrong choice.\n\n";
}
if(main_menu_choice==0){
// if(admin_check!=1)//check if user has admin rights.
// {
// cout<< "\nYou do not have admin rights.\n";
// break;
// }
ofstream std(student.dat,ios::out||ios::binary);
do{
clrscr();
cout<<"\nCreating database. Old records have been removed.\n"while(ans=='y'||ans=='Y'){
std.write((char*)&s1,sizeof(s1));
cout<<"\nDo you want to enter another record?(Y-Yes||Any other key-No)";
cin>>ans;
}while(ans=='y'|ans=='Y');
std.close();
ans='n';
}
if(main_menu_choice==1){
do{
clrscr();
cout<<"\n\t\tAccess basic student information::\n";
cout<<"a.Print all data\nb.Sort the data\nc.Edit data.\nd.Delete at a paricular position\n";
cout<<"\nPlease enter your choice (a/b/c/d)\t";
cout<<"\n\nDo you want to continue?(Y-Yes||Any other key-No)\t";
cin>>menu_choice_1;
}while(menu_choice_1=='y'|menu_choice_1=='Y');
}
cout<<"\nDo you want to continue?(Y-Yes||Any other key-No)\t";
cin>>main_menu_continue;
}while(main_menu_continue=='y');
return 1;
}
Sorry, but name (project)?? Description?? How it works??? Why you sad?? What point that makes you sad??
Sorry, we (and I) currently cannot answer your general question. This requires more information to be more understandable, also you'll get more helps, suggestions and comments from other people. :)
Please post your compiler output ( the errors)
These are some of the things I can see:
In general you need to split your code into functions. There is a bit of a rule that a function should not be more than 80 lines of code. And no more than 80 chars per line of code. You should have a function that prints the menu and then another to get a response and call the appropriate function to carry out the menu option. The processing of the menu option should be a switch statement, with each case calling a function.
You have a lot of these:
}while(menu_choice_1=='y'|menu_choice_1=='Y');
You need to use the || (Or operator) not |
If you make use of the toupper function (converts a char to upper case) you won't have to test the variable twice.
I personally dislike do loops, here is a better way of quitting from something:
1 2 3 4 5 6 7 8
bool Quit = false;
while (!Quit) {
//your code
//user wants to quit
Quit = true;
}
Also with variable names - I like to use CamelCase. No underscores - they make the name too long, capitalise the first letter of each word.
Now with your objects, To process multiple students, create a student object then use push_back to put it into a vector. This way you can store all the info.
With this: if(main_menu_choice!=0||main_menu_choice!=1||main_menu_choice!=2||main_menu_choice!=3||main_menu_choice!=4)
What if you had 15 menu options? There has to be a better way. Hint - the switch statement I mentioned earlier.
Hope all goes well.
edit:
You can build up the cout in stages - it doesn't have to be all on one giant long line - like this:
1 2 3 4 5 6 7 8
cout<<"Do you want to?\n";
cout << "1.Access basic student information.\n";
cout << "2.Access secure student information.(*)\n";
cout << "3.Edit the database.(*)\n";
cout << "4.Append to the database.(*)\n";
cout << "0.Create a new Database-Deletes the existing database(*)" << endl;
cout<<"\nEnter your choice (1/2/3/4/0)\t" << endl;
You should also have an option to end the program on the menu. And as I said earlier the whole thing should be in a function.
Another tip for you : If you can't find what's wrong in your code, put some halt (block) functions (or code) at a place (where you doubt). There are many halt functions such as (simplest : return (ErrorCode)), exit(int nCode), Sleep(int nMiliseconds), ExitProcess(int nExitCode),...
If the problem is not serious (Causes crashes or not), maybe you have to check the values of important variables. Try logging the variable(s) you want at anywhere you doubt, and I think it should be detected & solved soon.
(A powerful & helpful trick from my programming experience)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#define ErrorCode 174
int main()
{
//Where you think it will occur
[...]
return ErrorCode; //Simplest for beginner
//Continue executing
////////////////////////////////////////
//Where you think the result will wrong
cout << <Variable>...
[...]
return 0; //No Error
}
And @TheIdeasMan (1214) : I agree with you completely about your code tweaking & optimization if(main_menu_choice!=0||main_menu_choice!=1||main_menu_choice!=2||main_menu_choice!=3||main_menu_choice!=4)
Should be changed to
1 2 3 4 5 6 7 8 9
switch(main_menu_choice){
case 0 : [...] break;
case 1 : [...] break;
case 2 : [...] break;
case 3 : [...] break;
case 4 : [...] break;
default : //wrong choice
[...]
}
With a loop condition... (Also my favorite code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while(1)
{
[...] //code
if([...]) //Small errors detected or unnoticeable events (Optional)
{
[...] //Do something
continue; //Prepare the next loop immediately
}
if([...]) // A critical events or serious error detected
{
[...] //Do something
break;// Break the loop immediately
}
}