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.
  • SylverReZ @ SylverReZ:
    @ZeroT21, Exactly. They're only good if you're willing to practice soldering, not for the long run.
  • ZeroT21 @ ZeroT21:
    i only use 'em to fix my own home appliances or personal projects/mods
    +1
  • SylverReZ @ SylverReZ:
    Home appliances would be a soldering gun, including plumbing. Normal soldering irons are for hobbyist projects/modchip installs.
  • ZeroT21 @ ZeroT21:
    though i've heard plenty of people getting wrecked trying to ''fix'' their microwave
  • SylverReZ @ SylverReZ:
    @ZeroT21, Did somebody tell them to put something metal in there?
  • ZeroT21 @ ZeroT21:
    I'm wondering what's so hard about it
  • SylverReZ @ SylverReZ:
    🍆 <== This?
  • ZeroT21 @ ZeroT21:
    having bigger nuts than brains is genetic probably
    +1
  • SylverReZ @ SylverReZ:
    That's what they tried to tell you.
  • SylverReZ @ SylverReZ:
    Hypothetically.
  • ZeroT21 @ ZeroT21:
    People won't believe me anyways untill shit happens. Youtube teaches everything they say... so ima just sit back and enjoy the mayhem
    +1
  • SylverReZ @ SylverReZ:
    If brain small, does that mean bigger balls? :unsure:
  • ZeroT21 @ ZeroT21:
    no ,they have bigger balls
    +1
  • ZeroT21 @ ZeroT21:
    we can't possibly insinuate they have small brains, be modest and just tell 'em they have big balls so they feel better bout 'emselfs
    +1
  • SylverReZ @ SylverReZ:
    @ZeroT21, True lol.
  • BakerMan @ BakerMan:
    chat is this real?
  • K3Nv2 @ K3Nv2:
    Chat is Ai
  • ZeroT21 @ ZeroT21:
    big balls are REAL
  • ZeroT21 @ ZeroT21:
    AI has balls too

    :rofl2:
  • NinStar @ NinStar:
    Chat, is @HiradeGirl real?
    NinStar @ NinStar: Chat, is @HiradeGirl real?