Hello,

i need a little help with this code.

it's a header file personal.h

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

#ifndef PERSONAL
#define PERSONAL

class Personal {

public:

       Personal();
       Personal (char*,char*,char*,int,long);

       void writeToFile(fstream&)const;
       void readFromFile(fstream&);
       void readKey();
       
       int size() const
       {
           return 9 + nameLen + cityLen + sizeof(year) + sizeof (salary);
       }
       bool operator==(const Personal& pr) const 
       {          
            return strncmp(pr.SSN, SSN) == 0;
       }

protected:

          const int nameLen, cityLen;
          char SSN[10], *name, *city;
          int year;
          long salary;
          ostream& writeLegibly(ostream&);
          friend ostream& operator<<(ostream& out, Personal& pr)
          {
                 return pr.writeLegibly(out);
          }
          
          istream& readFromConsole (istream&);
          friend istream& operator>>(istream& in, Personal& pr)
          {
                 return pr.readFromConsole(in);
          }
};

#endif

and the cpp file

#include "personal.h"

Personal::Personal() : nameLen(10), cityLen(10)
{
   name = new char [nameLen+1];
   city = new char [cityLen+1];
}

Personal::Personal(char *ssn, char *n, char *c, int y, long s):

  nameLen(10), cityLen(10)
  {
   name = new char [nameLen+1];
   city = new char [cityLen+1];
   strcpy(SSN, ssn);
   strcpy(name, n);
   strcpy(city, c);
   year = y;
   salary = s;
  }

void Personal::writeToFile(fstream& out) const 
{
     out.write(SSN, 9);
     out.write(name, nameLen);
     out.write(city, cityLen);
     out.write(reinterpret_cast<const char*>(&year),sizeof(int));
     out.write(reinterpret_cast<const char*>(&salary),sizeof(int));
}

void Personal::readFromFile(fstream& in)
{
     in.read(SSN,9);
     in.read(name,nameLen);
     in.read(city,cityLen);
     in.read(reinterpret_cast<char*>(&year),sizeof(int));
     in.read(reinterpret_cast<char*>(&salary),sizeof(int));
}
void Personal::readKey()
{
     char s[80];
     cout << "Enter SSN: ";
     cin.getline(s,80);
     strncpy (SSN,s,9);

}

ostream& Personal::writeLegibly(ostream& out) 
{

 SSN[9] = name[nameLen] = city[cityLen] = '\0';

 out << "SSN = " << SSN << ", name = " << name
     << ", city = " << city << ", year = " << year
     << ", salary = " <<  salary;

    return out;

}

 

istream& Personal::readFromConsole(istream& in) 
{
         char s[80];
         cout  << "SSN: ";
         in.getline(s,80);
         strncpy(SSN,s,9);
         cout << "Name: ";
         in.getline(s,80);
         strncpy(name, s, nameLen);
         cout << "City: ";
         in.getline(s, 80);
         strncpy(city, s, cityLen);
         cout << "Birthyear: ";
         in >> year;
         cout << "Salary: ";
         in >> salary;

         in.ignore();

         return in;
}

I get this error after i compiled the code

