Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2006, 1:21 AM   #1
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
return 0; not working in c++ in visual studio 2005?

Everytime I put the return 0; command that you have to for apps like 'hello world' so that you need to press enter before the app closes doesnt work using visual studio 2005.

can someone help me?
chillypacman is offline   Reply With Quote
Old Mar 14th, 2006, 1:33 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3 Jimbo is on a distinguished road
return 0; doesnt make the program wait. Although in VS, if you don't run in debug mode, it should pause on its own (using Ctrl-F5). Otherwise, look here.
Jimbo is offline   Reply With Quote
Old Mar 14th, 2006, 5:30 AM   #3
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
Thanks for the link, I'll try that when I can get back on the computer with the compiler

Umm, I dont understand why return 0; isnt working anymore, I remember I had compiled a 'hello world' app sometime ago with another c++ compiler and it worked, is this a general change to how c++ works?
chillypacman is offline   Reply With Quote
Old Mar 14th, 2006, 6:03 AM   #4
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
As Jimbo said, return 0; does not make the programme wait. On the contrary, it returns the value 0 to the calling function/programme and in this case, it closes the programme.

If you want it to wait, add something like:
#include <iostream>
int main()
{
    // code
    std::cin.sync();
    std::cin.get();
    return 0;
}
UnKnown X is offline   Reply With Quote
Old Mar 14th, 2006, 9:55 AM   #5
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Quote:
Originally Posted by chillypacman
Thanks for the link, I'll try that when I can get back on the computer with the compiler

Umm, I dont understand why return 0; isnt working anymore, I remember I had compiled a 'hello world' app sometime ago with another c++ compiler and it worked, is this a general change to how c++ works?
Nothing to do with C++ - blame your old compiler for misleading you. it has been secretly adding an extra line to the end of your main() function, letting your program pause before it ends.
Bench is offline   Reply With Quote
Old Mar 14th, 2006, 2:47 PM   #6
.TD
Programmer
 
.TD's Avatar
 
Join Date: Feb 2006
Location: UK
Posts: 36
Rep Power: 0 .TD is on a distinguished road
You should learn what return 0 actually means. I know in Java System.exit(0) means that the program completed correctly and System.exit(1) (C++ equiv of return 1; at the end of main) means there is an error, but depending on how you work returning 0 and 1 could mean correct execution or error in the program.

That was probably rather unclear, someone else (who has more experience of C/C++) feel free to clarify. One more thing, why do people use the M$ products Vis Stuido n so on, is it because its easy to make GUIs like in Delphi or just don't they care about the non-portability and so on of their code?
__________________
Go away before I replace you with a very small shell script.
--
Get FireFox!
.TD is offline   Reply With Quote
Old Mar 14th, 2006, 3:06 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
.TD, you can code portably in Visual Studio - you just can't use .NET (well, you can, but the Mono project doesn't support everything yet).

Generally, in most OSes, returning an exit code of 0 means the program executed successfully - anything else is an error code. return 0; has nothing to do with making your program wait - it simply tells the operating system your program is exiting with an exit code of 0.

As UnKnown X said, use std::cin.get() to request input and stall the exiting.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 14th, 2006, 3:39 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
just don't they care about the non-portability and so on of their code?
Dint yo mama tell you not bleeve everthang ya hear?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 15th, 2006, 2:56 AM   #9
PPoA
Programmer
 
PPoA's Avatar
 
Join Date: Mar 2006
Location: Tennessee
Posts: 41
Rep Power: 0 PPoA is on a distinguished road
Quote:
Originally Posted by .TD
One more thing, why do people use the M$ products Vis Stuido n so on?
I use VStudio because that's what my school uses and they let you install it for free. They have some kind of agreement with Microsoft so they can let students install it. Still have to fill out the ULA though.
PPoA is offline   Reply With Quote
Old Mar 18th, 2006, 12:32 AM   #10
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
wtf???

my first problem with vs 2005 was the precompiled header stdafx.h being automatically included in all win32 console projects. figured it out...yay. it automatically inserts a system("pause") (or something that acts just like it after your code)...i don't understand what you mean.

but if it's not...just do the cin.get() deal and it'll be fine.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC