I am not familiar with C code. I want to convert this code to C++ and run it

any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<cstdlib>
using std::rand;
using std::srand;


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*Global Declarations */
/* define a new type KEY_TYPE as a synonym for int */
typedef int KEY_TYPE;

typedef struct
   {
    KEY_TYPE key;  
    /* Other data could go here */
   } DATA;

typedef struct nodeTag
   {
    DATA            data;
    struct nodeTag *link;
   } NODE;

/*Prototype Declarations */
void printList (NODE * pList);
void printSumAndAvg (NODE * pList);
void freeList (NODE * pList);

int main (void)
{
/*Local Definitions */
int   num;
NODE *pList = NULL;   /* pointer to start of the list */
NODE *pNew;           /* pointer to use when creating a new node */
NODE *pRear = NULL;   /* pointer to end of the list */
int i;                /* loop counter */

freeList(pList);  /* free the memory we malloc'd for the Nodes */
pList = NULL;  /* set the list to be empty again */

srand ( time(NULL) ); /* seed rand with a random seed to avoid pseudorandom number generation.  
							Takes current time in milliseconds. */

for (i=0; i < 25; i++)  /* we'll loop 25 times */
   {
             
    /* create a NODE by calling malloc to get the memory */
    /* store a pointer to the new NODE in the pointer variable pNew */
    pNew = (NODE *) malloc (sizeof (NODE));
    
    /* if pNew is NULL that means the malloc failed */
    if (!pNew)
       {
        printf ("\aCan not allocate node.\n");
        return 100;
       } /* if */

	
    /* set num to a random number between 0 and 100 */
    num = rand() % 101;
 
    
    /* store the int num in the key field of the DATA struct */
    pNew->data.key = num;
    
    /* set the pNew link to NULL since it will be added at the end */
    /* of the list -- there will be no node after it  */
    pNew->link     = NULL;
    if (pList == NULL) /* first node */
       pList = pRear = pNew;
    else /* other nodes */
       {
         pRear->link = pNew;
         pRear       = pNew;
       } /* else */
   } /* while */

   /* we have generated a linked list of 25 random ints */
   printf("\n\nLinked list of 25 random ints from 0-100 generated.\n\n");

   /* print it out */
   printList (pList);

   /* print sum and average  */
   printSumAndAvg(pList);
   
   return 0;
}
void printList (NODE *pList)
{
NODE *pWalker;
int lineCount = 0;

/*Statements */
pWalker = pList;         /* start at the first node in the list */
printf ("\nList contains:\n");

while (pWalker) /* stop when pWalker = NULL; there's no more list then */
   {
    if (++lineCount > 10)  /* for spacing - add a new line after every 10 ints */
       {
        lineCount = 1;
        printf ("\n");
       } /* if */
       
    /* print the int stored in the Node pointed to by pWalker */
    printf ("%d => ", pWalker->data.key);
    
    /* move pWalker to point to the next node in the list */
    pWalker = pWalker->link;
   } /* while */
printf ("<End of List>\n\n");
return;
}/* printList */ 


void printSumAndAvg (NODE *pList)
{
                                               
   NODE *pWalker;   /* pointer to the next node in the list */     
   int sum;         /* total of all the ints in the list  */
   int count;       /* number of ints in the list */
   float avg;       /* average */
   
   pWalker = pList;         /* start at the first node in the list */
   sum = 0;                 /* start the sum at 0 */
   count = 0;               /* ditto for count */
   


   while (pWalker) /* stop when pWalker = NULL; there's no more list then */
   {    
       
       sum += pWalker->data.key;
       count++;
    
       /* move pWalker to point to the next node in the list */
       pWalker = pWalker->link;
       
   } /* while */

   /* compute the average */
   avg = (float) sum / count;
   
   /* print the sum and average */
   printf("Sum = %d Average = %.2f\n", sum, avg);
   return;
}
void freeList (NODE *pList)
{
   NODE *pWalker;   /* pointer to the next node in the list */
   NODE *link;      /* pointer to save the link to following NODE */
   
   pWalker = pList;         /* start at the first node in the list */


   while (pWalker) /* stop when pWalker = NULL; there's no more list then */
   {

    link = pWalker->link;
       
    /* free the node pointed to by pWalker */
    free(pWalker);
    
    /* move pWalker to point to the next node in the list */
    pWalker = link;
   } /* while */
    system("PAUSE");
   return;
}/* freeList */

Learn C,
Learn C++
Do the conversion manually.

Or try this link to c++ conversion

In the simplest sense, all you need are to modify the #include's to the current C++ forms

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include<cstdlib>

Compile and run. C++ will use the C output methods. If you really want to make it all C++, you'll have to change the printf( ) statements to cout statements. Most in that code will be simple substitutions, some will take a bit of work to format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.