Hey? Can someone help me to this program? I need to Search an specific information of a student.

866 Views
#include<iostream>
#include<fstream>
#include<istream>
#include<stdlib.h>
#include<string>

using namespace std;

void login(),registr(),form(),Add(),ShowList(),Search(),Update();

struct Student
{
	int id;
	char name[25];
	char name1[25];
	char address[25];
	char city[25];
	int age;
	char gender[5];
	string email;
	string contact;
	
	char course[25];
	
	char gname[25];
	char gname1[25];
	
};

Student s;

int main()
{
	system("cls");
	int o; 
	
	m:
	cout<<"Admission System"<<endl;
	cout<<"1. Login"<<endl;
	cout<<"2. Register"<<endl;
	cout<<"3. Log Out"<<endl;
	cout<<endl;
	
	cout<<"Enter operation: ";
	cin>>o;
	
	switch(o)
	{
		case 1:
		{
			login();
		break;
		}
		case 2:
		{
			registr();
			break;
		}
		case 3:
		{
			exit(0);
			break;	
		}
		default:
		{
			goto m;
		}
	}
	
}
void login() //Login Form
{
	system("cls");
	
	string user,pass,u,p;
	int count=0; //Stand as a true or false
	
	for(int x=0;x<3;x++)
	{
		system("cls");
		cout<<"Username : ";
		cin>>user;
		cout<<"Password : ";
		cin>>pass;
		
		ifstream input("adminlogin.txt");
		while(input>>u>>p)
		{
			if(u==user&&p==pass)
			{
				count=1; //True
			}	
		}
		if(count==1)
		{
			system("cls");
			cout<<"Access Granted...";
			cin.get();
			cin.get();
			form();
			break;
		}
		system("cls");
		cout<<"Access denied...";
		cin.get();
		cin.get();
		
	}
	
}
void registr() //Registration Form
{
	system("cls");
	
	string reguser,regpass,ru,rp;
	
	cout<<"New Username : ";
	cin>>reguser;
	cout<<"New Password : ";
	cin>>regpass;
	
	ofstream reg("adminlogin.txt",ios::app);
	reg<<reguser<<" "<<regpass<<endl;
	
	cout<<"Account has been registered...";
	cin.get();
	cin.get();
	
	
	login();
}
void form()
{
	system("cls");

	int o;
	
	
	cout<<"Admission Form"<<endl;
	cout<<"1. Add Student"<<endl;
	cout<<"2. Show List of Student"<<endl;
	cout<<"3. Search Student"<<endl;		
	cout<<"4. Update Student"<<endl;
	cout<<"5. Log Out"<<endl;
	cout<<"Enter operation: ";
	cin>>o;
	
	switch(o)
	{
		case 1:
		{
		Add();
		break;		
		}
		
		case 2:
		{
		ShowList();
		break;
		}
		
		case 3:
		{
			Search();
			break;
		}
		
		case 4:
		{
			Update();
			break;
		}
		
		case 5:
		{
			exit(0);
			break;
		}
	}
}
void Add()
{
	system("cls");
	
			cout<<"\n\tEnter student ID : ";
			cin>>s.id;
			cout<<"\n\tEnter student first name : ";
			cin>>s.name;
			cout<<"\n\tEnter student last name : ";
			cin>>s.name1;
			cout<<"\n\tEnter address : ";
			cin>>s.address;
			cout<<"\n\tEnter City : ";
			cin>>s.city;
			cout<<"\n\tEnter Age : ";
			cin>>s.age;
			cout<<"\n\tEnter Gender : ";
			cin>>s.gender;
			cout<<"\n\tEnter Email : ";
			cin>>s.email;
			cout<<"\n\tEnter Contact Number : ";
			cin>>s.contact;
			cout<<"\n\tEnter Course : ";
			cin>>s.course;
			cout<<"\n\tGuardians First Name : ";
			cin>>s.gname;
			cout<<"\n\tGuardians Last Name : ";
			cin>>s.gname1;
			
			ofstream sdata;
			sdata.open("studentxp.txt", ios::app);
			sdata<<endl;
			sdata<<s.id;
			sdata<<endl;
			sdata<<s.name;
	    	sdata<<endl;
			sdata<<s.name1;	
			sdata<<endl;
			sdata<<s.address;
			sdata<<endl;
			sdata<<s.city;
			sdata<<endl;
			sdata<<s.age;
			sdata<<endl;
			sdata<<s.gender;
			sdata<<endl;
			sdata<<s.email;
			sdata<<endl;
			sdata<<s.contact;
			sdata<<endl;
			sdata<<s.course;
			sdata<<endl;
			sdata<<s.gname;
			sdata<<endl;
			sdata<<s.gname1;
			
			sdata<<endl;
			sdata<<endl;
			
	sdata.close();	
	
}
void ShowList()
{
      ifstream dataFile("studentxp.txt");
      dataFile>>s.id;
	 dataFile>>s.name;
  	dataFile>>s.name1;	
  	dataFile>>s.address;
  	dataFile>>s.city;
  	dataFile>>s.age;
      dataFile>>s.gender;
      dataFile>>s.email;
      dataFile>>s.contact;
      dataFile>>s.course;
      dataFile>>s.gname;
      dataFile>>s.gname1;
      
    while (!dataFile.fail() && !dataFile.eof() )
    {
          			cout<<"\n\n\tStudent ID is : "<<s.id;
				cout<<"\n\tStudent Name : "<<s.name<<" "<<s.name1;
				cout<<"\n\tStudent Address : "<<s.address;
				cout<<"\n\tCity : "<<s.city;
				cout<<"\n\tAge : "<<s.age;
				cout<<"\n\tGender : "<<s.gender;
				cout<<"\n\tEmail : "<<s.email;
				cout<<"\n\tContact Number : "<<s.contact;
				cout<<"\n\tCourse : "<<s.course;
				cout<<"\n\tGuardian's Name : "<<s.gname<<" "<<s.gname1;

				
				dataFile>>s.id;
				dataFile>>s.name;
				dataFile>>s.name1;	
				dataFile>>s.address;
				dataFile>>s.city;
				dataFile>>s.age;
				dataFile>>s.gender;
				dataFile>>s.email;
				dataFile>>s.contact;
				dataFile>>s.course;
				dataFile>>s.gname;
				dataFile>>s.gname1;
          
          
    }
  

}
void Search()
{
	int id;
	string name,name1;
	
	ifstream searchs;
	searchs.open("studentxp.txt");
	searchs>>id>>name>>name1;
	searchs.close();
	
	int sid;
	
	
	cout<<"Search ID : ";
	cin>>sid;
	
	if(!searchs.eof()&&sid==id)
	{
		cout<<id<<name<<name1;
	}
	
	
}
	
		
	
void Update()
{
	
}

tinstaafl 1,114

For your immediate problem, the login function searches through the text file to verify the login credentials. You should be able to adapt this algorithm for the search function.

Some points to consider:

Get out of the habit of using using namespace std;. If you create a function that has the same name as a library function or a later version of the standard library includes one, you can end up with all kinds of trouble trying to track down the bug. If writing std:: in front of all the function/class calls, is a bother, then use a series of using statments for the indiviual functions/classes(e.g using std::string).

Decide if you're using c or c++ in your code and stick to it. At present you have c-style strings and c++-style strings.

goto's are bad. They encourage spaghetti code and there's no reason for them when a normal loop will do.

If you make Student a class you can have better control over how the data is used. For instance you can overload the >> and << operators to simplify reading and inputting the data.

Don't use system This creates a security hole in your code. Any program on the computer named cls will run when your code calls it, and you have no control over what it will do.