error in including library database.txt file in c++ - c++

I am making a library management system and including text file which contains information of books and book allotment. But it is showing an error. I am having a database.txt file in which names of books and allotment of the book is given. please help...
my code is
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include "C:\Users\Ritesh Singh Chauhan\Desktop\librarydatabase.txt"
char name[20];
char book[20];
char person[20];
void searchBook()
{
ifstream infile;
infile.open("librarydatabase.txt");
cout<<"enter name of book";
cin>>name;
infile>>name;
cout<<name<<endl;
infile.close();
}
void addNewBook()
{
ofstream outfile;
outfile.open("librarydatabase.txt");
cout<<"enter name of book";
cin>>book;
outfile<<book<<endl;
cout<<"successfully added";
outfile.close();
}
void allotmentList()
{
cout<<"enter name of person";
cin>>person;
}
void exitLib()
{
//exit();
}
void main()
{
clrscr();
int number;
cout<<"press 1 to search book"<<" "<<"press 2 to add a new book"<<" "
<<"press 3 to allotment list by person"<<" "<<"press 4 to exit";
cin>>number;
switch(number)
{
case 1: searchBook();
break;
case 2: addNewBook();
break;
case 3: allotmentList();
break;
case 4: exitLib();
break;
default: cout<<"press number between 1 to 4";
}
[code is showing error and an error message is given in image][1]}

Remove following line from the code
#include "C:\Users\Ritesh Singh Chauhan\Desktop\librarydatabase.txt"
And
change the line
infile.open("librarydatabase.txt");
to
infile.open("C:\\Users\\Ritesh Singh Chauhan\\Desktop\\librarydatabase.txt");

Related

Problem(logical error) with the output of the code(file handling)

