Can someone help me convert c++ into c ? the code listed below #include #include
ID: 3686710 • Letter: C
Question
Can someone help me convert c++ into c ?
the code listed below
#include
#include
#include
#include
#include
#include
#include
#include
#include "commonhdr.h"
#define MAXHOSTNAME 256
using namespace std;
//songDet songInfm;
int display_menu()
{
int ch;
cout< cout<<" 1. ADD "< cout<<" 2. INSERT"< cout<<" 3. VIEW"< cout<<" 4. DELETE"< cout<<" 5. QUIT"< 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";
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 3: buffer = "VIEW";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
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";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
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 <iostream.h> // input output stream header file
#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
#include <string.h> // header file with string function
#include <conio.h> // console input output header file
#include "commonhdr.h"
#define MAXHOSTNAME 256
// using namespace std;
//songDet songInfm;
int display_menu()
{
int ch;
printf ( " 1. ADD 2. INSERT 3. VIEW 4. DELETE 5.QUIT ");
printf ( " Enter your choice:");
scanf("%d", &ch); // scanf ( ch
return (ch);
} /* end function display menu */
int main() // start main
{
// convert c++ code c code
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)
{
/* fprintf(stderr) is equivalent to cerr */
fprintf(stderr, "System DNS name resolution not configured properly. ");
fprintf(stderr, ""Error number: %d " , ECONNREFUSED ");
exit(EXIT_FAILURE);
} /* end if hPtr */
/* create socket */
if((socketHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
} /* end if sockt handle */
/* 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;
char buf[512];
int choice = -1;
char * 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);
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";
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 3: buffer = "VIEW";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
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";
strcpy(buf,buffer.c_str());
send(socketHandle, buf, strlen(buf) + 1, 0);
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;
} /* end switch */
/* strcpy(buf,buffer.c_str()); */
/* send(socketHandle, (songDet*)&songInfm, sizeof(songInfm), 0); */
/* send(socketHandle, buf, strlen(buf) + 1, 0); */
} /* end while */
return 0; // exit
} // end of main()
/*
C++ command equivalent C Command
comment double slash // slash * and * slash
scanf scanf
printf printf
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.