How to convert C++ code to C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran44
    New Member
    • May 2010
    • 1

    How to convert C++ code to C?

    How can I convert the following code from C++ to C? any software?

    Code:
    #include<time.h>
    #include<fstream>
    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    int sum;
    int q1(char q[60], char a1[20], char a2[20]);
    int q2(char q[60], char a1[20], char a2[20]);
    int q3(char q[60], char a1[20], char a2[20]);
    int q4(char q[60], char a1[20], char a2[20]);
    int q5(char q[60], char a1[20], char a2[20]);
    int q6(char q[60], char a1[20], char a2[20]);
    int q7(char q[60], char a1[20], char a2[20]);
    int q8(char q[60], char a1[20], char a2[20]);
    int q9(char q[60], char a1[20], char a2[20]);
    int q10(char q[60], char a1[20], char a2[20]);
    
    int tem(char q[60], char a1[20], char a2[20]);
    int tem(char q[60], char a1[20], char a2[20]);
    void imran();
    int record();
    int main(){
      char q[60];
      char a1[20];
      char a2[20];
      printf ("\nChoose either the options 1, 2 or 3 depending on the answer next to it.\n\n";
      sum = q1(q, a1, a2);
      printf ( "\n");
      sum = q2(q, a1, a2);
      printf ( "\n");
      sum = q3(q, a1, a2);
      printf ( "\n"); 
      sum = q4(q, a1, a2);
      printf ( "\n");
      sum = q5(q, a1, a2);
      printf ( "\n");
      sum = q6(q, a1, a2);
      printf ( "\n");
      sum = q7(q, a1, a2);
      printf ( "\n"); 
      sum = q8(q, a1, a2);
      printf ( "\n");
      sum = q9(q, a1, a2);
      printf ( "\n");
      sum = q10(q, a1, a2);
      record();
    }
    
      ////////////////////////////////////////////////////////////////////
      // These functions are the 2 templates for the questions each     //
      // question calls either tem() or tem2()                          // 
      ////////////////////////////////////////////////////////////////////
    
    
    int tem(char q[60], char a1[20], char a2[20])
    {
      int res;
      printf ( q ( "\n");
      printf ("1) "( a1 ("\n");
      printf ("2) "( a2 ("\n");
      printf ("3) Exit program\n";
      scanf  ( res);
    
      if (res==1){
      sum=(sum+10);
      }
      else if (res==2){
      } 
      else if (res==3) {
      printf ( sum (" out of 100\n";
      record();
      exit(1);
      }    
      else
      {  printf ( "You must enter a number between 1 and 3.\n";
      tem(q, a1, a2);
      }
      return sum;
      }
    
    int tem2(char q[60], char a1[20], char a2[20])
    {
      int res;
      printf ( q ( "\n";
      printf ("1) "( a1 ("\n";
      printf ("2) "( a2 ("\n";
      printf ("3) Exit program\n";
      scanf  ( res;
    
      if (res==1){
      }
      else if (res==2){
      sum=(sum+10);
      } 
      else if (res==3) {
      printf ( sum (" out of 100\n";
      record();
      exit(1);
      }    
      else
      {  printf ( "You must enter a number between 1 and 3.\n";
      tem2(q, a1, a2);
      }
      return sum;
      }
    
    
      ////////////////////////////////////////////////////////////////////
      // These functions are the questions and answers that are used in //
      // the question template functions.                               // 
      ////////////////////////////////////////////////////////////////////
     
    
    
    int q1(char q[60], char a1[20], char a2[20])
      { 
      q="What does the command ls stand for?";
      a1="long";
      a2="list";
      sum = tem2(q, a1, a2);
      return sum;
      }
    
    
    
    int q2(char q[60], char a1[20], char a2[20])
      {
      q="What does the cd command stand for";
      a1="Change Directory";
      a2="Choose Definition";
      sum = tem(q, a1, a2);
      return sum;
      }
    
    
    
    int q3(char q[60], char a1[20], char a2[20]) 
      {
      q="What does the -l argument stand for with the command ls?";
      a1="level";
      a2="long";
      sum = tem2(q, a1, a2);
      return sum;
      }
    
    
    
    int q4(char q[60], char a1[20], char a2[20]) 
      {
      q="What does the cal command display on the screen?";
      a1="Calendar";
      a2="Calculator";
      sum = tem(q, a1, a2);
      return sum;
      }
    
    
    
    int q5(char q[60], char a1[20], char a2[20])
      {
      q="What does the cp command stand for?";
      a1="Chop";
      a2="Copy";
      sum = tem2(q, a1, a2);
      return sum;
      }
    
    
    
    int q6(char q[60], char a1[20], char a2[20])
      {
      q="What does the rm command stand for?";
      a1="Remove";
      a2="Rename";
      sum = tem(q, a1, a2);
      return sum; 
      }
    
    
    
    int q7(char q[60], char a1[20], char a2[20])
      {
      q="What does the pwd command stand for?";
      a1="Password";
      a2="Print Working Directory";
      sum = tem2(q, a1, a2);
      return sum;
      }
    
    
    
    int q8(char q[60], char a1[20], char a2[20])
      {
      q="What does the mkdir command stand for?";
      a1="Make Direct";
      a2="Make Directory";
      sum = tem2(q, a1, a2);
      return sum;
      }
    
    
    
    int q9(char q[60], char a1[20], char a2[20])
      {
      q="What does the history command print on the screen?";
      a1="Command History";
      a2="Web History";
      sum = tem(q, a1, a2);
      return sum;
      }
      
    
    
    int q10(char q[60], char a1[20], char a2[20])
      {
      q="What is the fdisk program";
      a1="Disk Partitioner";
      a2="File Browser";
      sum = tem(q, a1, a2);
      return sum;
      }
    
      ////////////////////////////////////////////////////////////////////
      // This function calculates the date and inputs it and the users  //
      // score into a file called bashquizresults.txt in the same       //
      // direcory as the program                                        // 
      ////////////////////////////////////////////////////////////////////
    
    
      void imran()
      {
      time_t t = time(0);
      tm time = *localtime(&t);
      ofstream results;
      results.open ("q.txt", ios::app);
      results ( "The date of the quiz was " ( time.tm_mday ( '/' ( time.tm_mon + 1 ( '/' (
      time.tm_year + 1900 ( " Your score was " ( sum (" out of 100." "\n";;
      results.close(); 
      }
      
      ////////////////////////////////////////////////////////////////////
      // This function asks the user if they would like to record their //
      // results in a file and calls the function imran()                  //
      //////////////////////////////////////////////////////////////////// 
    
      int record()
      {
      int c;
      printf("\n"("Your score is "( sum ( " out of 100" ( "\n");
      printf( "Would you like to record your results?\n");
      printf ("1) Yes (\n");
      printf ("2) No  (\n");
      scanf ( c;
      
      if (c==1){
      imran();
      printf ( "Your results have been recorded in q.txt in the same directory as this program.\n");
      }
      else if (c==2){
      return 0;
      }
      else {
      printf ( "You must enter either 1 or 2!\n");
      record();
      }
      }
    Last edited by Niheel; May 21 '10, 08:21 AM. Reason: code tags, added description, changed title
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Most of that is already C in fact part from getting rid of the C++ headers and re-writing imran() to use C file handing functions there looks like there is very little to do to me.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      C++ code is generally not convertible back to C.

      Some C++ syntax does not exist in C. Like templates, classes, inline functions, etc.

      Other things in C++ look the same as in C but work differently.

      Good luck.

      Comment

      Working...