Need a project done?

C++ Programming Developer

Search This Blog

Convert Url to IP address in C++.

Convert Url to IP address in C++.


//Convert Url to IP address in C++.
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
//#include<<errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>

using namespace std;

int hostname_to_ip(char * hostname , char* ip); // url to ip

int main(){
    char hn[] = "www.google.com";
    char *hostname = hn;
    char ip[100];
    hostname_to_ip(hostname, ip);
    printf("%s resolved to %s" , hostname , ip);
    return 0;
}

int hostname_to_ip(char * hostname , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;

if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}

addr_list = (struct in_addr **) he->h_addr_list;

for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}

return 1;
}

Multiply 2 Matrices of any order in C++


//Multiply 2 Matrices of any order in C++
#include <iostream>
using namespace std;

int main()
{
cout << "Size of Matrix A:";
int rows1, columns1;
cin>>rows1>>columns1;
//Initializing a 2-d dynamic array.
int ** m1 = new int *[rows1];

for (int i = 0; i<rows1; i++)
m1[i] = new int [columns1];

//Inputing Matrix.
cout << "Enter Matrix A\n";
for (int r = 0; r<rows1; r++)
for (int c = 0; c<columns1; c++)
cin>>m1[r][c];
//--------------------------------------
cout << "Size of Matrix B:";
int rows2, columns2;
cin>>rows2>>columns2;

int ** m2 = new int *[rows2];

for (i = 0; i<rows2; i++)
m2[i] = new int [columns2];

//Inputing Matrix.
cout << "Enter Matrix B\n";
for ( r = 0; r<rows2; r++)
for (int c = 0; c<columns2; c++)
cin>>m2[r][c];
//--------------------------------------

//Printing Matrix A.
cout << "\nA:\n";
for ( r = 0; r<rows1; r++)
{
for (int c = 0; c<columns1; c++)
cout << m1[r][c] << ' ';

cout << endl;
}

//Printing Matrix B.
cout << "\nB:\n";
for ( r = 0; r<rows2; r++)
{
for (int c = 0; c<columns2; c++)
cout << m2[r][c] << ' ';

cout << endl;
}


int result = 0;
//A*B

cout << endl <<endl << "A*B =";
for (int constR = 0; constR < rows1; constR++)
{
cout << endl;
for (int constC = 0; constC < columns2; constC++)//for Next element
{
result = 0;
r = 0;
for (int c = 0; r<rows2; r++,c++)//calculating each element
{
result +=  m1[constR][c] * m2[r][constC];
}
cout << result<< ' ';
}
}

return 0;
}

Get Desktop Path Dynamically in C++

This program automatically finds the path of desktop in C++.
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetKnownFolderPath
using namespace std;

int _tmain() {
    PWSTR pPath = NULL; // this is always a wchar_t*

    // There is no Ansi (char*) version of this function so the TCHAR trick
    // doesn't work here
    HRESULT hr = SHGetKnownFolderPath( FOLDERID_Desktop,
                                       0,    // no flags
                                       NULL, // no tokens
                                       &pPath ); // note we're passing the address here

    if(SUCCEEDED(hr)) {
        wcout << L"Desktop path = " << pPath << endl;
        CoTaskMemFree(pPath); // free memory with correct deallocator function
    }

    return 0;
}

Get Desktop Path Dynamically in C++

This program automatically finds the path of desktop in C++.
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetFolderPath

using namespace std;

int main(int argc, _TCHAR* argv[])
{
TCHAR path[_MAX_PATH] = _T(""); // must be _MAX_PATH in size

HRESULT hr = SHGetFolderPath(NULL, // no parent window
CSIDL_DESKTOP,
0,    // no flags
NULL, // no tokens
path);

if (SUCCEEDED(hr)) {
#ifdef _UNICODE
wcout << L"Desktop path = " << path << endl;
#else
cout << "Desktop path = " << path << endl;
#endif
}
}

How to Convert Int to String in C++

I've created my own function to convert an integer (int) to String in C++.

#include "stdafx.h"
#include <string>
#include <iostream>
#include <stack>

using namespace std;

string intToString(int no);

int main(int argc, _TCHAR* argv[])
{
cout << "These are all strings numbers" << endl;
for (int i = 0; i < 20; i++)
cout << intToString(i) << endl;
}

string intToString(int no){
string s = "";
string s2 = "";
if (no <= 9 && no >= 0){
char c = no + 48;
s += c;
return s;
}
else{
do{
char c = no % 10;
s += c + 48;
no /= 10;
} while (no != 0);
//Reverse string
for (int i = s.length() - 1; i >= 0; i--)
{
s2 += s[i];
}
return s2;
}
}

Built in Stack in C++ - STL

#include "stdafx.h"
#include <iostream>
#include <stack>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
stack<int> st;
st.push(1);
cout << "Pushed: " << st.top() << endl;
st.push(2);
cout << "Pushed: " << st.top() << endl;
st.push(3);
cout << "Pushed: " << st.top() << endl << endl;
while (!st.empty())
{
cout << "Emptying..." << endl;
st.pop();
}
//cout << "Stack top now: " << st.top() << endl;
}

GCM - Greatest Common Multiple Program in C++

