Thread: Converting C++ program into C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Exclamation Converting C++ program into C

    Can anyone help me convert this C++ program into a C program?

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    
        class date
        {
              int month;
              int day;
              int year;
         public:  
              date (int month = 1, int day = 1, int year = 2001)
              {
                   date::month = month;
                   date::day = day;
                   date::year = year;
              };
             
             
             
    
    
    
    
              void showDate();
              ~date(){}
    
    
         };
         
    void date::showDate(){
         cout << month << "/" << day << "/" << year << endl;
         }
    
    
    int main()
    
    
    
    
    {
       int month;
       int day;
       int year;
     
       
       string monthName[12] = {"January","February","March","April","May","June","July",
       "August","September","October","November","December"};
            
    
    
         
         
         cout << "enter month (between 1 and 12)" << endl;
         cin >> month;
     
         
         if (month > 12 || month < 1)
         {
               month = 1;
         }
       
       
         cout << "enter day (between 1 and 31)" << endl;
         cin >> day;
         
         if (day > 31 || day < 1)
         {
               day = 1;
         }
         
         
         cout << "enter year (between 1900 and 2010)"<< endl;
         cin >> year;
         
         if (year > 2010 || year < 1900)
         {
              year = 2001;
         }
          
    
    
       
        date newDate(month, day, year);
        newDate.showDate();
         cout << monthName[month-1]<< " " << day << ", " << year << endl;
         cout << day << " " <<  monthName[month-1] << " " << year << endl;
    
    
        system ("PAUSE");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this? Do you know C? Do you know C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you know that you can interchange C and C++? By using a C interface, you can call your C++ code from C (you would still have to compile C++ code with a C++ compiler, of course).
    Do you also know that you can compile C with a C++ compiler (you will have to adapt your code a little bit, though, since C++ isn't 100% backwards compatible).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Why do you want to do this? Do you know C? Do you know C++?
    I suspect the OP has homework to do in a C class, has found a C++ sample, and is hoping someone will complete their homework by converting the code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting a java program to C
    By rpmischris in forum C Programming
    Replies: 8
    Last Post: 08-09-2012, 08:34 PM
  2. Converting Time Program
    By greeziakattack in forum C Programming
    Replies: 4
    Last Post: 01-18-2012, 01:23 AM
  3. Converting a program from C++ to C
    By pedrocosta03 in forum C Programming
    Replies: 3
    Last Post: 06-28-2011, 10:33 PM
  4. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  5. Why is this program converting to integers
    By 3DPhreak in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 09:44 AM