-3

Hi I am doing a project for 12th grader . But in Dev C++ I am getting errors

In GDB online compiler also error comes but different ones.

please can you check the code given below

here is the code :

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;

class Book
{
 int bno;
 char bname[20], aname[20];
 public:

 void getdata()
{
 cout<<"\n |New Book Entry| ";
 cout<<"\n Enter Book Number: ";
 cin>>bno;
 cout<<"\n Enter Book Name: ";
 gets(bname);
 cout<<"\n Enter Book Author: ";
 gets(aname);
}


 void putdata()
 {
 cout<<"\n Book Number: "<<bno;
 cout<<"\n Book Name: ";
 puts(bname);
 cout<<"\n Book Author: ";
 puts(aname);
 }

 void editdata()
 {
  cout<<"\n Book Number: "<<bno;
  cout<<"\n Changed Book Name: ";
  gets(bname);
  cout<<"\n Changed Book Author: ";
  gets(aname);
 }

  char bookname()
 {
  return bname;
 }

  int bookno()
  {
   return bno;
  }

 }
  // Class Objects

  fstream f;
  Book b;

  //Function for files


    void writebook()
  {
   char ch;
   f.open("book.dat",ios::app);
   do
   {
   //  clrscr();
   b.getdata();
    f.write((char*)&b,sizeof(Book));
   cout<<"\n Do You Want To Add More? (y/n)?";
    }while(ch=='y'||ch=='Y');
    f.close();
   }

 void display(int n)
{
 cout<<"\n Book Details";
 int flag=0;
 f.open("book.dat",ios::in);
 while(f.read((char *)&b,sizeof(Book)))
 {
  if(b.bookno()==n)
 {
  b.putdata();
  flag=1;  break;
 }
 }
 f.close();
 if(flag==0)
 cout<<"\n Book Does Not Exist";
 getch();
 }

 void search(char name)
 {
 cout<<"\n Book Details";
 int flag=0;
 f.open("book.dat",ios::in);
 while(f.read((char *)&b,sizeof(Book)))
 {
 if(b.bookname()==name)
 {
 b.putdata();
 flag=1;
 break;
 }
 }
 f.close();
if(flag==0)
cout<<"\n Book Does Not Exist.";
getch();
}


void displayall()
{
cout<<"\n Book Details:";
f.open("book.dat",ios::in);
b.putdata();
f.close();
getch();
}


void editbook()
{
int n, found=0;
clrscr();
cout<<"\n Edit Book Record.";
cout<<"\n Enter Book Number: ";
cin>>n;
f.open("book.dat",ios::in|ios::out);
while(f.read((char*)&b,sizeof(Book)) && found==0)
{
if(b.bookno()==n)
{
 b.putdata();
 cout<<"\n Enter The New Details: ";
 b.editdata();
 int pos=-1*sizeof(b);
 f.seekp(pos,ios::cur);
 f.write((char*)&b,sizeof(Book));
 cout<<"\n Record Updated";
 found=1;
}
}
f.close();
if(found==0)
cout<<"\n Record not Found.";
getch();
}


 // Delete Function
 void deletebook()
{
  int n;
  clrscr();
  cout<<"\n Delete Book";
  cout<<"\n Enter Book Number For The Book To Be Deleted: ";
   cin>>n;
  fstream f1;
  f.open("book.dat",ios::in|ios::out);
  f1.open("temp.dat",ios::out);
  f1.seekg(0,ios::beg);
 while(f.read((char*)&b,sizeof(Book)))
{
  if(b.bookno()!=n)
  {
      f1.write((char*)&b,sizeof(Book));
  }
}
f1.close();
 f.close();
 remove("book.dat");
 rename("temp.dat","book.dat")   ;
 cout<<"\n Record Deleted";
 getch();
 }



 // Main

 void main()
  {
 clrscr();
 int ch,n;
 char name;
 cout<<"\n Computer Science Project";
 cout<<"\n Book Details            ";
 cout<<"\n Name : XXXXXXXXXXX";
  cout<<"\n Class: 12 XX             ";
  do
  {
  cout<<"\n Main Menu             ";
  cout<<"\n 1.Accept Book.        ";
  cout<<"\n 2.Display All Books.  ";
  cout<<"\n 3.Display Book.       ";
  cout<<"\n 4.Search By Book Name.";
  cout<<"\n 4.Edit Book.          ";
  cout<<"\n 5.Delete Book.        ";
  cout<<"\n 6.Exit.               ";
  cout<<"\n Select Your Option:   ";
  cin>>ch;
  switch(ch)
  {
  case 1: clrscr();
     writebook();
     break;

   case 2: clrscr();
      displayall();
      break;

    case 3: clrscr();
       cout<<"\n Enter Book Number:";
        cin>>n;
        display(n);
        break;

    case 4: clrscr();
       cout<<"\n Enter Book Name:";
       gets(name);
       search(name);
       break;

   case 5: clrscr();
       editbook();
       break;

   case 6: clrscr();
       deletebook();
       break;

    case 7: exit(0);
       default:cout<<" Please Enter Valid Option: ";
     }
     }while(ch!=7);
        getch();
     }

Errors to be tackled : Line 47 - invalid conversion from char * to char line 131, 161 clrscr not declared

Amizade
  • 5
  • 2

1 Answers1

1

You need to include the relevant header files for the standard library code you're using. In this case:

#include <iostream> // for cout / cin
#include <cstdio> // for gets / puts

Also, these identifiers are declared in the std namespace, so you need to correctly qualify them by prefixing with std:: when using them, e.g.:

std::cout << "hello world" << std::endl;

It is possible that example code from books, etc. has a using namespace std; somewhere, allowing use of items from the std namespace without qualifying them. However, this is bad practice, and not recommended.

user673679
  • 1,327
  • 1
  • 16
  • 35