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.