Like Tree6Likes

Convert Java To C++

This is a discussion on Convert Java To C++ within the C++ Programming forums, part of the General Programming Boards category; Hello I wrote this out to count out the letters in a sequence for use in decryption class. but I ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Convert Java To C++

    Hello I wrote this out to count out the letters in a sequence for use in decryption class. but I want to knw can someone convert it to c or c++ for me

    Code:
    package Bsc;
    
    
    import java.io.*;
    
    
      
    public class MyAppMain {
    
    
       public static final int MAX_CHAR = 65535;
    
    
       public static void main(String[] args) throws IOException {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Enter the line :- ");
            String text = br.readLine();
            System.out.println("You have Enterd :- " + text);
    
    
           String s = text.toUpperCase();
           int res[]  = new int[256];
    
    
           for(int i=0 ; i<s.length() ; i++)
           {
               res[s.charAt(i)]++;
           }
    
    
           for(int i=0 ; i<res.length ; i++)
           {
               if(res[i] !=0)
               {
                   System.out.println(""+(char)i+" : "+res[i]);
               }
           }
       }
    }
    Any help is appreciated

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Looks like homework. Have you attempted anything?
    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.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's your first hint: main is not part of a class in C++.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    C++ Made Easy. Start here.
    Salem likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    Here's your first hint: main is not part of a class in C++.
    Quzah.
    Depends. Personally, I prefer to use a class in place of 'main'. For one thing, it can make accessing command line parameters and environment variables much simpler by prepackaging them into more user friendly formats. Since your member variables can take the place of globals, you can utilize less verbose function calls without resorting to uncompartmentalized coding. Finally, instead of returning a value, you can simply define an 'exit' function in your 'main' base class that throws an exception to be caught from the wrapped 'main'/'wmain' to do so, making it easy to safely return from anywhere in your 'main' class' functions.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    [Edit]Okay, I can't find the quote so I'll guesstimate. The point here is that Java makes you jump through hoops like that imposing a level of "OOP" that is absolutely not needed in C and C++; you don't need to bring those mistakes home, so don't.[/Edit]

    Personally, I prefer to use a class in place of 'main'. For one thing, it can make accessing command line parameters and environment variables much simpler by prepackaging them into more user friendly formats.
    A standard C or C++ program begins with `main'; you don't "use a class in place of 'main'"; you are simply calling another function.

    Since your member variables can take the place of globals, you can utilize less verbose function calls without resorting to uncompartmentalized coding.
    Congratulations, you've hidden global state behind a badly protected pointer; every problem related to global variables still applies without actually providing the convenience of globally visible state.

    Finally, instead of returning a value, you can simply define an 'exit' function in your 'main' base class that throws an exception to be caught from the wrapped 'main'/'wmain' to do so, making it easy to safely return from anywhere in your 'main' class' functions.
    O_o

    Now you've committed a terrifying assault on program logic while managing to poorly hide what is essentially a `goto' or `setjmp'/`longjmp' and badly implementing a measure of the behavior of the standard `exit' function.

    Soma
    Last edited by phantomotap; 09-19-2011 at 07:40 PM.

  7. #7
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by phantomotap View Post
    A standard C or C++ program begins with `main'; you don't "use a class in place of 'main'"; you are simply calling another function.
    Yes, I use a class instead of defining main when I write a program. What happens "behind the scenes" is immaterial.

    Quote Originally Posted by phantomotap View Post
    Congratulations, you've hidden global state behind a badly protected pointer; every problem related to global variables still applies without actually providing the convenience of globally visible state.
    Not at all. Suppose you wanted to use the class in a different context (that is, not as the actual main driver of your program). You could basically just change the name of the base class and reuse the code as-is, without dragging around global declarations that could conflict elsewhere. It's just a cleaner design, really.


    Quote Originally Posted by phantomotap View Post
    O_o

    Now you've committed a terrifying assault on program logic while managing to poorly hide what is essentially a `goto' or `setjmp'/`longjmp' and badly implementing a measure of the behavior of the standard `exit' function.
    How so? The stack unwinds safely, so destructors are called appropriately, plus you get to write more lucid code to boot. I don't see anything inherently flawed with that.

    Quote Originally Posted by phantomotap View Post
    [Edit]Okay, I can't find the quote so I'll guesstimate. The point here is that Java makes you jump through hoops like that imposing a level of "OOP" that is absolutely not needed in C and C++; you don't need to bring those mistakes home, so don't.[/Edit]
    Oh, I see. You just hate the idea of using a class instead of main. Got it.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by gardhr
    I use a class instead of defining main when I write a program.
    More accurately, you use a class in addition to defining main when you write a program.

    Quote Originally Posted by gardhr
    Not at all. Suppose you wanted to use the class in a different context (that is, not as the actual main driver of your program). You could basically just change the name of the base class and reuse the code as-is, without dragging around global declarations that could conflict elsewhere. It's just a cleaner design, really.
    I am curious as to the feasibility of reusing a "main class" in the first place.

    Quote Originally Posted by gardhr
    How so? The stack unwinds safely, so destructors are called appropriately, plus you get to write more lucid code to boot. I don't see anything inherently flawed with that.
    I agree. However, this has less to do with the class and more to do with having that last resort exception handler in main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,623
    Quote Originally Posted by gardhr View Post
    Yes, I use a class instead of defining main when I write a program. What happens "behind the scenes" is immaterial.
    Can you please explain that? I thought that a C++ program HAS to have its "main" defined. Is there another way?!
    Devoted my life to programming...

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by GReaper
    Can you please explain that? I thought that a C++ program HAS to have its "main" defined. Is there another way?!
    gardhr's approach is to write a class whose constructor or other, possibly static, member function contains the code that would normally be in the global main function. The global main function is then implemented to use this class, e.g., by calling the static member function or instantiating an object of the class (and possibly calling the designated member function).

    This approach is uncommon in C++. I think that it would be more common to write another function that is called from the global main function. This function would contain the code that would normally be in the global main function. The global main function itself would then do things like translate the command line arguments to a preferred format.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    What happens "behind the scenes" is immaterial.
    Ah, of course, the "Ostrich" argument.

    It's just a cleaner design, really.
    If your design is truly "clean", the one bit of code that can not be repurposed without a lot of work is the bit of code that drives the relationship between all other components.

    I don't see anything inherently flawed with that.
    Depending on an exception mechanism as a means of flow control is flawed program design by definition.

    You just hate the idea of using a class instead of main.
    Not at all, but I do hate bad design and the illusion of good design.

    Consider it a character flaw.

    Soma

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    Can you please explain that?
    I find an example in addition to the comments Laserlight made will be useful.

    He is essentially doing something like this:

    Code:
    class program
    {
        void start
        (
            int argc
          , char ** argv
        )
        {
            // program logic
        }
        void * data;
    };
    
    void main
    (
        int argc
      , char ** argv
    )
    {
        try
        {
            program a;
            a.start(argc, argv);
        }
        catch(...)
        {
            // cleanup logic
        }
    }
    which basically looks like this:

    Code:
    struct program
    {
        void * data;
    };
    
    void start
    (
        struct program * this
      , int argc
      , char ** argv
    )
    {
        // program logic
    }
    
    void main
    (
        int argc
      , char ** argv
    )
    {
        try
        {
            program a;
            start(&a, argc, argv);
        }
        catch(...)
        {
            // cleanup logic
        }
    }
    in order to avoid doing this:

    Code:
    void * data;
    
    void start
    (
        int argc
      , char ** argv
    )
    {
        // program logic
    }
    
    void main
    (
        int argc
      , char ** argv
    )
    {
        if(start(argc, argv))
        {
            // cleanup logic
        }
    }
    because he thinks it magics away design and globally visible state problems.

    Soma

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    Depends.
    No it doesn't.
    Quote Originally Posted by gardhr View Post
    Personally, I prefer to use a class in place of 'main'.
    No you don't.

    You can't use a class instead of main, unless they changed it in the standard. You must have a main function AFAIK. That was my whole point, almost word for word in fact.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by laserlight View Post
    More accurately, you use a class in addition to defining main when you write a program.
    Rather, 'main' resides in a header file where the class is defined.

    Quote Originally Posted by laserlight View Post
    I am curious as to the feasibility of reusing a "main class" in the first place.
    Well, let's say you have a program that provides some file conversion facility. You have another program that performs some higher level functionality and decide to "drag and drop" it into that codebase. Point it, if you really wanted to do it, you could do so with minimal effort.

    Quote Originally Posted by laserlight View Post
    I agree. However, this has less to do with the class and more to do with having that last resort exception handler in main.
    It isn't a really "last resort", but a practical way to return a value to the operating system without having to engineer the program with all of the "plumbing" needed to safely do so without such a facility. For example, you might be several function calls deep into the program when you realize that the user has supplied some malformed input. You can simply have defined a 'usage' function that reports the error and exits gracefully. The only safe alternative would be to return an error code or whatnot from each function, which has to be rechecked at multiple points before you get down to the business of shutting the program down.

    Quote Originally Posted by GReaper View Post
    Can you please explain that? I thought that a C++ program HAS to have its "main" defined. Is there another way?!
    See above. Basically, it entails invoking 'main' within the header file, which in turn calls the 'main' function of the wrapper class.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hiding 'main' away in a header file doesn't magically make it not part of your program. So there is no 'depends' answer.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. java to c.
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 08:56 AM
  2. Multiple Java files for one Java project
    By doubleanti in forum Windows Programming
    Replies: 2
    Last Post: 11-22-2004, 01:06 AM
  3. How to use Java with C++
    By Arrow Mk 84 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2003, 03:12 PM
  4. I use Java but...
    By zurdo1119 in forum C Programming
    Replies: 4
    Last Post: 08-04-2002, 02:05 PM
  5. C# vs. JAVA
    By talal*c in forum C# Programming
    Replies: 10
    Last Post: 04-06-2002, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21