I would like to bypass system configured nameserver and use my own nameserver (or list of them) configured in application. I can do in nslookup in windows. How can I do it in C++ preferably by using boost::asio? I would like to avoid using std::system("nslookup ...> output.txt") and reading the file.
I cannot see where I can specify nameserver to use for lookup in boost::asio.
#include <boost/asio.hpp>
#include <string>
#include <iostream>
using namespace boost;
int main()
{
asio::io_service io_service;
asio::ip::tcp::resolver resolver(io_service);//how to pass specific nameserver?
asio::ip::tcp::resolver::iterator itr = resolver.resolve("bbc.co.uk","");
asio::ip::tcp::resolver::iterator end;
for (int i = 1; itr != end; itr++, i++)
std::cout << "hostname #" << i << ": " << itr->host_name() << " " << itr->endpoint() << '\n';
return 0;
}
You can't. The DNS resvoling is handled by the sockets api and you can't specify the DNS servers.
You will have to directly use either a OS specific API to resolve names like DnsQueryEx in win32 or using a library like LDNS.
/*
* DNSResolver.h
*
* Created on: Jan 12, 2023
* Author: marian
*/
#ifndef DNSRESOLVER_H_
#define DNSRESOLVER_H_
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string_view>
#include <string>
#include <ostream>
#include <iostream>
#include <thread>
#include <chrono>
#include <boost/asio.hpp>
namespace tools
{
template <typename Func>
std::string_view find_first_success(const std::string_view& str,const Func& func, size_t pos0=0, char delim=',')
{
size_t pos=0;
do
{
pos=str.find_first_of(delim,pos0);
std::string_view sv=str.substr(pos0,pos-pos0);
if (func(sv))
return sv;
pos0=pos+1;
} while (pos!=std::string_view::npos);
return {};
}
template<int unused=0>
uint16_t be2uint16(const uint8_t* hi)
{
uint16_t hi1=*(hi+1);
return (((uint16_t)*hi)<<8)|hi1;
}
template<int unused=0>
uint32_t be2uint32(const uint8_t* hi)
{
uint32_t v3=*(hi+3);
uint32_t v2=*(hi+2);
uint32_t v1=*(hi+1);
uint32_t v0=*hi;
return (v0<<24)|(v1<<16)|(v2<<8)|v3;
}
template<int unused=0>
void out_IP4(std::ostream& os,uint32_t IP4)
{
os << (IP4>>24) << '.' << ((IP4>>16)&0xFF) << '.' << ((IP4>>8)&0xFF) << '.' << (IP4&0xFF);
}
}
namespace DNS
{
const char *rcode_msg[]={"No error","Format error","Server failure","Name error","Not implemented","Refused"};
const char *opcode_msg[]={"Standard query","Inverse query","Status"};
const char *type_msg[]={"0","A","NS","MD","MF","CNAME","SOA","MB","MG","MR","NULL","WKS","PTR","HINFO","MINFO","MX","TX"};
/*
A 1 a host address
NS 2 an authoritative name server
MD 3 a mail destination (Obsolete - use MX)
MF 4 a mail forwarder (Obsolete - use MX)
CNAME 5 the canonical name for an alias
SOA 6 marks the start of a zone of authority
MB 7 a mailbox domain name (EXPERIMENTAL)
MG 8 a mail group member (EXPERIMENTAL)
MR 9 a mail rename domain name (EXPERIMENTAL)
NULL 10 a null RR (EXPERIMENTAL)
WKS 11 a well known service description
PTR 12 a domain name pointer
HINFO 13 host information
MINFO 14 mailbox or mail list information
MX 15 mail exchange
TXT 16 text strings
*/
template<int unused=0>
void disp_type(std::ostream&os,uint16_t type)
{
os << (int)type;
if (type<=16)
os << " " << type_msg[type];
}
typedef boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO> rcv_timeout_option; //somewhere in your headers to be used everywhere you need it
template<int unused=0>
class resolver
{
public:
resolver(const std::string_view& nameservers="127.0.0.53",boost::asio::chrono::milliseconds timeout=boost::asio::chrono::milliseconds{3000})
:nameservers_{nameservers},timeout_(timeout)
{}
std::vector<uint32_t> resolve(
const std::string_view& hostname,
std::string_view& nameserver_used,
std::vector<uint32_t>* pTTLs=nullptr)
{
std::vector<uint8_t> result;
result.resize(1024);
nameserver_used=resolve(hostname,result);
if (nameserver_used.empty())
return {};
std::string qname;
std::vector<uint32_t> IP4s;
int cnt=answer_A(result,qname,IP4s,pTTLs);
(void)cnt;
if (hostname!=qname)
return {};
return IP4s;
}
void resolve_ostream(
const std::string_view& hostname,
std::string_view& nameserver_used,
std::ostream& os)
{
std::vector<uint8_t> result;
result.resize(1024);
nameserver_used=resolve(hostname,result);
if (nameserver_used.empty())
return;
os << "nameserver:" << nameserver_used << std::endl;
disp_result(os,result);
}
private:
static void out_name(std::ostream&os,const std::vector<uint8_t>& result,uint16_t& pos)
{
uint8_t sz=result[pos++];
for (;sz;)
{
if (sz>=64)
{
if ((sz>>6)!=3)
throw "label size exceed 64 character and not pointer, see RFC 1035";
uint16_t offset=(((uint16_t)(sz&63))<<8)|result[pos++];
out_name(os,result,offset);//recursion, hope not too deep
return;
}
uint16_t epos=pos+sz;
while (pos<epos)
os << result[pos++];
sz=result[pos++];
if (sz)
os << ".";
}
}
static void disp_rr(std::ostream&os,const std::vector<uint8_t>& result,uint16_t& pos)
{
if (pos>=result.size())
return;
if (result[pos]==0)
return;
out_name(os,result,pos);
if (((size_t)pos+10)>=(size_t)result.size())
{
throw "wrong packet size1";
}
uint16_t rr_type=tools::be2uint16(&result[pos]);
os << " (" ;
os << "type:" ; disp_type(os,rr_type) ;
pos+=2;
os << " class:" << tools::be2uint16(&result[pos]) ;
pos+=2;
os << " TTL:" << tools::be2uint32(&result[pos]) << " secs) ";
pos+=4;
uint16_t len=tools::be2uint16(&result[pos]);
pos+=2;
if (pos+len>result.size())
throw "wrong packet size2";
if (rr_type==1)
{
if (len!=4)
{
throw "unknown data size";
}
os << (int)result[pos] << "." << (int)result[pos+1] << "." << (int)result[pos+2] << "." << (int)result[pos+3];
} else if (rr_type==2)
{
uint16_t pos2=pos;
out_name(os,result,pos2);
} else
{
throw "unimplement type";
}
pos+=len;
}
static void split_rr(const std::vector<uint8_t>& result,
uint16_t& pos,
std::string& name,
uint16_t& rr_type,
uint16_t& rr_class,
uint32_t& TTL,
uint32_t& IP4,
std::string* pname2=nullptr
)
{
if (pos>=result.size())
return;
if (result[pos]==0)
return;
std::stringstream ss;
out_name(ss,result,pos);
name=ss.str();
if (((size_t)pos+10)>=(size_t)result.size())
{
throw "wrong packet size1";
}
rr_type=tools::be2uint16(&result[pos]);
pos+=2;
rr_class=tools::be2uint16(&result[pos]) ;
pos+=2;
TTL=tools::be2uint32(&result[pos]);
pos+=4;
uint16_t len=tools::be2uint16(&result[pos]);
pos+=2;
if (pos+len>result.size())
throw "wrong packet size2";
if (rr_type==1)
{
if (len!=4)
{
throw "unknown data size";
}
IP4=tools::be2uint32(&result[pos]); //*(uint32_t*)&result[pos];
} else if (rr_type==2)
{
uint16_t pos2=pos;
std::stringstream ss;
out_name(ss,result,pos2);
if (pname2)
*pname2=ss.str();
} else
{
throw "unimplement type";
}
pos+=len;
}
static int check_A(const std::vector<uint8_t>& result)
{
if (result.size()<12)
return -1;
uint8_t response=(result[2]>>7);
if (response!=1)
return -2;
uint8_t rcode=result[3]&0xf;
if (rcode!=0)
return -3;
uint8_t opcode=(result[2]>>3)&0xF;
if (opcode!=0)
return -4;
return 0;
}
static int answer_A(const std::vector<uint8_t>& result,
std::string& q_name,
std::vector<uint32_t>& IP4s,
std::vector<uint32_t>* pTTLs=nullptr)
{
int ret=check_A(result);
if (ret<0)
return ret;
std::string name;
uint16_t q_type=0;
uint16_t q_class=0;
uint16_t rr_type=0;
uint16_t rr_class=0;
uint32_t TTL=0;
uint32_t IP4=0;
uint16_t qc=tools::be2uint16(&result[4]);
uint16_t pos=12;
for (int i=0;i<qc;i++)//qc =1 usually
{
std::stringstream ss;
out_name(ss,result,pos);
q_name=ss.str();
q_type=tools::be2uint16(&result[pos]);
pos+=2;
q_class=tools::be2uint16(&result[pos]);
pos+=2;
}
uint16_t ac=tools::be2uint16(&result[6]);
IP4s.resize(ac);
if (pTTLs)
pTTLs->resize(ac);
for (int i=0;i<ac;i++)
{
split_rr(result,pos,name,rr_type,rr_class,TTL,IP4);
if (name!=q_name)
break;
if (rr_type!=q_type)
break;
if (rr_class!=q_class)
break;
IP4s[i]=IP4;
if (pTTLs)
(*pTTLs)[i]=TTL;
}
return ac;
}
static void disp_result(std::ostream&os,const std::vector<uint8_t>& result)
{
if (result.size()<12)
return;
os << std::string((result[2]&0x80) ? "response" : "query") << std::endl;
os << "size=" << result.size() << " bytes" << std::endl;
uint8_t opcode=(result[2]>>3)&0xF;
os << "Opcode:" << (int)opcode;
if (opcode<=2)
os << " " << opcode_msg[opcode];
os << std::endl;
os << ((result[2]&1) ? "recursion asked" : "recursion NOT asked") << std::endl;
if (result[2]&2)
os << "response truncated" << std::endl;
if (result[2]&4)
os << "Authoritative answer" << std::endl;
os << ((result[3]&0x80) ? "recursion available" : "recursion NOT available") << std::endl;
uint8_t rcode=result[3]&0xf;
os << "rcode:" << (int)rcode;
if (rcode<=5)
os << " " << rcode_msg[rcode];
os << std::endl;
uint16_t qc=tools::be2uint16(&result[4]);
os << "Query Count:" << qc << std::endl;
uint16_t ac=tools::be2uint16(&result[6]);
os << "Answer Count:" << ac << std::endl;
uint16_t nc=tools::be2uint16(&result[8]);
os << "Authoritative Name Server Count:" << nc << std::endl;
uint16_t arc=tools::be2uint16(&result[10]);
os << "Additional resource records Count:" << arc << std::endl;
uint16_t pos=12;
os << "_____________" << std::endl;
os << "Query:" << std::endl;
for (int i=0;i<qc;i++)//qc =1 usually
{
out_name(os,result,pos);
os << " (";
os << "type:"; disp_type(os,tools::be2uint16(&result[pos]));
pos+=2;
os << " class:" << tools::be2uint16(&result[pos]) << " ) ";
pos+=2;
os << std::endl;
}
//answer
if (ac)
{
os << "_________________________" << std::endl;
os << "Answer:" << std::endl;
}
for (int i=0;i<ac;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
if (nc)
{
os << "_________________________" << std::endl;
os << "Authoritative nameservers:" << std::endl;
}
for (int i=0;i<nc;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
if (arc)
{
os << "_________________________" << std::endl;
os << "Additional resource records:" << std::endl;
}
for (int i=0;i<arc;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
}
size_t udp_request(const boost::asio::const_buffer& request,
const boost::asio::mutable_buffer& response,
const std::string_view& destination_ip,
const unsigned short port,
boost::system::error_code& ec)
{
using namespace boost;
asio::ip::udp::socket socket(io_context_);
auto remote = asio::ip::udp::endpoint(asio::ip::make_address(destination_ip), port);
socket.open(boost::asio::ip::udp::v4());
size_t sent=socket.send_to(request, remote);
if (request.size()!=sent)
return 0;
return receive_from(socket,response,timeout_,ec);
}
std::size_t receive_from(
boost::asio::ip::udp::socket& sock,
const boost::asio::mutable_buffer& buffer,
boost::asio::chrono::steady_clock::duration timeout,
boost::system::error_code& ec)
{
std::size_t length = 0;
sock.async_receive(boost::asio::buffer(buffer),
[&](const boost::system::error_code& ec1,std::size_t sz){
ec=ec1;
length=sz;
}
);
run(sock,timeout);
return length;
}
void run(boost::asio::ip::udp::socket& sock,boost::asio::chrono::steady_clock::duration timeout)
{
// Restart the io_context, as it may have been left in the "stopped" state
// by a previous operation.
io_context_.restart();
// Block until the asynchronous operation has completed, or timed out. If
// the pending asynchronous operation is a composed operation, the deadline
// applies to the entire operation, rather than individual operations on
// the socket.
io_context_.run_for(timeout);
// If the asynchronous operation completed successfully then the io_context
// would have been stopped due to running out of work. If it was not
// stopped, then the io_context::run_for call must have timed out.
if (!io_context_.stopped())
{
// Cancel the outstanding asynchronous operation.
sock.cancel();
// Run the io_context again until the operation completes.
io_context_.run();
}
}
static std::vector<uint8_t> make_dns_request(const std::string_view& hostname)
{
static uint16_t id=257;
id++;
std::vector<uint8_t> req;
uint16_t epos=12+1+hostname.size();
req.resize(epos+1+4);//plus null plus type (2 bytes) plus class (2 bytes)
*((uint16_t*)&req[0])=id;
req[2]=1;//recursive
req[3]=32;//??, linux has 32, but 0 works too
req[5]=1;//one query
req[epos]=0;//end of string
req[epos+2]=1;//type A - host address
req[epos+4]=1;//class INT
uint16_t cnt=0;
for(int i=hostname.size()-1;i>=0;i--)
{
req[13+i]=(hostname[i]=='.') ? cnt : hostname[i];
cnt=(hostname[i]=='.') ? 0 : cnt+1;
}
req[12]=cnt;
return req;
}
std::string_view resolve(const std::string_view& hostname,std::vector<uint8_t>& result)
{
auto myrequest=make_dns_request(hostname);
return tools::find_first_success(nameservers_,
[&](const std::string_view& nameserver)
{
size_t sz=0;
try
{
boost::system::error_code ec;
sz=udp_request(boost::asio::buffer(myrequest),
boost::asio::mutable_buffer(&result[0],result.size()),
nameserver,53,ec);
if (ec.value()!=boost::system::errc::success)
{
std::cerr << "skipping due to error code not a success : error code=" << ec.value() << " " << ec.message() << std::endl;
return false;
}
if (sz<35)//min packet , 12 header+3 name+4 type/class+ 16 (rr type 1)
{
std::cerr << "skipping because packet size < 35" << std::endl;
return false;
}
if ((myrequest[0]!=result[0])||(myrequest[1]!=result[1]))
{
std::cerr << "skipping because ID respond not matching ID request " << std::endl;
return false;
}
if (check_A(result)<0)
{
std::cerr << "skipping because packet check failed " << std::endl;
return false;
}
}
catch(std::exception& ex)
{
std::cerr << "Skipping because of exception:" << ex.what() << std::endl;
return false;
}
catch(...)
{
std::cerr << "Skipping because of unknown exception:" << std::endl;
return false;
}
result.resize(sz);
return true;
});
}
private:
boost::asio::io_context io_context_;
std::string nameservers_;
boost::asio::chrono::milliseconds timeout_;
};
}
#endif /* DNSRESOLVER_H_ */
/*
* main.cpp
*
* Created on: Jan 12, 2023
* Author: marian
*/
#include <iostream>
#include <vector>
#include "DNSResolver.h"
#define check_offset if (offset==argc) { print_error(); return 1;}
int main (int argc, char **argv) {
auto print_error=[&]()
{
std::cerr << "Usage: " << argv[0] << " [-v] [-t timeout_msecs] [--nottl] [--nons] <query> [csv_ip_nameserver_list] \n";
std::cerr << "Example: " << argv[0] << " bbc.co.uk 127.0.0.53,8.8.8.8" << std::endl;
std::cerr << "Example: " << argv[0] << " -v bbc.co.uk" << std::endl;
};
if (argc < 2) {
print_error();
return 1;
}
int offset=1;
bool verbose= (std::string(argv[offset])=="-v");
if (verbose)
offset++;
check_offset;
int millisecs=3000;
if (std::string(argv[offset])=="-t")
{
offset++;
check_offset;
millisecs=std::stoi(argv[offset++]);
}
check_offset;
bool ttl=true;
if (std::string(argv[offset])=="--nottl")
{
ttl=false;
offset++;
}
check_offset;
bool ns=true;
if (std::string(argv[offset])=="--nons")
{
ns=false;
offset++;
}
check_offset;
const char *dname=argv[offset];
const char *nameservers= (offset+1<argc) ? argv[offset+1] : "127.0.0.53,8.8.8.8";
if (ns)
std::cout << "nameservers:" << nameservers << std::endl;
DNS::resolver resolver{nameservers,boost::asio::chrono::milliseconds{millisecs}};
if (verbose)
{
std::string_view nameserver_used;
resolver.resolve_ostream(dname,nameserver_used,std::cout);
return 0;
}
std::vector<uint32_t> TTLs;
std::string_view nameserver_used;
std::vector<uint32_t> IP4s=resolver.resolve(dname,nameserver_used,&TTLs);
if (ns&&(!nameserver_used.empty()))
std::cout << "nameserver:" << nameserver_used << std::endl;
int n=IP4s.size();
if (!n)
{
std::cerr << "Error: No IPs found" << std::endl;
return 2;
}
for (int i=0;i<n;i++)
{
tools::out_IP4(std::cout,IP4s[i]);
if (ttl)
std::cout << " (TTL:" << TTLs[i] << " sec)" ;
std::cout << std::endl;
}
return 0;
}
The problem is it prints the full name but not the rest of the lines about the person.
Could someone, please guide me?
I do really appreciate your help!
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << search << std::endl;
std::cout << "\nFull name: " << search << std::endl;
} else {
std::cout << "Match not found "<< std::endl;
}
There are a few style problems with your code:
No need to explicitly initialize strings, they will be empty by default (see here).
Keep a consistent style. For example, either always start brackets in the same line as the function signature or in the next line.
No need to close the file explicitly at the end of the function, this is done when the object goes out of scope (see (destructor) here).
No need to include <map> and <iomanip> headers.
Don't keep unused variables.
Give suggestive names to your variables.
Do not return error codes to the OS when the app is working as it should. Not finding a name is not an error, is it?
It seems your file has 6 entries per contact, so all you have to do is print 5 more lines. You do not need to store the lines in a vector, just parse and print them as you go. Here is an example:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
void findContact(std::string fullName, std::string contactListPath) {
std::ifstream inFile{contactListPath};
if (!inFile) {
std::cerr << "File could not be open!" << std::endl;
return;
}
std::string line;
while (std::getline(inFile, line)) {
if (line == fullName) {
std::cout << "Match found: \n";
std::cout << "\nFull name: " << fullName;
std::cout << "\nAddress: " << (std::getline(inFile, line), line);
std::cout << "\nE-mail: " << (std::getline(inFile, line), line);
std::cout << "\nPhone: " << (std::getline(inFile, line), line);
std::cout << "\nBirthday: " << (std::getline(inFile, line), line);
std::cout << "\nNote: " << (std::getline(inFile, line), line) << std::endl;
return;
}
}
std::cout << "Match not found " << std::endl;
}
int main() {
std::string fullName;
std::string contactListPath;
std::cout << "Enter full name to search: ";
std::getline(std::cin, fullName);
std::cout << "Enter path to contact list: ";
std::getline(std::cin, contactListPath);
findContact(fullName, contactListPath);
return 0;
}
If every entry contains 6 lines. Then you can print all the lines starting from the line that you found like:
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << std::endl;
// print the next 6 lines
for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr++,remaining--) {
std::cout << *itr << std::endl;
}
} else {
std::cout << "Match not found "<< std::endl;
}
I download gsoap_2.8.83 and ran into following soap error when executing the example from the online document.
SOAP 1.1 fault SOAP-ENV:Client[no subcode]
"Error 200: HTTP 200 OK"
Detail: [no detail]
The Code is exactly this:
//#include "gsoapWinInet.h"
#include "soapcalcProxy.h"
#include "calc.nsmap"
#include "stdafx.h"
/* the Web service endpoint URL */
const char server[] = "http://websrv.cs.fsu.edu/~engelen/calcserver.cgi";
int main(int argc, char **argv)
{
if (argc < 4)
{
fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n");
exit(1);
}
calcProxy calc(server);
//soap_register_plugin(calc.soap, wininet_plugin);
calc.soap->recv_timeout = 60;
double a, b, result;
a = strtod(argv[2], NULL);
b = strtod(argv[3], NULL);
int ret = 0;
switch (*argv[1])
{
case 'a':
std::cout << a << " + " << b;
ret = calc.add(a, b, result);
break;
case 's':
std::cout << a << " - " << b;
ret = calc.sub(a, b, result);
break;
case 'm':
std::cout << a << " * " << b;
calc.mul(a, b, result);
break;
case 'd':
std::cout << a << " / " << b;
calc.div(a, b, result);
break;
case 'p':
std::cout << a << " ^ " << b;
calc.pow(a, b, result);
break;
default:
fprintf(stderr, "Unknown command\n");
exit(1);
}
if (calc.soap->error)
calc.soap_stream_fault(std::cerr);
else
std::cout << " = " << result << std::endl;
calc.destroy(); /* clean up */
getchar();
return 0;
}
Full VS2015 project can also be fetched here.
Build and run with command line arguments:
main.exe add 1 2
Getting the error abovemetioned.
In order to debug the SOAP message, I added gsoap WinInet plugin (just uncommenting out those two lines in the code above), then the response turns to be good.
My question is why the code without wininet plugin is unsuccessful? Thanks.
Using Windows Management Instrumentation (WMI) in VC++ we can find the SystemInfo like System Name and other properties.
Example for GetComputerName :
BOOL WINAPI GetComputerName(
_Out_ LPTSTR lpBuffer,
_Inout_ LPDWORD lpnSize
);
There are 3 printers attached in my system 1 thermal and 2 Shared printers ,
How can i get the information about printer which is offline ?
How can i classify/list printers based on their status ?
Thanks
See also EnumPrinters
DWORD flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
DWORD bufsize, printerCount;
DWORD level = 2; //2 is for PRINTER_INFO_2
::EnumPrinters(flags, NULL, level, NULL, 0, &bufsize, &printerCount);
if (bufsize)
{
BYTE* buffer = new BYTE[bufsize];
::EnumPrinters(flags, NULL, level, buffer, bufsize, &bufsize, &printerCount);
if (bufsize && printerCount)
{
const PRINTER_INFO_2* info = (PRINTER_INFO_2*)buffer;
for (DWORD i = 0; i < printerCount; i++)
{
if (info->pServerName) cout << "pServerName: " << info->pServerName << endl;
if (info->pPrinterName) cout << "pPrinterName: " << info->pPrinterName << endl;
if (info->pShareName) cout << "pShareName: " << info->pShareName << endl;
if (info->pPortName) cout << "pPortName: " << info->pPortName << endl;
if (info->Attributes & PRINTER_ATTRIBUTE_LOCAL) cout << "[local]\n";
if (info->Attributes & PRINTER_ATTRIBUTE_NETWORK) cout << "[network]\n";
wcout << "status: " << info->Status << endl;
if (info->Status & PRINTER_STATUS_ERROR) cout << "status: error\n";
wcout << endl;
info++;
}
}
delete[] buffer;
}
This program is a nightmare, it wont even give me errors when ran, visual studios tells me nothing and i need some help
#include <iostream>
using namespace std;
class Textbook
{
private:
char *aPtr;
char *tPtr;
int yearPub;
int numPages;
char bookType;
public:
Textbook(char *, char *, int, int, char);
void display();
void operator=(Textbook&);
};
Textbook::Textbook(char*string = NULL, char*string2 = NULL, int ypub = 0, int npages = 0, char btype = 'X')
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
tPtr = new char[strlen(string2) +1];
strcpy(tPtr, string2);
yearPub = ypub;
numPages = npages;
bookType = btype;
}
void Textbook::display()
{
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
cout << "The year it was published is: " << yearPub << endl;
cout << "The number of pages is: " << numPages << endl;
cout << "The initial of the title is: " << bookType << endl;
return;
}
void Textbook::operator=(Textbook& newbook)
{
if(aPtr != NULL) //check that it exists
delete(aPtr);// delete if neccessary
aPtr = new char[strlen(newbook.aPtr) + 1];
strcpy(aPtr, newbook.aPtr);
if(tPtr != NULL) //check that it exists
delete(tPtr); // delete if neccessary
tPtr = new char[strlen(newbook.tPtr) + 1];
strcpy(tPtr, newbook.tPtr);
yearPub = newbook.yearPub;
numPages = newbook.numPages;
bookType = newbook.bookType;
}
void main()
{
Textbook book1("sehwag", "Programming Methods", 2009, 200, 'H');
Textbook book2("Ashwin", "Security Implementation", 2011, 437, 'P');
Textbook book3;
book1.display();
book2.display();
book3.display();
book3 = book1;
book2 = book3;
book1.display();
book2.display();
book3.display();
}
im not sure if the problem lies in the default constructor but that's about the only thing i could think of, but im not sure at all on how to fix it.
Problem is with the default-parameters in the constructor.
You can't do those kind of operations with NULL-pointers.
Textbook book3;
crashes your program.
Change:
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
to:
cout << "The name of the author is: " << aPtr << endl;
cout << "The Title of the book is: " << tPtr << endl;
Also change:
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
to:
if (string != NULL)
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
}
else
{
aPtr = new char[1];
aPtr[0] = '\0';
}
and ditto for tptr and string2.
The reason you need this checking is because you have NULL as a default value for your two string inputs, so when you call the constructor with no arguments (as is the case with book3) these strings are just NULL pointers. Calling functions such as strlen or strcat with a NULL pointer will result in an exception as you have seen.
Ideally you should not be using C-style strings with C++ - use C++ strings instead - this will help to avoid problems such as the above.