Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 12-30-2005, 04:06 PM   #1 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default Program Terminating too quickly.

Alright at the moment I am using a cin>> with a variable in order to keep the program from exiting before I can see the results. I am wondering if there is a better way to keep the window open.
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-30-2005, 04:11 PM   #2 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,278
Send a message via MSN to arul
Default

Don't know if it's the best way ( I'm learning C++ too ) but I would rather use cin.get() instead of declaring a slave variable.
Code:
cin.get();
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Old 12-30-2005, 04:56 PM   #3 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

that doesn't seem to stop it from closing. Maybe I am using it wrong @_@
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-30-2005, 05:22 PM   #4 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

I found out what it is, the compiler I use runs it in a shell that closes when it is through. When i launch it from a command prompt it stops.

grrr. It still isn't what I want. When I launch the exe it opens it and closes it as soon as the results are displayed.

This is with cin.get();
__________________

Robin Lovett
April 21, 1948 - March 12, 2007

Last edited by DontdroptheSOAD; 12-30-2005 at 05:27 PM.
DontdroptheSOAD is offline   Reply With Quote
Old 12-30-2005, 05:58 PM   #5 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

One of the best C++-standard way to do this is to use the istream::ignore() method with cin, ala

Code:
#include <iostream>
#include <limits>

...

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'z');
This will extract and discard as many characters as can fit into a stream up until the character 'z' is found, then that is extracted, discarded, and the statement ends. The first argument is the number of characters to remove and the second is an optional delimiter to stop ignoring at.

Last edited by Sep102; 12-30-2005 at 06:01 PM.
Sep102 is offline   Reply With Quote
Old 12-30-2005, 06:00 PM   #6 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

Quote:
Originally Posted by Sep102
One of the best C++-standard way to do this is to use the istream::ignore() method with cin, ala

Code:
#include <iostream>
#include <limits>

...

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'z');
This will ignore as many characters as can fit into a stream up until the character 'z' is ignored. The first argument is the number of characters to ignore and the second is an optional delimiter to stop ignoring at.
so if z is pressed it will terminate?
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-30-2005, 06:02 PM   #7 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Yes, if 'z' is entered to cin it will end and (almost) only when 'z' is entered.
Sep102 is offline   Reply With Quote
Old 12-30-2005, 06:03 PM   #8 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

Excellent, once again you have been very helpful
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-31-2005, 12:09 AM   #9 (permalink)
xir
Forum Newbie
 
Join Date: Jul 2004
Posts: 59
Default

Something I don't really like with the new VC++ .NET. The old VC6 used keep the console open. What I do is add a breakpoint just before the main() function ends and then run it in debug.

If someone has a solution to make it work like the old VC6 I'd be interested in knowing too
xir is offline   Reply With Quote
Old 12-31-2005, 03:40 AM   #10 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

I don't exactly know the answer to that, however on both machines that I've installed the entirety of VS.NET 2003 on (one heavily configured and the other fully-default installed and unconfigured) both prompt me before quitting the application, thereby alleviating the need for me to do any of this, as do all of the other machines running it at my school.

Last edited by Sep102; 12-31-2005 at 03:46 AM.
Sep102 is offline   Reply With Quote
Old 12-31-2005, 05:50 AM   #11 (permalink)
xir
Forum Newbie
 
Join Date: Jul 2004
Posts: 59
Default

If you use the red exclamation mark to run the program or Ctrl+F5 it will bring up the prompt asking to close once the program has finished. It just doesn't work in debug, but I can live with that ;/
xir is offline   Reply With Quote
Old 01-01-2006, 01:03 AM   #12 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Ahh, well you see, you're not supposed to be running in debug mode all of the time testing, just when something actually does "go to shit" (so to speak) and you need debug mode to fix it.
Sep102 is offline   Reply With Quote
Old 01-01-2006, 09:29 PM   #13 (permalink)
Garm Brood Cerebrate
 