Program to calculate GCM ( Greatest Commmon Multiple ) in C++ is given below:

// GCM - Greateset Common Multiple program in C++
#include "stdafx.h" //COMMENT THIS LINE IF IT CAUSES ERROR
#include <iostream>

Typecasting in C++

Typecasting in C++ is shown below by typecasting an integer to a character.

//integer to character typecasting
#include <iostream>
using namespace std;

Get System Time and Date in C++

This program gets system time and date in C++.

//How to get system time in C++.
#include <iostream>
#include <ctime>

Mouse Handling in C++

Following is the program for mouse handling in C++ when user left clicks.

#include <iostream>
using namespace std;

Dynamically allocate memory in C++

Dynamically allocate memroy in C++ using following program:


#include <iostream>
using namespace std;

Stack using linked list in C++


Stack using linked list in C++ program is given below:

//Stack using linked list in C++ using templates
#include <iostream>
using namespace std;

Stack using Link List in C++

Program to create a stack using link list in C++.

//Stack using Link List in C++
#include <iostream>
using namespace std;

Stack in C++ | Program to create a stack in C++

Stack in C++ program is given below:

//Stacks in C++
#include <iostream>
using namespace std;

Swap without using variable - C++


Swap two values without using a variable in C++ program is given below:

//Program to swap 2 numbers without using a variable in C++.
#include <iostream>
using namespace std;

Binary Tree in C++


Binary Tree program in C++ is given below:

See First: Binary Tree in C++ (A relatively easier Binary Tree program)

// Binary Tree in C++
// Binary Tree program to insert generically in the binary tree.

#include "stdafx.h" //Comment This line if it creates error.
#include <iostream>
using namespace std;

Printing Linked List Recursively in C++

Printing Linked List Recursively in C++ Program is given below:


// Printing Linked List Recursively in C++
// Linked List is created by inserting at head and then printing recursively.
#include <iostream>
using namespace std;

Operator Overloading: Overload << and >> operator


Overload << and >> operator program is given below:

// operator overloading
// overloading << and >> operator
#include "stdafx.h" //Delete this line if it creates error
#include <iostream>
using namespace std;
const int LEN = 80;           //maximum length of names
////////////////////////////////////////////////////////////////
class employee                        //employee class
{
private:
char name[LEN];                 //employee name
unsigned long number;
public:
friend istream& operator >> (istream& s, employee& e);
friend ostream& operator << (ostream& s, employee& e);
};
//-------------------------------------------------------------
istream& operator >> (istream& s, employee& e){
cout << "Name: "; s.getline(e.name,LEN,'\n');
cout << "Number: "; s >> e.number;
return s;
}
//-------------------------------------------------------------
ostream& operator << (ostream& s, employee& e){
cout << "Name: " << e.name << endl
<< "Number: " << e.number << endl;
return s;
}
///////////////////////////////////////////////////////////////
int main(){
employee oe; // oe => object of class employee.
cin >> oe;
cout << oe;
return 0;
}

Printing Linked List in Reverse Order Recursively in C++


Printing Linked List in Reverse Order Recursively in C++ Program is given below:

// Printing Linked List in Reverse Order Recursively in C++
// Insertion at head is done in the following program
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
struct link                           //one element of list
{
int data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
class linklist                        //a list of links
{
private:
link* first;                    //pointer to first link also known as head.
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
link* getHead(){ return first; }
void additem(int d);            //add data item (one link)
void displayRecursivelyReversed(link* current);                 //display all links in REVERSE ORDER.
};
//-------------------------------------------------------------void
void linklist::additem(int d)         //add data item
{
link *newLink = new link;
newLink->data = d;
//insert at head
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
void linklist::displayRecursivelyReversed(link* current)              //display all links
{
if (current != NULL){
//move to next node.
displayRecursively(current->next);
cout << current->data << endl;
}

}
////////////////////////////////////////////////////////////////
int main()
{
linklist li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.additem(64);
li.displayRecursivelyReversed(li.getHead());      //display entire list
return 0;
}

Linked List using Templates in C++


Linked List using Templates in C++ program is given below:


// linkedlist.cpp
// linked list using templates
// See also: Link List program in C++

#include "stdafx.h" //Remove this line if you see any error.
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
template <typename T>
struct link                           //one element of list
{
T data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
template <class T>
class linklist                        //a list of links
{
private:
link<T>* first;                    //pointer to first link
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
void additem(T d);            //add data item (one link)
void display();                 //display all links
};
//-------------------------------------------------------------void
template <class T>
void linklist<T>::additem(T d)         //add data item
{
link<T> *newLink = new link<T>;
newLink->data = d;
//insert at head of link list
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
template <class T>
void linklist<T>::display()              //display all links
{
link<T>* current = first;             //set ptr to first link
while( current != NULL )           //quit on last link
{
cout << current->data << endl;  //print data
current = current->next;        //move to next link
}
}
////////////////////////////////////////////////////////////////

int main()
{
linklist<int> li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.display();      //display entire list

linklist<char> lch;      //make linked list
lch.additem('a');    //add four items to list
lch.additem('b');
lch.additem('c');
lch.display();      //display entire list

return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."