Jump to content


Photo
- - - - -

New to C++ Having trouble...


  • Please log in to reply
14 replies to this topic

#1 5imp7y

5imp7y

    Hakker addict

  • Binrev Financier
  • 507 posts
  • Country:
  • Gender:Male
  • Location:PA again....

Posted 02 September 2009 - 02:15 AM

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;
}
:-)

#2 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 02 September 2009 - 02:26 AM

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.

#3 AlexZ

AlexZ

    elite

  • Members
  • 114 posts
  • Country:
  • Gender:Male

Posted 02 September 2009 - 06:30 AM

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!

#4 chown

chown

    SUPR3M3 31337 Mack Daddy P1MP

  • Moderating Team
  • 493 posts
  • Country:
  • Gender:Male
  • Location:Floating on a sea of hydrogen

Posted 02 September 2009 - 07:12 AM

Put this code on a line before the "return 8;":
system("pause");

Edit: Ohm's getch() idea is better

Edited by chown, 02 September 2009 - 07:14 AM.


#5 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 02 September 2009 - 01:30 PM

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.

#6 tekwizz123

tekwizz123

    I broke 10 posts and all I got was this lousy title!

  • Members
  • 13 posts
  • Country:
  • Gender:Male

Posted 02 September 2009 - 03:03 PM

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:

#7 Pan

Pan

    Gibson Hacker

  • Members
  • 94 posts
  • Location:Detroit

Posted 02 September 2009 - 08:48 PM

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.

#8 5imp7y

5imp7y

    Hakker addict

  • Binrev Financier
  • 507 posts
  • Country:
  • Gender:Male
  • Location:PA again....

Posted 02 September 2009 - 10:25 PM

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, 02 September 2009 - 10:28 PM.


#9 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 02 September 2009 - 11:21 PM

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.

#10 zraith

zraith

    SUP3R 31337

  • Members
  • 198 posts
  • Location:Southern Indiana

Posted 02 September 2009 - 11:38 PM

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.

#11 AlexZ

AlexZ

    elite

  • Members
  • 114 posts
  • Country:
  • Gender:Male

Posted 04 September 2009 - 05:22 PM

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! ;)

#12 soloist

soloist

    DDP Fan club member

  • Members
  • 48 posts

Posted 07 September 2009 - 10:28 PM

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

#13 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 07 September 2009 - 10:51 PM

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.

#14 mecca_

mecca_

    DDP Fan club member

  • Members
  • 54 posts

Posted 08 September 2009 - 01:27 PM

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/te...gedit_few_steps (again I haven't tried it so ymmv)

#15 zraith

zraith

    SUP3R 31337

  • Members
  • 198 posts
  • Location:Southern Indiana

Posted 11 September 2009 - 10:28 AM

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!)




BinRev is hosted by the great people at Lunarpages!