Sign in to follow this  
Followers 0
epew23

New to C++ Having trouble...

15 posts in this topic

Just started to learn coding in C++ and I was trying the "Hello World" thing. I am using Dev-C++. After i compile the code and run the .exe it only runs for like a split second... How do i make it last. Maybe make it so i click and it shuts down?

Here is my code maybe it is a typo....


//I fail Srry Bin rev :-(
#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 8;
}

:-)

0

Share this post


Link to post
Share on other sites

Everything is working fine here. You run the program, it opens a terminal window, prints the message, then exits. When it exits, it closes the terminal window. It's only supposed to run for a split second, that's what you told it to do. Open a command prompt, go to the exe, and run it from there. Alternatively, add a getch(); line to the end of main (just before the return statement) so it'll wait for you to hit enter.

Also, don't use Dev-C++. First, it uses MingW. This is a Windows port of GCC. It's always buggy. It's always been buggy. It will always be buggy. Even with simple code, it's possible you'll encounter completely unexplainable bugs. Second, Dev-C++ hasn't been update since February of 2005. It's been abandoned. It's using a now ancient version of GCC, and a buggy Windows port of it at that. Why it's recommended and why people download it is beyond me. What you want is Visual C++ Express Edition. It's free. It's modern. It works really well. It's much more complex, but you rarely have to go delving into the config dialogs. Download it, and be happy.

Edit: You also don't want to return 8 from main. If everything went well and there were no errors, you want to return 0. Doesn't matter much on Windows, but it does on Linux if you ever want to use your programs in shell scripts.

0

Share this post


Link to post
Share on other sites

Everything is working fine here. You run the program, it opens a terminal window, prints the message, then exits. When it exits, it closes the terminal window. It's only supposed to run for a split second, that's what you told it to do. Open a command prompt, go to the exe, and run it from there. Alternatively, add a getch(); line to the end of main (just before the return statement) so it'll wait for you to hit enter.

Also, don't use Dev-C++. First, it uses MingW. This is a Windows port of GCC. It's always buggy. It's always been buggy. It will always be buggy. Even with simple code, it's possible you'll encounter completely unexplainable bugs. Second, Dev-C++ hasn't been update since February of 2005. It's been abandoned. It's using a now ancient version of GCC, and a buggy Windows port of it at that. Why it's recommended and why people download it is beyond me. What you want is Visual C++ Express Edition. It's free. It's modern. It works really well. It's much more complex, but you rarely have to go delving into the config dialogs. Download it, and be happy.

Edit: You also don't want to return 8 from main. If everything went well and there were no errors, you want to return 0. Doesn't matter much on Windows, but it does on Linux if you ever want to use your programs in shell scripts.

Yes, DevC++ is buggy, but it works always fine if you write some lines in C!!

If you want you can use Code::Blocks, a very good IDE for coding in C++!

You can easily find it here and for this program there are a Linux-version and a Windows version!

0

Share this post


Link to post
Share on other sites

Put this code on a line before the "return 8;":

system("pause");

Edit: Ohm's getch() idea is better

Edited by chown
0

Share this post


Link to post
Share on other sites

Yes, DevC++ is buggy, but it works always fine if you write some lines in C!!

If you want you can use Code::Blocks, a very good IDE for coding in C++!

You can easily find it here and for this program there are a Linux-version and a Windows version!

That's not the point. I understand that it does work, but there will be subtle bugs behind the scenes even in simple programs. They should really put a big warning up on the Dev-C++ website against using it anymore.

I've never used Code::Blocks before. Frankly, I don't see the need for it with Visual C++ Express around.

Put this code on a line before the "return 8;":

system("pause");

Edit: Ohm's getch() idea is better

Actually.. I'm not sure about that. It just occurred to me that if, prior to the end of the program, you have something like scanf("%d",&i);, getch will happily eat the newline character (or another other character that came after the int in user input) and exit. system("pause"); is probably better. Not exactly cross-platform, but it'll work.

0

Share this post


Link to post
Share on other sites

thanks for this I have been looking for something to stop the script from going so fast across the screen for ages. this has really helped me with that! :biggrin:

0

Share this post


Link to post
Share on other sites

thanks for this I have been looking for something to stop the script from going so fast across the screen for ages. this has really helped me with that! :biggrin:

Just as an aside, the question of what to return (0 or 1) and keeping an app alive are sort of general application programming techniques. These types of things are similar across programming environments. You might find a general programming book useful for picking up stuff like that which you can extrapolate for any situation.

Also, using pause or any other method of preventing execution is going to be inferior to a loop, which allows the app to continue, the ability to poll state, the ability to work in threads, etc. In which case, for(::) {} or while() {} are your best bet.

Good luck, have fun.

0

Share this post


Link to post
Share on other sites

Ok so i was able to get it to pause and wait untill i press a key with:

system("pause");

But the one before :

getch()

The window was still closing instantly. Is there somthing that needs to be contained in ()?

Edited by 1337_snic
0

Share this post


Link to post
Share on other sites

No, there's another character in the stream it's reading instead of waiting for you to hit enter (thus injecting a newline into the stream). Use the system function, it's a better solution here. I suppose you could try something like scanf("%*s\n); though.

0

Share this post


Link to post
Share on other sites

What is happening is the program is returning 0, which returns to the shell (command prompt), which then sees there is nothing to do and closes.

Open up command prompt (start->run and type 'cmd') then navigate to where the compiled exe is located. Once in the correct directory, type the program's name and voila, you see your program works fine.

On another note, I thought there used to be an option (buried in the menus) that would pause the command prompt when the program was finished executing.

0

Share this post


Link to post
Share on other sites

Ok so i was able to get it to pause and wait untill i press a key with:

system("pause");

But the one before :

getch()

The window was still closing instantly. Is there somthing that needs to be contained in ()?

If you want to stop your windows you can use or and ifdef declaration, or else you can use the getchar() function that attemps a character in input and it's useful to you to have the time to watch the program's results! ;)

0

Share this post


Link to post
Share on other sites

Not wanting to start a new thread, could anyone recommend an IDE for linux? or is using gedit+gcc the best way to go?

0

Share this post


Link to post
Share on other sites

Gedit? I suppose that'll work, but for coding I usually want an editor with a little more oomph. I use vim, the learning curve is a bit steep compared to something like gedit, but the payoff is definitely worth it. For project management, I just use make. I don't do any large-scale projects though, so for those you might want a real IDE.

0

Share this post


Link to post
Share on other sites

Gedit? I suppose that'll work, but for coding I usually want an editor with a little more oomph. I use vim, the learning curve is a bit steep compared to something like gedit, but the payoff is definitely worth it. For project management, I just use make. I don't do any large-scale projects though, so for those you might want a real IDE.

I tend to stick with vim as well but I think it's worth noting that gedit is actually really nice as an IDE. I had no idea, but with a bit of tweaking you can actually get gedit to perform quite a bit like the Mac IDE TextMate that so many ruby enthusiasts are in love with. I haven't tried this myself but a friend of mine has been using gedit in this way for quite some time and he really likes it.

Instructions: http://grigio.org/textmate_gedit_few_steps (again I haven't tried it so ymmv)

0

Share this post


Link to post
Share on other sites

There are some really good guides to learning vi/vim. I think vimtutor is a really nice program. If your on ubuntu, install vimtutor and go to town (either in its own package or its in vim-full, cant remember). Other people really like emacs and it may be more suited to you.

If you are going to use vim, i recommend making sure you have vim-full installed. I would also recommend turning syntax on. (Makes the screen look like a christmas tree. YAY!)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0