How to make admno unique in prog. with FILES ? Code is here below.

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
#include <iostream>
#include<string.h>
#include<fstream>
#include<conio.h>
#include<iomanip>


using namespace std;

class student{
    char admno[6];
	char name[20];
	char stbno[6];
	int token;
public:
void create_student()
	{
	    cout<<"\nNEW STUDENT ENTRY...\n";
		cout<<"\nEnter The admission no. ";
		cin.getline(admno,6);
        cout<<"\n\nEnter The Name of The Student ";
		cin.getline(name,20);
        token=0;
		stbno[0]='/0';
        getch();
	}

	void show_student()
	{
	    cout<<"\nAdmission no. : ";
	    cout.write(admno,'\n');
		cout<<"\nStudent Name : ";
		cout.write(name,'\n');
		cout<<"\nNo of Book issued : "<<token;
		if(token!=0)
        {
            cout<<"\nBook No: ";
            cout.write(stbno,'\n');
        }
	}

	void report()
	{
	    int l=strlen(admno), m=strlen(name);
	    cout<<"\t";
	    cout.write(admno, l);
	    cout<<"\t\t";
	    cout.write(name, m);
	    cout<<setw(10)<<token<<endl;
    }

     char* retadmno()
	{
		return admno;
	}
};

student st;
fstream fp, f;

void write_student()
{
    char ch;
	fp.open("student.txt",ios::out|ios::app);
	do
	{
	    st.create_student();
		fp.write((char*)&st,sizeof(st));
		cout<<"\n\ndo you want to add more record..(y/n?)";
		ch=getch();
		cout<<endl;
	}while(ch=='y'||ch=='Y');
	fp.close();
}

void display_alls()
{
    fp.open("student.txt",ios::in);
    if(!fp)
    {
        cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
        getch();
        return;
    }

    cout<<"\n\n\t\tSTUDENT LIST\n\n";
	cout<<"==================================================================\n";
	cout<<"\tAdmission No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
	cout<<"==================================================================\n";

    while(fp.read((char*)&st,sizeof(student)))
	{
	    st.report();
	}
    fp.close();
	getch();
}

void admin_menu()
{
    int ch2;
	cout<<"\n\n\n\tADMINISTRATOR MENU";
	cout<<"\n\n\t1.CREATE STUDENT RECORD";
	cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORD";
	cin>>ch2;
	cin.ignore();
	switch(ch2)
	{
	    case 1: write_student();break;
        case 2: display_alls();break;
	}
}

int main()
{
    admin_menu();
    return 0;
}


How do I make admno unique i.e. if a record with admno=001 exists, it should give error if I try to enter a new record with same admno.

I rewrote this create_student() func but it is not working, everytime I run the above program with "write_student() func" given below, it always says Admission no. exists.. Pls advice what should I do ?

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
void write_student()
{
    int flag=0;
    char ch, temp[6], pemp[6];
	fp.open("student.txt",ios::out|ios::app);
	f.open("adm.txt",ios::in|ios::out|ios::app);
	fp.seekg(0,ios::beg);
	while(fp.read((char*)&st,sizeof(st)))
    {
	    f.write((char*)&st,sizeof(st));
    }
	do
	{
	    st.create_student();
	    strcpy(pemp,st.retadmno());
	    while(!f.eof())
		{
		 if(strcmpi(pemp,st.retadmno())==0) {flag=1; break;}
		}
		if(flag==0)
           {fp.write((char*)&st,sizeof(st)); cout<<"\n\nStudent Record Created..";}
        else
            cout<<"\nAdmission no. exists.\n";
		cout<<"\n\ndo you want to add more record..(y/n?)";
		ch=getch();
		cout<<endl;
	}while(ch=='y'||ch=='Y');
	fp.close();
	f.close();
}

Last edited on
One way would be to keep all the students in a vector. Before you add a new student you check if one with admno exists.

Alternatively you could read all the students from the file and check it one with the admno exists.
Topic archived. No new replies allowed.