this is the input I made the following code for my cs project but i am facing some logical errors which i am unable to fix i tried a lot but still i am unable to fix it so please help me,,
as you can see in the picture above i entered male in sex but it is writing only ale in the file and there is a problem with the output
This is software which i made as a project for my practical exam it was working fine until my friend did some modifications to it whenever i am entering any record(using file) it is working fine(at least it seems to be) but when i am displaying the record some of the characters are missing i don't know why so please help me to fix it....*
thank you
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class patient
{
public:
int year,date,month;
int bno;
char na[20],street[20],city[20],sex[8];
patient()
{
bno=0;
year=0;
date=0;
month=0;
strcpy(na,NULL);
strcpy(city,NULL);
strcpy(street,NULL);
strcpy(sex,NULL);
}
void enterdata()
{
cout<<"\nENTER THE NAME OF THE PATIENT \t\t-----\t\t";gets(na);
cout<<"\nENTER GENDER\t\t\t\t-----\t\t";gets(sex);
cout<<"\nASSIGN A UNIQUE BED NO. \t\t-----\t\t"; cin>>bno;
cout<<"\nDATE OF BIRTH\nDATE \t\t\t\t\t-----\t\t";cin>>date;
cout<<"\nMONTH \t\t\t\t\t-----\t\t";cin>>month;
cout<<"\nYEAR \t\t\t\t\t-----\t\t";cin>>year;
cout<<"\nENTER STREET \t\t\t\t-----\t\t";gets(street);
cout<<"\nENTER CITY \t\t\t\t-----\t\t";gets(city);
}
void showdata()
{
clrscr();
cout<<" \t\t\tINFORMATION OF THE PATIENT\n";
for(int vg=0;vg<78;vg++)
cout<<"-";
cout<<"\n";
for(vg=0;vg<78;vg++)
cout<<"-";
cout<<"\n\n\t\t\tBED NO \t\t:-\t"<<bno;
cout<<"\n\n\t\t\tNAME \t\t:-\t"<<na;
cout<<"\n\n\t\t\tGENDER \t\t:-\t"<<sex;
cout<<"\n\n\t\t\tCITY \t\t:-\t"<<city;
cout<<"\n\n\t\t\tSTREET \t\t:-\t"<<street;
}
~patient()
{
bno=0;
year=0;
date=0;
month=0;
strcpy(na,NULL);
strcpy(city,NULL);
strcpy(street,NULL);
strcpy(sex,NULL);
}};
void main()
{
patient p,p1;
char ch,cna[20];
xy:
do
{
ifstream ifile;
ifile.open("file1.dat",ios::in);
ofstream ofile;
ofile.open("file1.dat",ios::app|ios::binary);
ofstream temp;
temp.open("temp1.dat",ios::app|ios::binary);
clrscr();
cout<<"\n\t\t\t***HOSPITAL MANAGEMENT SOFTWARE***"<<"\n";
cout<<"\n\t\t\t **Hospital Management Tasks**"<<"\n\n";
for(int st=0;st<80;st++)
cout<<"-";
for(st=0;st<80;st++)
cout<<"|";
for(st=0;st<80;st++)
cout<<"-";
cout<<"\n\n";
cout<<"\t\t\tPlease select a task to do...."<<"\n\n";
cout<<"\t\t\t1. Enter a new patient information ."<<"\n\n";
cout<<"\t\t\t2. View detail of existing patient ."<<"\n\n";
cout<<"\t\t\t3. Emergency"<<"\n\n";
cout<<"\t\t\t4. Discharge patient"<<"\n\n";
cout<<"\t\t\t5. Exit from the program ."<<"\n\n";
cout<<"\t\t\t6.ADMIN MODE"<<"\n\n";
cout<<"\t\t\tEnter your task serial :"<<"\n\n\t\t\t";
ch=getche();
switch(ch)
{
case '1': clrscr();
p.enterdata();
ofile.write((char*)&p,sizeof(p));
getch();
break;
case '2':
clrscr();
int k=0,cbno=0;
if(!ifile)
{
cout<<"\n unable to open file\n";
getch();
}
else
{
cout<<"\n ENTER THE NAME OF PATIENT\t\t\t-----\t\t";gets(cna);
cout<<"\n ENTER BED NUMBER\t\t\t\t-----\t\t";cin>>cbno;
ifile.clear();
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{
ifile.read((char*)&p1,sizeof(p1));
if(cbno==p1.bno)
{
if(strcmpi(cna,p1.na)==0)
{
p1.showdata();
getch();
k=1;
break;
}
}
}
if(k!=1)
cout<<"\n NO SUCH RECORD PRESENT\n";
getch();
}
ifile.close();
break;
case '3':
clrscr();
temp.clear();
temp.seekp(0,ios::beg);
ifile.clear();
ifile.seekg(0,ios::beg);
p1.enterdata();
temp.write((char*)&p1,sizeof(p1));
while(!ifile.eof())
{
ifile.read((char*)&p,sizeof(p));
cout<<"\ndone writing\n";
cout<<"\n done";getch();
temp.write((char*)&p,sizeof(p));
}
ifile.close();
temp.close();
remove("file1.dat");
rename("temp1.dat","file1.dat");
break;
case '4' :
char ch;int pos=-1;
clrscr();
cout<<"\t\t\tDISCHARGE PATIENT\n";
if(!ifile)
{ clrscr();
cout<<"\n\n\t\t\t!!!!FAILED TO OPEN FILE!!!! \n";
getch();
}
else
{
cout<<"\n ENTER THE NAME OF PATIENT\t\t\t-----\t\t";gets(cna);
cout<<"\n ENTER BED NUMBER\t\t\t\t-----\t\t";cin>>cbno;
ifile.clear();
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{
ifile.read((char*)&p1,sizeof(p1));
if(cbno==p1.bno)
{
if(strcmpi(cna,p1.na)==0)
{ pos=ifile.tellg();
p1.showdata();
cout<<"\n \t\t ARE YOU SURE TO DISCHARGE THIS PATIENT(y/n)? \n \t\t";
ch=getche();
getch();k=1;
break;
} }
}
if(k!=1)
{ cout<<"\n NO SUCH RECORD PRESENT\n";
getch();
ifile.close();
goto xy;}
ifile.close();
if(ch=='y'||ch=='Y')
{
ifile.open("file1.dat");
while(!ifile.eof())
{
ifile.read((char*)&p1,sizeof(p1));
if(pos!=ifile.tellg())
temp.write((char*)&p1,sizeof(p1));
}
ifile.close();
temp.close();
remove("file1.dat");
rename("temp1.dat","file1.dat");
} }
break;
case '5':
break;
case '6':
char id[5];
clrscr();
cout<<"\n ENTER THE ADMIN ID\t\t---- ";
gets(id);
if(strcmp(id,"70176")==0)
{
cout<<"\n\n\n\t\t\t VERIFICATION SUCCESSFUL ";
getch();
if(!ifile)
{cout<<"unable to open file";
getch();}
else
{
ifile.clear();
ifile.seekg(0,ios::beg);
while(!ifile.eof())
{ ifile.read((char*)&p,sizeof(p));
p.showdata();
getch();
}
} ifile.close(); }else
{ cout<<"\n\n\t\t\t!!!!!! FAILED TO VERIFY!!!!!!\n"; getch();}
break;
default :
cout<<"\n ERROR \n";
cout<<"\a";
getch();
break;}}
while(ch!='5');
}
if the code is not clear here is the link to download it https://drive.google.com/file/d/1hTz3awCDaW-40ewEbpK1VHYa6k5SoD-d/view?usp=sharing

