-2

I made this program in Java but I am fairly new in C++ and using strings has been imposible. I was wondering if anyone could point me in a direction for me to "translate" this program to C++. Just how to use strings and maybe something you see in my code that is worth mentioning. Note: I am not asking for any of you for a direct translation, just info for me to do it, the code is reference.

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class DragonCurve{
    public static void main(String[] args){
        try{
        Scanner sc = new Scanner(System.in);
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));

        int n;
        while((n = Integer.parseInt(sc.nextLine()))!=-1){
            log.write(dragon(n)+"\n");
        }

        log.flush();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static String dragon(int n){

    if(n>2){
        String aux = dragon(n-1);
        return aux+"L"+reverse(aux);
    }else if(n==2)
        return "LLR";   
    else if(n==1)
        return "L";
    else if(n==0)
        return "";

    return "";
}

public static String reverse(String aux){
    String ans = ""; 
    for(int i = aux.length();i>0;i--){
        if(aux.charAt(i-1)=='L')
            ans+='R';
        else
            ans+='L';
    }

    return ans;
}
}
Darwin57721
  • 177
  • 1
  • 14
  • c++ is a different language from java. Code conversion isn't trivial! You can't ask us for _gimme the codez plz_ with such situation. Try yourself fist, and ask for particular problems where you get stuck. – πάντα ῥεῖ Sep 23 '14 at 04:49
  • I believe [examples on this page](http://en.cppreference.com/w/cpp/string/basic_string/operator%2B) and [this page](http://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt) can help you. If you still don't understand something, feel free to ask that specific question. Compile the example program and add your things incrementally should help. – Mohit Jain Sep 23 '14 at 04:50
  • I wasn't actually asking for you to translate but to tell me where to look since I've been trying to do it myself and just got confused. I posted the code just for reference. Sorry if it looked like I was asking just for you to do it for me. @MohitJain thanks, I'll have a look and post my code if I have any problems. – Darwin57721 Sep 23 '14 at 04:58

1 Answers1

2

To get you started, here is how to write the I/O . (Many books cover this badly).

I left the string handling section for you to figure out. It is not dissimilar to Java, however you have to bear in mind that std::string does not include any automatic functions for converting an integer or a character to a string; you can't just use the + operator. To learn about that, read this thread.

I would recommend learning from a book. If you don't have a book, you can look up cppreference.com or other sites to see how std::string works.

Note that std::reverse exists, you don't have to write your own reverse function.

#include <iostream>
#include <string>
#include <sstream>

std::string dragon(int n)
{
    // Exercise for the reader.
}

int main()
{
    std::string s;
    int n;

    while ( std::getline( std::cin, s ) )
    {
         std::istringstream iss(s);
         while ( iss >> n )
             std::cout << dragon(n) << std::endl;
    }
}
Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • OK. Another thing I should have mentioned is that string literals have type `char const[]` for historical reasons. To get a std::string, write `std::string("abc")`, or if your compiler is very new, `"abc"s`. – M.M Sep 23 '14 at 05:04
  • Oh, Ok! thanks again, I don't think my compiler is up up to date so I'll use the other way. I'll post my code in a few minutes I hope. – Darwin57721 Sep 23 '14 at 05:17
  • @Darwin57721 OK. Post a new question instead of updating this one, I guess we should edit this one to just be about the I/O side of it – M.M Sep 23 '14 at 05:29