can someone help me convert c++ into c? code listed below #include <iostream> #i
ID: 3686712 • Letter: C
Question
can someone help me convert c++ into c? code listed below
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "commonhdr.h"
#define MAXHOSTNAME 256
using namespace std;
//songDet songInfm;
int display_menu()
{
int ch;
cout<<endl;
cout<<" 1. ADD "<<endl;
cout<<" 2. INSERT"<<endl;
cout<<" 3. VIEW"<<endl;
cout<<" 4. DELETE"<<endl;
cout<<" 5. QUIT"<<endl;
cout<<" Enter your choice :";
cin>>ch;
return ch;
}
main()
{
struct sockaddr_in remoteSocketInfo;
struct hostent *hPtr;
int socketHandle;
const char *remoteHost="localhost";
int portNumber = 8080;
bzero(&remoteSocketInfo, sizeof(sockaddr_in)); // Clear structure memory
// Get system information
if((hPtr = gethostbyname(remoteHost)) == NULL)
{
cerr << "System DNS name resolution not configured properly." << endl;
cerr << "Error number: " << ECONNREFUSED << endl;
exit(EXIT_FAILURE);
}
// create socket
if((socketHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
// Load system information into socket data structures
memcpy((char *)&remoteSocketInfo.sin_addr, hPtr->h_addr, hPtr->h_length);
remoteSocketInfo.sin_family = AF_INET;
remoteSocketInfo.sin_port = htons((u_short)portNumber); // Set port number
if(connect(socketHandle, (struct sockaddr *)&remoteSocketInfo, sizeof(sockaddr_in)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
int rc = 0; // Actual number of bytes read by function read()
string buffer;
char buf[512];
int choice = -1;
string netBuff("");
while(buffer!="QUIT")
{
buffer="";
memset(buf,'',sizeof(buf));
choice = display_menu();
switch(choice)
{
case 1: buffer = "ADD";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Enter song Details" << endl;
cout << "Title :";
cin >> buffer;
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Artist :";
cin >> buffer;
netBuff = "Artist: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Album :";
cin >> buffer;
netBuff = "Album: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Duration :";
cin >> buffer;
netBuff = "Duration: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 2: buffer = "INSERT";
cout << "Enter song Details" << endl;
cout << "Title :";
cin >> buffer;
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Artist :";
cin >> buffer;
netBuff = "Artist: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Album :";
cin >> buffer;
netBuff = "Album: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
cout << "Duration :";
cin >> buffer;
netBuff = "Duration: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 3: buffer = "VIEW";
cout << "Enter song Title :";
cin >> buffer;
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 4: buffer = "DELETE";
cout << "Enter song Title :";
cin >> buffer;
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 5: buffer = "QUIT";
choice =0;
break;
}
// strcpy(buf,buffer.c_str());
//send(socketHandle, (songDet*)&songInfm, sizeof(songInfm), 0);
// send(socketHandle, buf, strlen(buf) + 1, 0);
}
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "commonhdr.h"
#define MAXHOSTNAME 256
//songDet songInfm;
int display_menu()
{
int ch;
printf(" ");
printf(" 1. ADD ");
printf(" 2. INSERT ");
printf(" 3. VIEW ");
printf(" 4. DELETE ");
printf(" 5. QUIT ");
printf(" Enter your choice :");
scanf("%c",ch);
return ch;
}
main()
{
struct sockaddr_in remoteSocketInfo;
struct hostent *hPtr;
int socketHandle;
const char *remoteHost="localhost";
int portNumber = 8080;
bzero(&remoteSocketInfo, sizeof(sockaddr_in)); // Clear structure memory
// Get system information
if((hPtr = gethostbyname(remoteHost)) == NULL)
{
cerr << "System DNS name resolution not configured properly." << endl;
cerr << "Error number: " << ECONNREFUSED << endl;
exit(EXIT_FAILURE);
}
// create socket
if((socketHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
// Load system information into socket data structures
memcpy((char *)&remoteSocketInfo.sin_addr, hPtr->h_addr, hPtr->h_length);
remoteSocketInfo.sin_family = AF_INET;
remoteSocketInfo.sin_port = htons((u_short)portNumber); // Set port number
if(connect(socketHandle, (struct sockaddr *)&remoteSocketInfo, sizeof(sockaddr_in)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
int rc = 0; // Actual number of bytes read by function read()
char buffer[512];
char buf[512];
int choice = -1;
char netBuff[512];
while(buffer!="QUIT")
{
buffer="";
memset(buf,'',sizeof(buf));
choice = display_menu();
switch(choice)
{
case 1: buffer = "ADD";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Enter song Details ");
printf("Title :");
scanf("%s",buffer);
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Artist :");
scanf("%s",buffer);
netBuff = "Artist: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Album :");
scanf("%s", buffer);
netBuff = "Album: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Duration :");
scanf("%s",buffer);
netBuff = "Duration: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 2: buffer = "INSERT";
printf("Enter song Details ");
printf("Title :");
scanf("%s",buffer);
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Artist :");
scanf("%s",buffer);
netBuff = "Artist: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Album :");
scanf("%s",buffer);
netBuff = "Album: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
printf("Duration :");
scanf("%s",buffer);
netBuff = "Duration: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 3: buffer = "VIEW";
printf("Enter song Title :");
scanf("%s",buffer);
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 4: buffer = "DELETE";
printf("Enter song Title :");
scanf("%s",buffer);
netBuff = "Title: " + buffer;
strcpy(buf,netBuff.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
break;
case 5: buffer = "QUIT";
choice =0;
break;
}
// strcpy(buf,buffer.c_str());
//send(socketHandle, (songDet*)&songInfm, sizeof(songInfm), 0);
// send(socketHandle, buf, strlen(buf) + 1, 0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.