hello.cpp First C++ program help?

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
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 ;)
 

hamstarr

Member
Newcomer
Joined
May 28, 2006
Messages
24
Trophies
0
Location
legoland whitehouse
XP
141
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
 
  • Like
Reactions: 1 person

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
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

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.
 

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
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
 

redfalcon

GBATemp Member
Member
Joined
Feb 19, 2009
Messages
250
Trophies
0
Website
code.google.com
XP
206
Country
Gambia, The
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.
 
  • Like
Reactions: 1 person

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
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.

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!
 

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
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:

To declare an array, write the type of object stored, followed by the name of the array and a subscript with the number of objects to be held in the array.
Example 1
int MyIntegerArray[90];
Example 2
long * ArrayOfPointersToLongs[100];
To access members of the array, use the subscript operator.
Example 1
// assign ninth member of MyIntegerArray to theNinthInteger
int theNinthInteger = MyIntegerArray[8];
Example 2
// assign ninth member of ArrayOfPointersToLongs to pLong.
long * pLong = ArrayOfPointersToLongs[8];
Arrays count from zero. An array of n terms is numbered from 0 to n-1.
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.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Sicklyboy @ Sicklyboy:
    maaaaan that's so awesome but I also don't want to fork over a hundo for it
  • Veho @ Veho:
    The fuuuuu---
  • Veho @ Veho:
    I thought it was an actual xBox at that price.
  • Sicklyboy @ Sicklyboy:
    I wanna grab a 360 Slim and a 360 E one of these days. Missed the boat of getting them at their lowest though, once they were discontinued. Could've got them for cheap back when I was a broke 20 something working at Target, but then again, I was a broke 20 something working at Target
  • Veho @ Veho:
    Being broke is no fun.
  • K3Nv2 @ K3Nv2:
    @Sicklyboy, $150 isn't that bad for a jtag slim on ebay
  • Veho @ Veho:
    I only wish it was actually playable.
  • Veho @ Veho:
    There's a guy on the Tube of You that makes playable mechanical arcade games out of Lego. This could work on the same principle.
  • Veho @ Veho:
    Just a couple of guys taking their manatee out for some fresh air, why you have to molest them?
  • Veho @ Veho:
    Stupid Chinese shop switched their shipping company and this one is slooooooow.
  • LeoTCK @ LeoTCK:
    STOP BUYING CHINESE CRAP THEN
  • LeoTCK @ LeoTCK:
    SUPPORT LOCAL PRODUCTS, MAKE REVOLUTION
  • LeoTCK @ LeoTCK:
    THEY KEEP REMOVING LOCAL SHIt AND REPLACING WItH INFERIOR CHINESE CRAP
  • LeoTCK @ LeoTCK:
    THATS WHY MY PARTNER CANT GET A GOOTWEAR HIS SIZE ANYMORE
  • LeoTCK @ LeoTCK:
    HE HAS BIG FOOT AND BIG DUCK
  • LeoTCK @ LeoTCK:
    d*ck i mean*
  • LeoTCK @ LeoTCK:
    lol
  • Veho @ Veho:
    Mkay.
  • Veho @ Veho:
    I just ordered another package from China just to spite you.
  • SylverReZ @ SylverReZ:
    Leo could not withstand communism.
  • SylverReZ @ SylverReZ:
    Its OUR products to begin with lol.
    SylverReZ @ SylverReZ: Its OUR products to begin with lol.