hello.cpp First C++ program help?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Lucifer666, Apr 10, 2012.

Apr 10, 2012
  1. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    As you've probably guessed, I'm onto my very first "Hello World!" C++ program, which seems easy enough.

    In Ubuntu, I've opened up the text editor and I've written the code and saved it as a .cpp which worked fine.

    In the terminal, after taking it to the place where I saved my .cpp, I wrote:
    g++ -Wall -W -Werror hello.cpp -o hello

    It created a file in that folder, with no extension, which I guess is expected on Linux.

    If I type "./hello" (without "s), it says Hello World! on my screen. Great.

    Now the issue is that when I head over to the folder where hello is saved, double-clicking it won't do anything.
    How can I change it in a way so that it won't need to be opened using the terminal, but rather simply by being double-clicked?

    Thanks in advance, and I apologise for my noobiness. I guess we all start somewhere ;)
     
  2. pistone

    Member pistone GBAtemp Advanced Fan

    Joined:
    Feb 18, 2010
    Messages:
    504
    Location:
    in your heart...coz secretly you love me !!!!
    Country:
    Albania
    if you are new to programing is a good thing to start with a text editor as you will remember things easy but somehow is not that simple if you want a nice coding editor for ubuntu try Geany as is a great ide
    also if you want a better program try code::blocks :)
    and then tell me
     
    1 person likes this.
  3. hamstarr

    Newcomer hamstarr Member

    Joined:
    May 28, 2006
    Messages:
    24
    Location:
    legoland whitehouse
    Country:
    Netherlands
    From the top of my head: easiest way to do this in Ubuntu,

    create a shell script (echo "./hello" > run.sh) , make it executable (chmod +x ./run.sh)
    double click the run.sh file, and Nautilus will ask what to do with the shell script (run, display in terminal, edit, etc)

    note: when doubleclicking the ./hello executable from Ubuntu, it does actually run the executable, but because there is no GUI, no output is shown, because its not run in console mode/terminal.

    The best fix is to actually instantiate a gui-window (for example with GTK), and output the text there.. see http://developer.gno...#SEC-HELLOWORLD
     
    1 person likes this.
  4. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    Thanks! That worked :)
    Also thanks for the explanation to why it won't run without the terminal. I wouldn't have figured it out myself.
     
  5. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    I've actually encountered another issue! I didn't want to make a new thread, so I'll post it here:

    So I've written my very first extremely basic code in a file called Functions2.cpp

    This is what it consists of:

    Code:
    #include 
    int Add (int first, int second)
    {
    std::cout 
     
  6. redfalcon

    Member redfalcon GBATemp Member

    Joined:
    Feb 19, 2009
    Messages:
    250
    Country:
    Germany
    Because thats exactly what you programmed. You type in two numbers, add them, print the solution and exit the program. If you want to leave the window open, you either need to pause the application, or wait for a final "return" keypress. Hint: You basically did the latter before.
     
    1 person likes this.
  7. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    Thanks man, I added the two lines
    Code:
    char response;
    std::cin >> response;
    Before the return 0; bit and it worked. ^_^

    Not too shabby for my first day!
     
  8. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    I've stumbled upon yet another obstacle in my C++ adventure. This time it isn't really a big thing hindering my C++ app development, but rather just a little misconception that's been bugging me for a while and some clarification would be appreciated.

    So I've got my C++ textbook in front of me. I've pretty much finished working with enumerations and arrays and reached a section which basically recaps what I've been doing in the past couple chapters, and there's this note which really bothers me:

    I know, very simple stuff.
    The bits that are bothering me are those in bold.
    This is the first time these have popped up in the whole book in that form (in the recap section too?), and I'm really unsure what they do.
    So it says to specify the type of object when defining an array, that's fine. Except that 'long' isn't exactly a type of variable/object, it's just the amount of storage given to a variable (and this also depends on the compiler IIRC). That being said, the actual variable type hasn't been specified, and there's this star '*' right after it, whose function I am currently unsure of. Is 'long *' a fancy way of writing 'long int' ? If not, exactly what is long *? Thanks.
     
  9. megazig

    Member megazig SU

    Joined:
    Oct 25, 2008
    Messages:
    467
    Country:
    United States
    it means a pointer to a long.



    edit:
    long is a type that is the same as a long int: http://en.wikipedia.org/wiki/C_data_types
    as the variable name says, it's an array of pointers to longs. the video should explain the pointer idea. and then it's just a list of those.
     
    Lucifer666 likes this.
  10. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    That was incredibly helpful. Thank you for the clarification.
     

Share This Page