Why is the data not getting properly written/read in this file?

The following code just accepts some data from the user in an object and writes it in a binary file. The first object gets written fine but after that it gets messed up.
Below is the output on reading the file after writing the first object onto it.
ROLL NO. NAME MARKS
234 Sansa 67
All fine and just as I expect. But when I append another object in the file, then this happens.
ROLL NO. NAME MARKS
234 SansSnow 78
564 Jon Snow 78
The roll numbers remain intact however names of the previous objects gets mixed up with the last object's and their marks get overwritten by the marks of the last object.
Why is it happening?
CODE:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
fstream file;
class Student
{
protected:
int rollno;
char name[30];
float marks;
public:
void getData()
{
cout<<"ENTER THE DATA..."<<endl<<endl;
cout<<"Roll Number : ";
cin>>rollno;
fflush(stdin);
cout<<"Name : ";
cin.getline(name,30);
cout<<"Marks : ";
cin>>marks;
}
void displayData()
{
cout<<"\n"<<rollno;
cout<<setw(17)<<name;
cout<<setw(13)<<marks;
}
void writeData()
{
file.open("StudentData.dat", ios::app|ios::binary);
file.write((char*)this, sizeof(this));
file.close();
}
void readData()
{
file.open("StudentData.dat",ios::in|ios::binary);
while(file.read((char*)this, sizeof(this)))
{
displayData();
}
file.close();
}
};
Student S1;
int main()
{
cout<<"\nMAIN MENU"<<endl<<endl;
cout<<"1. Enter data and write to file\n";
cout<<"2. Read data from file and display\n";
cout<<"3. Exit\n\n";
cout<<"Enter your choice : ";
int choice;
cin>>choice;
switch(choice)
{
case 1:{
S1.getData();
S1.writeData();
cout<<endl<<endl;
cout<<"Data written to file successfully!"<<endl;
main();
break;
}
case 2:{
cout<<"ROLL NO."<<setw(10)<<"NAME"<<setw(15)<<"MARKS";
S1.readData();
cout<<endl;
main();
break;
}
case 3:{
exitop:
char confirmExit;
cout<<"\nAre you sure you want to exit? (Y/N) : ";
cin>>confirmExit;
if(confirmExit=='y' or confirmExit=='Y')
exit(0);
else if(confirmExit=='n' or confirmExit=='N')
{
cout<<"Exit Aborted.\n\n";
getchar();
main();
}
else{
cout<<"Invalid Input!";
goto exitop;
}
}
default:{
cout<<"Invalid Input!";
break;
}
}
return 0;
}
You’re only reading and writing 8 bytes. this is a pointer so sizeof(this) is 8 (on a 64 bit machine). What you actually want to do is get the size of the object instead so use sizeof(*this) or sizeof(Student).

Getting the same output twice when reading a file