51 C:\Dev-Cpp\include\string.h too few arguments to function `int strncmp(const char*, const char*, size_t)'

i have a doubt that this line of code has the error

return strncmp(pr.SSN, SSN) == 0;

i attached the file here. Only in the personal.h file i get an error aside from the other file. Could you please help me figure this out.

Edited 5 Years Ago by zhinokin: n/a

You need to put a length to compare. The compiler tells you the exact problem, not enough arguments, you need a size_t (or, rather, in the case of strings, string.size, usually) as a third parameter.

Edited 5 Years Ago by Red Goose: n/a

yeah, that solves the problem :).thanks mate..

now i have a new problem i encounter after i filled all the input data then it will display the data being input but the output is an infinite loop i mean it display the input data but it's being repeated always. short to say it loops always..

here's the full code

i just noticed also that the output data given is wrong. I can't figure out where to look exactly the wrong code.

--database.cpp

#include<iostream>

#include "personal.h"
#include "database.h"
#include "student.h"

template<class T>
Database<T>::Database() 
{

}

template<class T>

void Database<T>::add(T& d) 
{
     database.open(fName,ios::in|ios::out|ios::binary);
     database.seekp(0,ios::end);
     d.writeToFile(database);
     database.close();
}

template<class T>

void Database<T>::modify(const T& d) 
{
     T tmp;
     database.open(fName,ios::in|ios::out|ios::binary);
     
     while(!database.eof()) 
     {
          tmp.readFromFile(database);
          if (tmp == d) { // overloaded ==
          cin >> tmp; // overloaded >>
          database.seekp(-d.size(),ios::cur);
          tmp.writeToFile(database);
          database.close();

          return;
      }
}
database.close();
cout << "The record to be modified is not in the database\n";

}
template<class T>

bool Database<T>::find(const T& d) 
{
     T tmp;
     database.open(fName,ios::in|ios::binary);
     
     while(!database.eof()) 
     {
          tmp.readFromFile(database);
          if(tmp == d) { // overloaded ==
          database.close();

          return true;
     }

}
 database.close();
return false;

}

template<class T>

ostream& Database<T>::print(ostream& out){
         
         T tmp;
         database.open(fName,ios::in|ios::binary);

while(true) {

tmp.readFromFile(database);

if(database.eof())

      break;
      out << tmp << endl; //overloaded <<
      
}
    database.close();
    
    return out;
}

template<class T>

void Database<T>::run() {

     cout << "File name: ";
     cin >> fName;
     char option[5];
     T rec;

cout << "1.Add 2.Find 3.Modify a record; 4.Exit\n";
cout << "Enter an option: ";
cin.getline(option,4); // get '\n';

while (cin.getline(option,4)) {

      if(*option == '1') {
      cin >> rec;
      add(rec);
  }

else if (*option == '2'){
     rec.readKey();
     cout << "The record is ";

if(find(rec) == false)

     cout << "not";
     cout << "in the database\n";
}

else if(*option == '3') {
     rec.readKey();
     modify(rec);
}

else if(*option != '4')
     cout << "Wrong option\n";

else return;
     cout << *this;
     cout << "Enter an option:";

     }

}

 

int main() {

    Database<Personal> ().run();
    //Database<Student> ().run();
    return 0;
}

--database.h

#ifndef DATABASE
#define DATABASE

template<class T>

class Database {

public:

       Database();
       void run();

private:

        fstream database;
        char fName[20];

        ostream& print(ostream&);

void add(T&);

bool find(const T&);

void modify(const T&);

friend ostream& operator<<(ostream& out, Database& db){

return db.print(out);

}

};

#endif

--personal.cpp

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

#ifndef PERSONAL
#define PERSONAL

class Personal {

public:

       Personal();
       Personal (char*,char*,char*,int,long);

       void writeToFile(fstream&)const;
       void readFromFile(fstream&);
       void readKey();
       
       int size() const
       {
           return 9 + nameLen + cityLen + sizeof(year) + sizeof (salary);
       }
       bool operator==(const Personal& pr) const 
       {          
            return strncmp(pr.SSN, SSN, 9) == 0;
       }

protected:

          const int nameLen, cityLen;
          char SSN[10], *name, *city;
          int year;
          long salary;
          ostream& writeLegibly(ostream&);
          friend ostream& operator<<(ostream& out, Personal& pr)
          {
                 return pr.writeLegibly(out);
          }
          
          istream& readFromConsole (istream&);
          friend istream& operator>>(istream& in, Personal& pr)
          {
                 return pr.readFromConsole(in);
          }
};

#endif

--personal.h

#include "personal.h"

Personal::Personal() : nameLen(10), cityLen(10)
{
   name = new char [nameLen+1];
   city = new char [cityLen+1];
}

Personal::Personal(char *ssn, char *n, char *c, int y, long s):

  nameLen(10), cityLen(10)
  {
   name = new char [nameLen+1];
   city = new char [cityLen+1];
   strcpy(SSN, ssn);
   strcpy(name, n);
   strcpy(city, c);
   year = y;
   salary = s;
  }

void Personal::writeToFile(fstream& out) const 
{
     out.write(SSN, 9);
     out.write(name, nameLen);
     out.write(city, cityLen);
     out.write(reinterpret_cast<const char*>(&year),sizeof(int));
     out.write(reinterpret_cast<const char*>(&salary),sizeof(int));
}

void Personal::readFromFile(fstream& in)
{
     in.read(SSN,9);
     in.read(name,nameLen);
     in.read(city,cityLen);
     in.read(reinterpret_cast<char*>(&year),sizeof(int));
     in.read(reinterpret_cast<char*>(&salary),sizeof(int));
}
void Personal::readKey()
{
     char s[80];
     cout << "Enter SSN: ";
     cin.getline(s,80);
     strncpy (SSN,s,9);
}

ostream& Personal::writeLegibly(ostream& out) 
{

 SSN[9] = name[nameLen] = city[cityLen] = '\0';

 out << "SSN = " << SSN
     << ", name = " << name
     << ", city = " << city << ", year = " << year
     << ", salary = " <<  salary;

    return out;
    

}

 

istream& Personal::readFromConsole(istream& in) 
{
         char s[80];
         cout  << "SSN: ";
         in.getline(s,80);
         strncpy(SSN,s,9);
         cout << "Name: ";
         in.getline(s,80);
         strncpy(name, s, nameLen);
         cout << "City: ";
         in.getline(s, 80);
         strncpy(city, s, cityLen);
         cout << "Birthyear: ";
         in >> year;
         cout << "Salary: ";
         in >> salary;

         in.ignore();

         return in;
}

--student.cpp

#include "student.h"

Student::Student(): majorLen(10) 
{
  Personal();

  major = new char[majorLen+1];

}

Student::Student(char *ssn, char *n, char *c, int y, long s, char *m):

majorLen(11) 
{

 Personal(ssn,n,c,y,s);
 major = new char[majorLen+1];
 strcpy(major,m);

}

void Student::writeToFile(fstream& out)const 
{

  Personal::writeToFile(out);

  out.write(major,majorLen);

}

void Student::readFromFile(fstream& in) 
{

 Personal::readFromFile(in);
 in.read(major,majorLen);
 
}

ostream& Student::writeLegibly(ostream& out) 
{
 
 Personal::writeLegibly(out);
 major[majorLen]='\0';

 out << ", major = " << major;

 return out;
}

istream& Student::readFromConsole(istream& in) 
{
         
 Personal::readFromConsole(in);
 
 char s[80];
 cout << "Major: ";

 in.getline(s,80);
 strncpy(major,s,9);
 
 return in;
}

--student.h

#ifndef STUDENT
#define STUDENT

#include "personal.h"

class Student : public Personal {

public:

       Student();
       Student (char*,char*,char*,int,long,char*);
       void writeToFile(fstream&) const;
       void readFromFile(fstream&);

int size() const
{
 return Personal :: size() + majorLen;
}

protected:

char *major;

     const int majorLen;

     ostream& writeLegibly (ostream&);

     friend ostream& operator<<(ostream& out, Student& sr) 
     {    
            return sr.writeLegibly(out);
     }
     istream& readFromConsole(istream&);
     
     friend istream& operator>>(istream& in, Student& sr) 
     {
            return sr.readFromConsole(in);
      }

};

#endif

Edited 5 Years Ago by zhinokin: n/a

As an aside, please do not put

using namespace std;

at the top scope of a header file. You can get away with it when it's just play code for your own use and you know it's there, but if anyone else ever included a header file you wrote with that at the top scope, it would do them no favours at all and when they found out it was your code they would be very upset with you :)

thank you for that moschops. i already remove it now :). But still the problem still their. can you please help me out to trace solve this problem.

This article has been dead for over six months. Start a new discussion instead.