ZaSz-RH's Avatar
 
Join Date: Dec 2003
Location: Québec, Canada
Age: 23
Posts: 201
Send a message via MSN to ZaSz-RH
Default

You can also use
Code:
system("PAUSE");
It's like a pause in DOS, any entry will close the program.
__________________
Named after the fierce hellhound of Norse myth, the Garm strikes with alarming speed and ferocity. The minions of this Brood excel at hit and run raids that weaken their enemy's defensive formations. Zasz, the cunning Cerebrate of this Brood, delights in preemptive attacks, relying chiefly upon surprise to throw enemy forces into total chaos.
ZaSz-RH is offline   Reply With Quote
Old 01-01-2006, 11:45 PM   #14 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by ZaSz-RH
You can also use
Code:
system("PAUSE");
It's like a pause in DOS, any entry will close the program.
However the one problem with that is the fact that it is a nonportable way of pausing, moving the program to another platform than Windows may very well cause it to "break" (so to speak, as not pausing may or may not be seen as the program breaking).

The way I presented is one of the few completely portable (as in, as long as the compiler supports the ISO C++ standard well enough) ways enacting a pause in the program.
Sep102 is offline   Reply With Quote
Old 01-02-2006, 01:22 AM   #15 (permalink)
 
Join Date: Dec 2005
Location: Montreal
Age: 23
Posts: 3
Default

use system pause :
Code:
system( "PAUSE" );
It will show a message and wait any key press
p4tRi0t3 is offline   Reply With Quote
Old 01-02-2006, 02:38 PM   #16 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

I do like the pause method, it is nice however i would like programs I write to be available in all supporting os's also I was wondering if I can use the method you mentioned Sep to work with the enter key. Because pressing a key and then hitting enter seems redundent. Like when a program says press anykey to exit it is just one thing to do rather than the "is that your final answer" approach by typing the key and then pressing enter.
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 01-02-2006, 06:34 PM   #17 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

You can, to a point. When the enter key is pressed in a console, any characters are first read by an input device (or extracted and discarded with ignore) then a newline character is generated at the end ('\n') so ignoring up until a newline character will do what you want.

However, one caveat to this is if any other newline characters are still in the input buffer (via an earlier input or something of the sort) then the one ignore() will find that newline and end the statement without waiting like you wanted. This is not normal program flow however, as long as the user of the program enters only what they are supposed to, one could very well write a program with input where this would not happen.

Thus for safety in a program with user-supplied inputs, use two ignore() statements at the end with a newline delimiter, with no user-supplied input, one ignore().

Last edited by Sep102; 01-03-2006 at 11:22 PM.
Sep102 is offline   Reply With Quote
Old 06-23-2007, 02:54 AM   #18 (permalink)
Forum Novice
 
Join Date: Oct 2005
Posts: 142
Default

in MSVC 2005:

1. CTRL+F5 to compile in debug, returns a "Press any key to continue..." prompt.

2. system("PAUSE"); returns a "Press any key to continue..." prompt

3. cin.get(); waits for any key input

4. Make a batch file and add a system("PAUSE");, returns a "Press any key to continue..." prompt
Shadow-Sigma is offline   Reply With Quote
Old 03-27-2008, 09:26 AM   #19 (permalink)
Forum Expert
 
Join Date: Dec 2003
Location: Sitting in a chair fulfilling my life's goal
Age: 22
Posts: 2,577
Send a message via AIM to Killamus Send a message via MSN to Killamus
Default

Code:
#include "conio.h"
char c = '\n';
while ( c = getch() && c != 'yourchar' )
{
}
Standard C lib, I'd think it'd be supported on any OS, though I don't honestly know. Basically, it'll keep asking for a new character until it gets the one it likes.
__________________
Procrastinators unite!
Tomorrow.
Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders.
Killamus is online now   Reply With Quote
Reply

Bookmarks


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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5