I am having a problem with the following program,
#include<iostream>
#include<fstream>
#include<windows.h>
using namespace std;
class student
{
int rollno;
char name[50];
int cls;
int marks;
char grade;
public:
void getdata()
{
cout<<"Enter Roll No.: ";
cin>>rollno;
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Class: ";
cin>>cls;
cout<<"Enter Marks: ";
cin>>marks;
if(marks>=75)
{
grade='A';
}
if(marks>=60 && marks<75)
{
grade='B';
}
if(marks>=45 && marks<60)
{
grade='C';
}
if(marks>=35 && marks<45)
{
grade='D';
}
if(marks<35)
{
grade='F';
}
}
void display()
{
cout<<"\nRoll No.: "<<rollno;
cout<<"\nName: "<<name;
cout<<"\nClass: "<<cls;
cout<<"\nMarks: "<<marks;
cout<<"\nGrade: "<<grade<<endl;
}
int rno()
{
return rollno;
}
} s1, s2, s3;
void append()
{
fstream fo ("STUDENT.DAT",ios::binary | ios::app | ios::out);
if(!fo)
{
cout<<"CANNOT OPEN FILE!!!!!";
}
else
{
s1.getdata();
fo.write((char*)&s1, sizeof(s1));
}
fo.close();
}
void sdisplay()
{
fstream fi ("STUDENT.DAT",ios::binary | ios::in);
if(!fi)
{
cout<<"\nNO RECORDS !!!!";
}
else
{
while(!fi.eof())
{
fi.read((char*)&s2, sizeof(s2));
if(s2.rno()!='\0')
{
s2.display();
}
}
}
fi.close();
}
void rdisplay()
{
int n, found;
fstream fi ("STUDENT.DAT", ios::in | ios::binary);
if(!fi)
{
cout<<"\nNO RECORDS !!!!";
}
else
{
cout<<"\nEnter Roll No. to be displayed: ";
cin>>n;
while(!fi.eof())
{
fi.read((char*)&s3, sizeof(s3));
if(s3.rno()==n)
{
s3.display();
found=1;
}
}
if(found!=1)
{
cout<<"\nCannot find roll no!!";
}
}
fi.close();
}
int main()
{
char ch='y';
int choice, n;
while(ch=='y' || ch=='Y')
{
cout<<"\n1. Append Data to File";
cout<<"\n2. Display Entire File";
cout<<"\n3. Search & Display record based on Roll No.";
cout<<"\n4. Exit";
cout<<"\n\n Enter your choice (1-4): ";
cin>>choice;
switch(choice)
{
case 1:
append();
break;
case 2:
sdisplay();
break;
case 3:
rdisplay();
break;
case 4:
exit(0);
break;
default:
cout<<"\nWrong Choice !!!!!";
break;
}
cout<<"\n\nDo you want to continue? (Y/N)";
cin>>ch;
}
}
So, After appending the data, When I go to Display the data (option 2 & 3) it outputs the same entry twice.
Append Data to File
Display Entire File
Search & Display record based on Roll No.
Exit
Enter your choice (1-4): 2
NO RECORDS !!!!
Do you want to continue? (Y/N)y
Append Data to File
Display Entire File
Search & Display record based on Roll No.
Exit
Enter your choice (1-4): 1 Enter Roll No.: 23 Enter Name: James Enter
Class: 12 Enter Marks: 85
Do you want to continue? (Y/N)y
Append Data to File
Display Entire File
Search & Display record based on Roll No.
Exit
Enter your choice (1-4): 2
Roll No.: 23 Name: James Class: 12 Marks: 85 Grade: A
Roll No.: 23 Name: James Class: 12 Marks: 85 Grade: A
Do you want to continue? (Y/N)n
Process returned 0 (0x0) execution time : 25.013 s Press any key to
continue.
Can someone help me?
Thanks
eof() won't get set until you read past the end, which won't happen until your 2nd read. That read will do nothing, so it won't overwrite s2, it'll just fail - so you'll end up printing that row again.
The solution is don't loop over eof(). Just loop over read():
while(fi.read((char*)&s2, sizeof(s2)))
{
s2.display();
}

data written in file only in alternate steps

code
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
using namespace std;
class student
{
int admno;
char name[20];
// char address[20];
//string name;
public:
void getdata()
{
cout<<"\n\nEnter The Name of The Student ";
//gets(name);
//cin.ignore();
cin>>name;
cout<<"\nEnter The admission no. ";
cin>>admno;
// getch();
}
void showdata()
{
cout<<"\nAdmission no. : "<<admno;
cout<<"\nStudent Name : "<<name<<endl;
//puts(name);
}
void display()
{
//student obj;
ifstream fp1;
fp1.open("student.dat",ios::binary);
while(fp1.read((char*)this,sizeof(*this)))
{
this->showdata();
fp1.read((char*)this,sizeof(*this));
}
fp1.close();
}
void add()
{
ofstream fp2;
fp2.open("student.dat",ios::binary|ios::app);
this->getdata();
fp2.write((char*)this,sizeof(*this));
fp2.close();
}
};
int main()
{
student obj;
//system("cls");
cout<<"\n1. Add new student";
cout<<"\n2. View all student";
cout<<"\n3. Search student";
cout<<"\n4. modify student";
cout<<"\n5. delete student";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice";
int ch;
cin>>ch;
switch(ch)
{
case 1:
obj.add();
break;
case 2:
obj.display();
//b.viewbook();
break;
default:
cout<<"Enter Valid choice";
}
return 0;
}
problem:
when I enter the data in file, the data is written to the file only in the alternate steps.
at the first run and enter the data it is OK and in second run to add the student, the data isnot written in the file and again in the next step it writes properly and it continues to give proper output in alternate addition.
You should put your switch inside a loop:
bool exitLoop = false;
while(!exitLoop) {
int ch;
cin>>ch;
switch(ch) {
case '1':
obj.add();
break;
case '2':
obj.display();
//b.viewbook();
break;
case '6':
exitLoop = true;
break;
default:
cout<<"Enter Valid choice";
}
}
Also note how to handle character inputs: 1 != '1'

How do I compare a string variable to an array of strings created from a file

Hello Ive been working on a program where I have a file with 3 columns containing both the inauguration year and departure year of a president, along with the name of the president. Im trying to have the user input a president and have the program return the start and stop year. I began by opening the file (which opens correctly) and making 3 arrays, 2 integer arrays and one string array. The program runs but when I press 2 regardless of what name I enter the bool stays false. The file can be found here: http://pastebin.com/8h3BJxGD
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("presidents.txt");
if(file.fail())
cout<<"failed to open file"<<endl;
for(i=0;file>>start[i];i++)
{
file>>stop[i];
file.get(junk);
getline(file,names[i]);
cout<<start[i]<<stop[i]<<names[i]<<endl;
count++;
}
do
{
cout<<"What would you like to know?"<<endl;
cout<<"Press 1 for who was President in what year"<<endl;
cout<<"Press 2 for the years served by a President"<<endl;
cout<<"Press 3 to stop"<<endl;
cin>>x;
if(x=='1')
{
bool valid=false;
cout<<"Enter a year: "<<endl;
cin>>year;
for(i=0;i<count;i++)
{
if(start[i]<=year&&stop[i]>=year)
{
cout<<names[i]<<endl;
cout<<endl;
valid=true;
}
}
if(valid==false)
{
cout<<"Invalid year"<<endl;
cout<<endl;
}
}
if(x=='2')
{
bool valid=false;
cout<<"Enter a President: "<<endl;
cin>>president;
getline(cin,president);
for(i=0;i<count;i++)
{
if(president==names[i])
{
cout<<start[i]<<"-"<<stop[i]<<endl;
cout<<endl;
valid=true;
}
}
if(valid==false)
{
cout<<"Please be more percise"<<endl;
cout<<endl;
}
}
}
while (x!='3');
cin>>junk;
return 0;
}
Here, problem is not with the comparison, but with input string into president variable, try printing president variables value, you will understand the problem.
You need to add following line after reading x.
cin.ignore(); //add this line after cin>>x;
This will remove \n from the input buffer and will not cause any issue while reading president string. You need to take care of such issues while combining use of formatted input (i.e. >>) with unformatted input (i.e. get(), getline() etc).
Below is the modified code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("president.txt");
if(file.fail())
cout<<"failed to open file"<<endl;
for(i=0;file>>start[i];i++)
{
file>>stop[i];
file.get(junk);
getline(file,names[i]);
cout<<start[i]<<stop[i]<<names[i]<<endl;
count++;
}
do
{
cout<<"What would you like to know?"<<endl;
cout<<"Press 1 for who was President in what year"<<endl;
cout<<"Press 2 for the years served by a President"<<endl;
cout<<"Press 3 to stop"<<endl;
cin>>x;
cin.ignore();
if(x=='1')
{
bool valid=false;
cout<<"Enter a year: "<<endl;
cin>>year;
for(i=0;i<count;i++)
{
if(start[i]<=year&&stop[i]>=year)
{
cout<<names[i]<<endl;
cout<<endl;
valid=true;
}
}
if(valid==false)
{
cout<<"Invalid year"<<endl;
cout<<endl;
}
}
if(x=='2')
{
bool valid=false;
cout<<"Enter a President: ";
getline(cin,president);
for(i=0;i<count;i++)
{
if(president==names[i])
{
cout<<start[i]<<"-"<<stop[i]<<endl;
cout<<endl;
valid=true;
}
}
if(valid==false)
{
cout<<"Please be more percise"<<endl;
cout<<endl;
}
}
}
while (x!='3');
cin>>junk;
return 0;
}
Following is the output:
What would you like to know?
Press 1 for who was President in what year
Press 2 for the years served by a President
Press 3 to stop
2
Enter a President: Theodore Roosevelt
1901-1909
if(president==)
if president == what???
Also,
getline(cin,president); //why this line?
if you have already read the file and put into data structure, then just search through the data structure for the name of the president that the user enters...if you find it, keep track of the index as you have done and print out the start[] and stop[] at the same index...