Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:fstream is a C++ class which I've never used. You may try it and it can work but I wouldn't know how to help.
I've used it so I'm okay with the it but was just wondering if you knew if it would work. I'll give it a shot and update on how it went later.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

You are going to try to create a Ruby to C++ converter now?
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:You are going to try to create a Ruby to C++ converter now?
Yep. I did some simple stuff: http://codepad.org/XuJefoZw

Here's the Ruby code I used: http://codepad.org/54hIqK98

Some stuff is wrong, other stuff doesn't work, just wanted to show what I was doing. I am at a loss of what .chomp does right now as I haven't programmed in Ruby for a long time.

EDIT: New code:

Code: Select all

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    fstream rubycode;
    rubycode.open("anothertest.rb");
    string rubyline;
    bool semicolon = true, newline = true;
    int i = 0, j;

    while (! rubycode.eof())
    {
        getline(rubycode, rubyline);
        semicolon = true;
        newline = true;

        for (i = 0; i < rubyline.length(); i++)
        {
            //puts to cout
            if (rubyline[i] == 'p' && rubyline[i+1] == 'u' && rubyline[i+2] == 't' && rubyline[i+3] == 's')
            cout << "std::cout << ";
            //writing what string is being put
            if (rubyline[i-5] == 'p' && rubyline[i-4] == 'u' && rubyline[i-3] == 't' && rubyline[i-2] == 's')
            {
                for (j = i; j < rubyline.length(); j++)
                {
                    if (rubyline[j] == '\'')
                    cout << "\"";
                    else
                    cout << rubyline[j];
                }
            }
            //gets
            if (rubyline[i] == 'g' && rubyline[i+1] == 'e' && rubyline[i+2] == 't' && rubyline[i+3] == 's' && rubyline[i+4] != '.')
            cout << "std::cin >> ";
            //gets.
            if (rubyline[i] == 'g' && rubyline[i+1] == 'e' && rubyline[i+2] == 't' && rubyline[i+3] == 's' && rubyline[i+4] == '.')
            cout << "std::cin.";
            //chomp
            if (rubyline[i] == 'c' && rubyline[i+1] == 'h' && rubyline[i+2] == 'o' && rubyline[i+3] == 'm' && rubyline[i+4] == 'p')
            cout << "get()";
            //if
            if (rubyline[i] == 'i' && rubyline[i+1] == 'f')
            cout << "if ";
            //in between the brackets
            if (rubyline[i] == '(')
            {
                for (j = i; j < rubyline.length(); j++)
                {
                    if (rubyline[j] == ')')
                    {
                        cout << ")\n{";
                        semicolon = false;
                        break;
                    }
                    else
                    cout << rubyline[j];
                }
            }
            //else
            if (rubyline[i] == 'e' && rubyline[i+1] == 'l' && rubyline[i+2] == 's' && rubyline[i+3] == 'e')
            {
                cout << "}\nelse\n{";
                semicolon = false;
            }
            //end
            if (rubyline[i] == 'e' && rubyline[i+1] == 'n' && rubyline[i+2] == 'd')
            {
                cout << "}";
                semicolon = false;
            }
        }
        if (rubyline.length() == 0)
        {
            semicolon = false;
            newline = false;
        }
        if (semicolon == true)
        cout << ";";
        if (newline == true)
        cout << "\n";
    }
    rubycode.close();

    return 0;
}
This completely converts the Ruby code (linked above) except headers, int main, return 0, ect. Simply the code written. I will probably continue with this project and may get some more experience Ruby programmers that I know/can find. Thanks for the idea :)-
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

To make a full converter you will need to implement the dynamic features of Ruby to C++. Thats hard to do. Also the expressions will work differently so you'd need an expression converter like mine. Converting the OOP to C++ will be easier than converting to C because C doesn't do OOP like C++ does.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:To make a full converter you will need to implement the dynamic features of Ruby to C++.
:?:
Matthew wrote:Also the expressions will work differently so you'd need an expression converter like mine.
:?:
Matthew wrote:Converting the OOP to C++ will be easier than converting to C because C doesn't do OOP like C++ does.
That part I understood :)
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

I'm talking mostly about the dynamic typing (Use google).

Google expressions also.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Do you create a .c file with the converted code? That would be cool although formatting would probably prove difficult.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

A c source file, yes. I might include some option to pass the result to gcc for compilation.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:A c source file, yes. I might include some option to pass the result to gcc for compilation.
That would be cool. I was wondering if that was possible myself.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

I have a problem with my char pointers (C strings). A char pointer changes and I don't know why. It's breaking my pointer arithmetic which in turn results in a number too large for strncpy and hence a buffer overflow.

People have been least helpful on this website so far - http://stackoverflow.com/questions/3773 ... inter-arit
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:I have a problem with my char pointers (C strings). A char pointer changes and I don't know why. It's breaking my pointer arithmetic which in turn results in a number too large for strncpy and hence a buffer overflow.

People have been least helpful on this website so far - http://stackoverflow.com/questions/3773 ... inter-arit
I noticed the bracket thing too...but anyway; you may want to try these forums http://www.cplusplus.com/forum It's C++ but they could probably still help.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

I had an answers which was correct. I wasn't allocating enough memory for the stack.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:I had an answers which was correct. I wasn't allocating enough memory for the stack.
I could have told you that :lol:

A question I always wonder, what are you planning on doing as a career? I would assume Computer Science, but even so, what career path afterwards?
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

I want to run my own business. Likely computer related. I've just started a degree called Enterprise development.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:I want to run my own business. Likely computer related. I've just started a degree called Enterprise development.
I've always wanted to start my own business as well; taking business classes to teach me some of the challenges that could potentially await. The thing is, I want to become a Computer Scientist with a higher knowledge in the branch of game/simulation development. The problem being that you can't get a paycheck for years and even then the game might not sell. I guess I could work IT and game program with a team on the side, release the game, hope to get a publisher's attention, and go from there. That's a lot of hope and ifs though.
Image
Post Reply